张量流急切的gpu错误

时间:2018-04-02 18:18:47

标签: tensorflow

我正在学习张量流急切执行的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))

完整的错误消息:

  

追踪(最近一次呼叫最后一次):

     

文件“”,第4行,in       print(tf.matmul(A,A))

     

文件   “C:\ python的\ python35_64 \ LIB \站点包\ 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 \站点包\ 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):

  

追踪(最近一次呼叫最后一次):

     

文件“”,第1行,in       tfe.enable_eager_execution(tfe.DEVICE_PLACEMENT_SILENT)

     

文件   “C:\ python的\ python35_64 \ LIB \站点包\ tensorflow \ python的\框架\ 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))

1 个答案:

答案 0 :(得分:1)

  • 错误消息中描述的修补程序当然可以使用调整,尝试:
  

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(a.k.a。" host")内存复制到GPU(a.k.a。" device")内存。这可以明确地完成,或者通过将device_policy参数设置为enable_eager_execution() - 可以告诉TensorFlow运行时在需要时在设备之间静默复制张量。

希望有所帮助。