简体   繁体   English

在GPU而不是CPU上运行的Python-Tensorflow

[英]Python-Tensorflow running on GPU instead of CPU

I have the sample tensorflow code below 我有下面的示例张量流代码

import tensorflow as tf
with tf.device('/cpu:0'):
  a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3], name='a')
  b = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3, 2], name='b')
c = tf.matmul(a, b)
sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))
print(sess)
print(sess.run(c))

explicitly i have given tf.device('/cpu:0') but it is giving the following error : 明确地我给了tf.device('/ cpu:0')但它给出了以下错误:

InvalidArgumentError (see above for traceback): Cannot assign a device for operation 'MatMul_5': Operation was explicitly assigned to /device:GPU:0 but available devices are [ /job:localhost/replica:0/task:0/cpu:0 ]. Make sure the device specification refers to a valid device.
     [[Node: MatMul_5 = MatMul[T=DT_FLOAT, transpose_a=false, transpose_b=false, _device="/device:GPU:0"](a_5, b_5)]]

Tensorflow version : 1.3.0, python version : 3.6.1 with Anaconda Distribution 使用Anaconda发行版的Tensorflow版本:1.3.0,python版本:3.6.1

I ran this and it worked, with the following output: 我运行了它,它的工作如下,输出如下:

MatMul: (MatMul): /job:localhost/replica:0/task:0/gpu:0
2017-10-20 15:50:04.556484: I 
tensorflow/core/common_runtime/simple_placer.cc:872] MatMul: 
(MatMul)/job:localhost/replica:0/task:0/gpu:0
b: (Const): /job:localhost/replica:0/task:0/cpu:0
2017-10-20 15:50:04.556595: I tensorflow/core/common_runtime/simple_placer.cc:872] b: (Const)/job:localhost/replica:0/task:0/cpu:0
a: (Const): /job:localhost/replica:0/task:0/cpu:0
2017-10-20 15:50:04.556624: I tensorflow/core/common_runtime/simple_placer.cc:872] a: (Const)/job:localhost/replica:0/task:0/cpu:0
[[ 22.  28.]
 [ 49.  64.]]

Looks like the matmul is ending up on GPU and your GPU is not available. 看来Matmul即将在GPU上运行,而您的GPU不可用。 I have the same version of Tensorflow, but Python 3.5.4. 我有相同版本的Tensorflow,但Python 3.5.4。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM