PythontorFlow运行在GPU上而不是CPU上

2024-04-18 12:15:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我有下面的示例tensorflow代码

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))

我已经明确表示tf.装置('/cpu:0'但它给出了以下错误:

^{pr2}$

Tensorflow版本:1.3.0, python版本:3.6.1和Anaconda发行版


Tags: 代码nameimport版本示例devicetftensorflow
1条回答
网友
1楼 · 发布于 2024-04-18 12:15:51

我运行了这个程序,结果如下:

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.]]

看来matmul正在GPU上结束,而你的GPU不可用。我有相同版本的Tensorflow,但是python3.5.4。在

相关问题 更多 >