TensorFlow中遇到的各种错误

Author:吾爱北方的母老虎

原创链接:https://blog.csdn.net/weixin_41010198/article/details/80339203


1.出现貌似检测不到设备了

错误提示内容如下:

InvalidArgumentError: Cannot assign a device for operation 'MatMul_11': Operation was explicitly assigned to /device:GPU:1 but available devices are [ /job:localhost/replica:0/task:0/device:CPU:0 ]. Make sure the device specification refers to a valid device.

import tensorflow as tf

# 创建一个变量, 初始化为标量 0.
state = tf.Variable(0, name="counter")

# 创建一个 op, 其作用是使 state 增加 1

one = tf.constant(1)
new_value = tf.add(state, one)
update = tf.assign(state, new_value)

# 启动图后, 变量必须先经过`初始化` (init) op 初始化,
# 首先必须增加一个`初始化` op 到图中.
init_op = tf.initialize_all_variables()

# 启动图, 运行 op
with tf.Session() as sess:
  # 运行 'init' op
  sess.run(init_op)
  # 打印 'state' 的初始值
  print(sess.run(state))
  # 运行 op, 更新 'state', 并打印 'state'
  for _ in range(3):
    sess.run(update)
    print(sess.run(state))

问题解决方式:

在Jupyter notebook中在Kernel下把程序Shutdown,然后重新启动一下就可以了Restart

有时可能会出现就是电脑没有GPU可用,智能用CPU,这种情况下,可以加一条语句,当找不到设备的时候,会自动默认去找CPU tf.ConfigProto(log_device_placement=True,allow_soft_placement=True) 


猜你喜欢

转载自blog.csdn.net/weixin_41010198/article/details/80339203