我目前正在 Paperspace/Gradient Notebooks(Python 3.8.10、Tensorflow 2.7.0)上训练 CNN
训练时间出奇地长,并且似乎使用了 >200% 的 CPU,但只使用了 15-20% 的 GPU。 Tensorflow 似乎可以识别 GPU:
此外,我按照他们的模板使用 tf.device() 设置训练:
try:
with tf.device('/device:GPU:0'):
model_Sezer.fit(train_dataset,
epochs = 100,
validation_data = validation_dataset,
callbacks = [tensorboard_callback, checkpoint_Accuracy,],
class_weight = class_weight
)
except RuntimeError as e:
print(e)
有谁知道如何在 GPU 上完全训练?
确保您拥有支持 CUDA 的 NVIDIA GPU 并安装了 CUDA 工具包。阅读this文章,了解有关先决条件的更多信息。
import os
import tensorflow as tf
# suppress info and warnings outputted by tensorflow
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
tf.compat.v1.logging.set_verbosity(tf.compat.v1.logging.ERROR)
# enable memory growth for gpu devices
# source: https://stackoverflow.com/a/55541385/8849692
gpu_devices = tf.config.experimental.list_physical_devices('GPU')
if gpu_devices:
for device in gpu_devices:
tf.config.experimental.set_memory_growth(device, True)
这是我在所有项目中使用的。这需要位于主文件的顶部,并且如果能够找到 GPU 设备,则应该可以正常工作,无需在
tf.device()
上运行它。