我正在学习TensorFlow急切执行的演示。当我尝试使用" 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))
完整错误消息:
追溯(最近的最新电话(:
文件",第4行,在 打印(tf.matmul(a,a((
文件 " c: python python35_64 lib site-packages tensorflow python 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(,无(
file",第3行,在rish_from
中无效的仪器:矛盾的设备上的张量:无法计算 预计输入#0的matmul将打开 /job:localhost/epplica:0/任务:0/设备:GPU:0,但实际上在 /job:localhost/epplica:0/任务:0/设备:cpu:0(操作运行在运行 /job:local -Host/副本:0/任务:0/设备: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
(:
追溯(最近的最新电话(:
文件",第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必须是tf.configproto,但获得了
如何解决错误?我也不知道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))
- 错误消息中描述的修复程序当然可以使用调整,请尝试:
tfe.enable_eager_execution(device_policy=tfe.DEVICE_PLACEMENT_SILENT)
而不是(请注意device_policy
关键字参数的使用(。
- 另一个建议是使用
.cpu()
或.gpu()
方法,因此类似:
A = A.gpu() print(tf.matmul(A, A))
- 这似乎是演示中的错误。但是这里发生的事情是张量
A
放置在CPU内存中,我们要求在GPU上执行矩阵乘法。因此,必须将A
张量从CPU(又称"主机"(内存复制到GPU(又称"设备"(内存。可以明确完成,也可以通过将device_policy
参数设置为enable_eager_execution()
-可以告诉TensorFlow运行时,可以在需要时静静地在设备之间复制张量。
希望会有所帮助。