Tensorflow在梯度上不使用GPU

nhaq1z21  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(111)

我目前正在Paperspace/Gradient Notebooks上训练CNN(Python 3.8.10,Tensorflow 2.7.0)
训练需要惊人的时间,似乎超过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上进行完整训练吗?

w46czmvw

w46czmvw1#

确保您拥有支持CUDA的NVIDIA GPU,并安装了CUDA Toolkit。阅读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()上运行它。

相关问题