为了使Keras使用GPU,必须做很多事情。将其放在jupyter笔记本顶部附近:
# confirm TensorFlow sees the GPU
from tensorflow.python.client import device_lib
assert 'GPU' in str(device_lib.list_local_devices())
# confirm Keras sees the GPU (for TensorFlow 1.X + Keras)
from keras import backend
assert len(backend.tensorflow_backend._get_available_gpus()) > 0
# confirm PyTorch sees the GPU
from torch import cuda
assert cuda.is_available()
assert cuda.device_count() > 0
print(cuda.get_device_name(cuda.current_device()))
注意:随着TensorFlow 2.0的发布,Keras现在已包含在TF API中。
0
运行keras脚本时,得到以下输出:
这是什么意思?我是否正在使用GPU或CPU版本的Tensorflow?
在安装keras之前,我正在使用Tensorflow的GPU版本。
sudo pip3 list
显示tensorflow-gpu(1.1.0)
,与tensorflow-cpu
类似。运行[此stackoverflow问题]中提到的命令,将得到以下信息: