Tensorflow加载模型报错InvalidArgumentError (see above for traceback):Connot assign a device...

错误提示:

InvalidArgumentError (see above for traceback): Cannot assign a device for operation G_paper_1/Run/G_paper_1/latents_in: node G_paper_1/Run/G_paper_1/latents_in (defined at /home/abc/Downloads/iGAN-HD-master/iGAN_HD-v2-1024/tfutil.py:508) was explicitly assigned to /device:GPU:0 but available devices are [ /job:localhost/replica:0/task:0/device:CPU:0, /job:localhost/replica:0/task:0/device:XLA_CPU:0 ]. Make sure the device specification refers to a valid device. The requested device appears to be a GPU, but CUDA is not enabled.

原因是tf里的一个bug,解决方法是设置配置里面的allow_soft_placement=True,当运行在出现不允许在GPU运行的时候,可以切换到CPU运行。

解决方法:
tf.ConfigProto()内添加allow_soft_placement=True

run_config = tf.ConfigProto(allow_soft_placement=True)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这个错误是因为您尝试在 GPU 上执行一个不支持 GPU 的操作。具体地说,在您的代码中,`IteratorToStringHandle` 这个操作被指定在了 `/device:GPU:0` 上执行,但是这个操作只能在 CPU 上运行。因此,TensorFlow 抛出了这个错误。 要解决这个问题,您需要将 `IteratorToStringHandle` 这个操作从 GPU 设备上移除,使其能够在 CPU 上运行。一种简单的方法是将 `with tf.device("/gpu:0"):` 改为 `with tf.device("/cpu:0"):`,这样包括 `IteratorToStringHandle` 在内的所有操作都将在 CPU 上运行。修改后的代码如下所示: ```python with tf.device("/cpu:0"): # 在这里添加您的 TensorFlow 训练代码 ``` 如果您需要将某些操作固定在 GPU 上执行,可以使用 `tf.device` 和 `tf.compat.v1.disable_eager_execution()` 来手动控制 TensorFlow 的计算图。具体来说,您可以在 `with tf.device("/gpu:0"):` 块中创建一个 TensorFlow 计算图,并使用 `tf.compat.v1.Session` 来运行这个计算图。这样,您就可以将某些操作固定在 GPU 上运行,而将其他操作放在 CPU 上运行。下面是一个示例代码: ```python import tensorflow as tf # 关闭 Eager Execution tf.compat.v1.disable_eager_execution() # 定义一个 TensorFlow 计算图 with tf.device("/gpu:0"): x = tf.constant([1.0, 2.0, 3.0]) y = tf.constant([4.0, 5.0, 6.0]) z = tf.add(x, y) # 创建一个 TensorFlow 会话并运行计算图 with tf.compat.v1.Session() as sess: # 将 z 固定在 GPU 上运行 result = sess.run(z) print(result) ``` 在这个示例代码中,`x` 和 `y` 这两个操作被固定在 GPU 上执行,而 `z` 这个操作则会被自动放到 GPU 上执行。最终,您将获得一个包含 `[5.0, 7.0, 9.0]` 的 NumPy 数组。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值