问题:Cannot assign a device for operation Variable

部署运行你感兴趣的模型镜像

报错代码:

import tensorflow as tf
import numpy as np
import os
os.environ["CUDA_DEVICES_ORDER"] = "PCI_BUS_ID"
os.environ["CUDA_VISIBLE_DEVICES"] = "0,1"

with tf.device("/gpu:0"): ## 与下面注释的命令等价
# with tf.device("/job:localhost/replica:0/task:0/device:GPU:0"):
    a = tf.Variable(0,dtype=tf.float32)

with tf.device("/gpu:1"):
    b = tf.constant(0, shape=[1], dtype=tf.float32)

config = tf.ConfigProto()
config.gpu_options.allow_growth = True
config.log_device_placement = True
with tf.Session(config=config) as sess:
    sess.run(tf.global_variables_initializer())
    c = sess.run(a)
    b_ = sess.run(b)
    print(b_,c)

报错:

tensorflow.python.framework.errors_impl.InvalidArgumentError: Cannot assign a device for operation Variable: node Variable (defined at /wzg_code/×××/multi_gpu_test.py:19) was explicitly assigned to /device:GPU:1 but available devices are [ /job:localhost/replica:0/task:0/device:CPU:0, /job:localhost/replica:0/task:0/device:XLA_CPU:0, /job:localhost/replica:0/task:0/device:XLA_GPU:0, /job:localhost/replica:0/task:0/device:XLA_GPU:1 ]. Make sure the device specification refers to a valid device.
	 [[Variable]]

当时考虑了将 tf.device("gpu:0") 的位置改成: tf.device(“/job:localhost/replica:0/task:0/device:XLA_GPU:0”),仍然报错,最后发现需要在sess的配置上进行修改,增加 config.allow_soft_placement=True,虽然不知道为什么,但是work了。更正后的代码如下:

import tensorflow as tf
import numpy as np
import os
os.environ["CUDA_DEVICES_ORDER"] = "PCI_BUS_ID"
os.environ["CUDA_VISIBLE_DEVICES"] = "0,1"

with tf.device("/gpu:0"):
# with tf.device("/job:localhost/replica:0/task:0/device:GPU:0"):
    a = tf.Variable(0,dtype=tf.float32)

with tf.device("/gpu:1"):
    b = tf.constant(0, shape=[1], dtype=tf.float32)


#config = tf.ConfigProto()
config = tf.ConfigProto(allow_soft_placement=True)
config.gpu_options.allow_growth = True
config.log_device_placement = True
with tf.Session(config=config) as sess:
    sess.run(tf.global_variables_initializer())
    c = sess.run(a)
    b_ = sess.run(b)
    print(b_,c)

 

您可能感兴趣的与本文相关的镜像

TensorFlow-v2.15

TensorFlow-v2.15

TensorFlow

TensorFlow 是由Google Brain 团队开发的开源机器学习框架,广泛应用于深度学习研究和生产环境。 它提供了一个灵活的平台,用于构建和训练各种机器学习模型

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值