使用GPU代替CPU,使用Keras和Tensorflow后端Linux

使用GPU代替CPU,使用Keras和Tensorflow后端Linux,tensorflow,deep-learning,keras,cudnn,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 我想我先安装了tensorflow的CPU版本-我不记得了,因为我花了一整天的时间让cuda和cu

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

>>> import keras
Using TensorFlow backend
…这意味着它在工作,但在CPU上,而不是GPU上。 我安装了Cuda和cuDNN并使用此环境:

conda create -n tensorflow python=3.5 anaconda 
我想我先安装了tensorflow的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一起工作)

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

pip install --upgrade --no-deps keras

您是否已尝试使用
pip安装tensorflow
?这将安装cpu版本,而
pip install tensorflow gpu
将安装gpu版本

首先,您必须确保tensorflow实际检测到您的CPU和GPU。您可以使用以下代码来检查它

from tensorflow.python.client import device_lib
device_lib.list_local_devices()
如果只列出了CPU而没有列出GPU,可能是因为您没有相同的tensorflow和tensorflow GPU版本(因为升级)。您可以通过使用检查版本(如果您使用pip)

如果它们不相同,则必须先卸载不兼容的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 install tensorflow==1.2 
pip install tensorflow-gpu==1.2
要使用pip安装,请执行以下操作:

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


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

要求已经更新:keras in./anaconda3/envs/tensorflow/lib/python3.5/site-packages-如何卸载keras?对不起,我是linuxok的新手,我卸载了它,并用你的链接重新安装了它,但仍然没有任何变化,我不知道该怎么做。。。键入pip uninstall tensorflow只会给我这样一条消息:无法卸载需求tensorflow,未安装您是否能够解决此问题。我面临着完全相同的问题?