Python 急切执行-内部错误:找不到节点名称的有效设备;Sqrt";

Python 急切执行-内部错误:找不到节点名称的有效设备;Sqrt";,python,tensorflow,Python,Tensorflow,如果启用了急切执行,TensorFlow平方根函数tf.sqrt()将导致内部错误 import tensorflow as tf # enable eager execution tf.enable_eager_execution() > tf.pow(2,4) 'Out': <tf.Tensor: id=48, shape=(), dtype=int32, numpy=16> > tf.sqrt(4) >>> Traceback (most

如果启用了急切执行,TensorFlow平方根函数
tf.sqrt()
将导致
内部错误

import tensorflow as tf

# enable eager execution
tf.enable_eager_execution()

> tf.pow(2,4)
'Out': <tf.Tensor: id=48, shape=(), dtype=int32, numpy=16>

> tf.sqrt(4)

>>> Traceback (most recent call last):

  File "<ipython-input-21-5dc8e2f4780c>", line 1, in <module>
    tf.sqrt(4)

  File "/Users/ekababisong/anaconda3/envs/py36_dl/lib/python3.6/site-packages/
     tensorflow/python/ops/math_ops.py", line 365, in sqrt
         return gen_math_ops.sqrt(x, name=name)

  File "/Users/ekababisong/anaconda3/envs/py36_dl/lib/python3.6/site-packages/
     tensorflow/python/ops/gen_math_ops.py", line 7795, in sqrt
        _six.raise_from(_core._status_to_exception(e.code, message), None)

  File "<string>", line 3, in raise_from

InternalError: Could not find valid device for node name: "Sqrt"
op: "Sqrt"
input: "dummy_input"
attr {
  key: "T"
  value {
    type: DT_INT32
  }
}
 [Op:Sqrt] name: Sqrt/
将tensorflow导入为tf
#启用即时执行
tf.enable_eager_execution()
>战俘特遣部队(2,4)
“出去”:
>tf.sqrt(4)
>>>回溯(最近一次呼叫最后一次):
文件“”,第1行,在
tf.sqrt(4)
文件“/Users/ekababisong/anaconda3/envs/py36_dl/lib/python3.6/site-packages/
tensorflow/python/ops/math_ops.py”,第365行,sqrt格式
返回gen_math_ops.sqrt(x,name=name)
文件“/Users/ekababisong/anaconda3/envs/py36_dl/lib/python3.6/site-packages/
tensorflow/python/ops/gen_math_ops.py”,第7795行,sqrt格式
_六、从(_核心。_状态_到_异常(例如代码、消息),无)
文件“”,第3行,从
内部错误:找不到节点名为“Sqrt”的有效设备
作品:《Sqrt》
输入:“虚拟输入”
属性{
键:“T”
价值观{
类型:DT_INT32
}
}
[Op:Sqrt]名称:Sqrt/

我在尝试将图像通过卷积滤波器时遇到了类似的错误。正如P-Gn所说,通过简单地将其转换为float就解决了这个问题

x=tf.cast(x,tf.float32)

在尝试在字典中查找Nan值时遇到类似错误,p-Gn的解决方案有效

TF2.0 RC, 之前(内部错误:找不到节点的有效设备):

之后:

any(tf.math.is_nan(tf.cast(val, tf.float32) for val in dict.values())

返回真/假

在这种情况下,值仅为int,使用numpy可能是个好主意

如果您仍然想使用TensorFlow,可以这样做:

tf.math.sqrt(tf.convert_to_tensor(4, dtype='float32'))


在TensorFlow 2上测试。

首先尝试转换为浮点。@p-Gn您怎么知道它是浮点?我之所以这么说是因为指定浮点也解决了conv1d的问题我的输入张量是float32,我仍然有相同的错误,解决方案?@DsCpp你试过转换成int吗?(即,
x=tf.cast(x,tf.int32
)(͜͡ʖ͡͡)@DsCpp如果您正在调试
restrape()
像我一样,您可能需要确保第二个参数
restrape(x,[此参数])
是一个整数元组。我在将旧的python2代码转换为Python3时遇到了相同的错误,不知道为什么。Did tf.cast,现在没有错误,不知道为什么。对于简单的强制转换,出现了奇怪的错误提示。我想这是因为两个操作数的数据类型不同。操作时出现了某种数据不匹配错误。
tf.math.sqrt(tf.convert_to_tensor(4, dtype='float32'))
tf.sqrt(tf.convert_to_tensor(4, dtype='float32'))