使用GPU代替CPU与Keras和Tensorflow Backend for Linux

时间:2017-05-12 00:39:48

标签: tensorflow deep-learning keras cudnn

我无法让Keras使用Tensorflow的GPU版本而不是CPU。每次我导入keras时都会说:

>>> import keras
Using TensorFlow backend

...这意味着它正在工作,但在CPU上,而不是GPU。 我安装了Cuda和cuDNN并使用这个环境:

conda create -n tensorflow python=3.5 anaconda 

我认为我首先安装了张量流的CPU版本 - 我不记得因为我花了一整天才让cuda和cudnn工作。 无论如何,我也安装了GPU版本:

pip install --ignore-installed --upgrade \ https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow_gpu-1.1.0-cp35-cp35m-linux_x86_64.whl

它仍然给出相同的信息。我尝试通过以下代码检查正在使用的设备:

sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))

但我得到了这个输出,表明我正在使用设备0,我的GPU:

2017-05-12 02:14:10.746679: W 
tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow 
library wasn't compiled to use SSE4.1 instructions, but these are 
available on your machine and could speed up CPU computations.
2017-05-12 02:14:10.746735: W         
tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow 
library wasn't compiled to use SSE4.2 instructions, but these are 
available on your machine and could speed up CPU computations.
2017-05-12 02:14:10.746751: W 
tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow 
library wasn't compiled to use AVX instructions, but these are 
available on your machine and could speed up CPU computations.
2017-05-12 02:14:10.746764: W 
tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow 
library wasn't compiled to use AVX2 instructions, but these are 
available on your machine and could speed up CPU computations.
2017-05-12 02:14:10.746777: W 
tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow 
library wasn't compiled to use FMA instructions, but these are 
available on your machine and could speed up CPU computations.
2017-05-12 02:14:10.926330: I 
tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:901] successful 
NUMA node read from SysFS had negative value (-1), but there must be 
at least one NUMA node, so returning NUMA node zero
2017-05-12 02:14:10.926614: I 
tensorflow/core/common_runtime/gpu/gpu_device.cc:887] Found device 0 
with properties: 
name: GeForce GTX 1060 6GB
major: 6 minor: 1 memoryClockRate (GHz) 1.7845
pciBusID 0000:01:00.0
Total memory: 5.93GiB
Free memory: 5.51GiB
2017-05-12 02:14:10.926626: I 
tensorflow/core/common_runtime/gpu/gpu_device.cc:908] DMA: 0 
2017-05-12 02:14:10.926629: I 
tensorflow/core/common_runtime/gpu/gpu_device.cc:918] 0:   Y 
2017-05-12 02:14:10.926637: I 
tensorflow/core/common_runtime/gpu/gpu_device.cc:977] Creating 
TensorFlow device (/gpu:0) -> (device: 0, name: GeForce GTX 1060 6GB, 
pci bus id: 0000:01:00.0)
Device mapping:
/job:localhost/replica:0/task:0/gpu:0 -> device: 0, name: GeForce GTX 
1060 6GB, pci bus id: 0000:01:00.0
2017-05-12 02:14:10.949871: I 
tensorflow/core/common_runtime/direct_session.cc:257] Device mapping:
/job:localhost/replica:0/task:0/gpu:0 -> device: 0, name: GeForce GTX 
1060 6GB, pci bus id: 0000:01:00.0

我真的没事了。我唯一剩下的就是卸载anaconda并重新安装所有东西,我真的不想做,因为我真的花了一整天才能让它与keras和所有东西一起工作(还没有用GPU)< / p>

3 个答案:

答案 0 :(得分:1)

可能的是,使用默认选项安装keras将安装tensorflow的CPU版本。您可以卸载该版本,然后运行...

pip install --upgrade --no-deps keras

https://github.com/fchollet/keras/issues/5766

答案 1 :(得分:1)

首先,您必须确保tensorflow实际上检测到了CPU和GPU。您可以使用以下代码进行检查。

from tensorflow.python.client import device_lib
device_lib.list_local_devices()

如果仅列出CPU而不列出GPU,则可能是由于您没有相同的tensorflow和tensorflow-gpu版本(由于升级)引起的。您可以使用

检查版本(如果使用pip)
pip list

如果它们不相同,则必须先卸载不兼容的tensorflow或tensorflow-gpu版本,然后再安装与CUDA和CUDNN版本兼容的tensorflow和tensorflow-gpu版本。

例如,我使用的是CUDA 8.0和CUDNN 5.1.10,因此兼容的tensorflow和tensorflow-gpu版本为1.2版。

要使用pip卸载:

pip uninstall tensorflow
pip uninstall tensorflow-gpu

要通过pip安装:

pip install tensorflow==1.2 
pip install tensorflow-gpu==1.2

然后,您只需要检查tensorflow是否再次检测到您的CPU和GPU。如果是这样,那么您只需要运行代码,如果您使用的是keras,它将自动选择在GPU中运行计算。

这是tensorflow.org发布的经过测试的兼容组合的link,这里是与此相关的另一个问题的link

答案 2 :(得分:0)

您是否已尝试使用pip install tensorflow?这将安装cpu版本,而pip install tensorflow-gpu将安装gpu版本。 https://www.tensorflow.org/install/