【发布时间】:2018-04-02 18:18:47
【问题描述】:
我正在学习 TensorFlow Eager Execution 的 demo。当我尝试单元格“GPU 使用情况”(见下文)时,出现错误提示变量未放置在 GPU 上。
import tensorflow as tf
import tensorflow.contrib.eager as tfe
tf.enable_eager_execution()
A = tf.constant([[2.0, 0.0], [0.0, 3.0]])
if tf.test.is_gpu_available() > 0:
with tf.device(tf.test.gpu_device_name()):
print(tf.matmul(A, A))
完整的错误信息:
Traceback(最近一次通话最后一次):
文件“”,第 4 行,在 打印(tf.matmul(A, A))
文件 "c:\python\python35_64\lib\site-packages\tensorflow\python\ops\math_ops.py", 第 2108 行,在 matmul 中 a, b, transpose_a=transpose_a, transpose_b=transpose_b, name=name)
文件 "c:\python\python35_64\lib\site-packages\tensorflow\python\ops\gen_math_ops.py", 第 4517 行,在 mat_mul _six.raise_from(_core._status_to_exception(e.code, message), None)
文件“”,第 3 行,在 raise_from 中
InvalidArgumentError:冲突设备上的张量:无法计算 MatMul 作为输入 #0 预计将打开 /job:localhost/replica:0/task:0/device:GPU:0 但实际上在 /job:localhost/replica:0/task:0/device:CPU:0(运行在 /job:localhost/replica:0/task:0/device:GPU:0) 可以复制张量 显式使用 .gpu() 或 .cpu(),或使用透明复制 tfe.enable_eager_execution(tfe.DEVICE_PLACEMENT_SILENT)。复印 设备之间的张量可能会减慢您的模型 [Op:MatMul] 名称: MatMul/
按照说明,我尝试了tfe.enable_eager_execution(tfe.DEVICE_PLACEMENT_SILENT),但它返回了另一个错误消息(从tfe.DEVICE_PLACEMENT_SILENT返回的值是2):
Traceback(最近一次通话最后一次):
文件“”,第 1 行,在 tfe.enable_eager_execution(tfe.DEVICE_PLACEMENT_SILENT)
文件 "c:\python\python35_64\lib\site-packages\tensorflow\python\framework\ops.py", 第 5229 行,在 enable_eager_execution "config 必须是 tf.ConfigProto,但得到了 %s" % type(config))
TypeError: config must be a tf.ConfigProto, but got
如何解决错误?我也不知道Tensors can be copied explicitly using .gpu() or .cpu() 是如何工作的。
谢谢。
感谢@ash,修改后的代码有效(需要重启笔记本)。
import tensorflow as tf
import tensorflow.contrib.eager as tfe
tfe.enable_eager_execution(device_policy=tfe.DEVICE_PLACEMENT_SILENT)
A = tf.constant([[2.0, 0.0], [0.0, 3.0]])
if tf.test.is_gpu_available() > 0:
with tf.device(tf.test.gpu_device_name()):
print(tf.matmul(A, A))
或者(需要重启笔记本),
import tensorflow as tf
import tensorflow.contrib.eager as tfe
tfe.enable_eager_execution()
A = tf.constant([[2.0, 0.0], [0.0, 3.0]])
if tf.test.is_gpu_available() > 0:
with tf.device(tf.test.gpu_device_name()):
A = A.gpu()
print(tf.matmul(A, A))
【问题讨论】:
标签: tensorflow