我正在运行 Keras 模型,提交截止日期为 36 小时,如果我在 cpu 上训练我的模型大约需要 50 小时,有没有办法在 gpu 上运行 Keras?
我正在使用 Tensorflow 后端并在我的 Jupyter 笔记本上运行它,但没有安装 anaconda。
是的,您可以在 GPU 上运行 keras 模型。您必须先检查几件事。
你的系统有 GPU(Nvidia。因为 AMD 还没有工作)你已经安装了 tensorflow 的 GPU 版本你已经安装了 CUDA 安装说明验证 tensorflow 是否正在与 GPU 一起运行检查 GPU 是否正在工作
sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))
对于 TF > v2.0
sess = tf.compat.v1.Session(config=tf.compat.v1.ConfigProto(log_device_placement=True))
(感谢@nbro 和@Ferro 在评论中指出这一点)
或者
from tensorflow.python.client import device_lib
print(device_lib.list_local_devices())
输出将是这样的:
[
name: "/cpu:0"device_type: "CPU",
name: "/gpu:0"device_type: "GPU"
]
完成所有这些后,您的模型将在 GPU 上运行:
检查 keras(>=2.1.1) 是否正在使用 GPU:
from keras import backend as K
K.tensorflow_backend._get_available_gpus()
一切顺利。
2.0 兼容答案:虽然上面提到的答案详细解释了如何在 Keras 模型上使用 GPU,但我想解释如何为 Tensorflow Version 2.0
完成它。
要知道有多少 GPU 可用,我们可以使用以下代码:
print("Num GPUs Available: ", len(tf.config.experimental.list_physical_devices('GPU')))
要找出您的操作和张量分配给哪些设备,请将 tf.debugging.set_log_device_placement(True)
作为程序的第一条语句。
启用设备放置日志记录会导致打印任何张量分配或操作。例如,运行以下代码:
tf.debugging.set_log_device_placement(True)
# Create some tensors
a = tf.constant([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]])
b = tf.constant([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]])
c = tf.matmul(a, b)
print(c)
给出如下所示的输出:
在设备 /job:localhost/replica:0/task:0/device:GPU:0 中执行 op MatMul tf.Tensor( [[22. 28.] [49. 64.]], shape=(2, 2), dtype=float32)
有关详细信息,请参阅 this link
当然。我想你已经为 GPU 安装了 TensorFlow。
导入 keras 后需要添加以下块。我正在一台有 56 个核心 cpu 和一个 gpu 的机器上工作。
import keras
import tensorflow as tf
config = tf.ConfigProto( device_count = {'GPU': 1 , 'CPU': 56} )
sess = tf.Session(config=config)
keras.backend.set_session(sess)
当然,这种用法会强制执行我的机器的最大限制。您可以降低 cpu 和 gpu 消耗值。
module 'tensorflow' has no attribute 'ConfigProto'
当然。如果您在 Tensorflow 或 CNTk 后端上运行,您的代码将默认在您的 GPU 设备上运行。但如果是 Theano 后端,您可以使用以下
Theano 标志:“THEANO_FLAGS=device=gpu,floatX=float32 python my_keras_script.py”
我在 Windows 10 上使用 Anaconda 和 GTX 1660 Super。我首先在此 step-by-step 之后安装了 CUDA 环境。但是现在 Anaconda 上有一个 keras-gpu
元包,显然不需要事先安装 CUDA 和 cuDNN 库(无论如何我的库已经安装了)。
这就是我创建名为 keras_gpu
的专用环境的原因:
# need to downgrade from tensorflow 2.1 for my particular setup
conda create --name keras_gpu keras-gpu=2.3.1 tensorflow-gpu=2.0
要添加 @johncasey 的答案,但对于 TensorFlow 2.0,添加此块对我有用:
import tensorflow as tf
from tensorflow.python.keras import backend as K
# adjust values to your needs
config = tf.compat.v1.ConfigProto( device_count = {'GPU': 1 , 'CPU': 8} )
sess = tf.compat.v1.Session(config=config)
K.set_session(sess)
This post 解决了我得到的 set_session
错误:您需要使用 tensorflow 路径中的 keras 后端而不是 keras 本身。
使用 Tensorflow 2.5,以 @MonkeyBack 的回答为基础:
conda create --name keras_gpu keras-gpu tensorflow-gpu
# should show GPU is available
python -c "import tensorflow as tf;print('GPUs Available:', tf.config.list_physical_devices('GPU'))"
查看您的脚本是否在任务管理器中运行 GPU。如果不是,请怀疑您的 CUDA 版本是否适合您正在使用的 tensorflow 版本,正如其他答案已经建议的那样。
此外,需要一个适合 CUDA 版本的 CUDA DNN 库才能使用 TensorFlow 运行 GPU。从 here 下载/提取它并将 DLL(例如 cudnn64_7.dll)放入 CUDA bin 文件夹(例如 C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.1\bin)。
Could not find any downloads that satisfy the requirement tensorflow in /usr/local/lib/python2.7/dist-packages Downloading/unpacking tensorflow Cleaning up... No distributions at all found for tensorflow in /usr/local/lib/python2.7/dist-packages Storing debug log for failure in /home/hyperworks/.pip/pip.log
K.tensorflow_backend._get_available_gpus()
在 TensorFlow 2.0 中不起作用。