python - 无法计算 Add 作为输入 #1(从零开始)应该是一个 int32 张量,但它是一个双张量 [Op:Add]
问题描述
import tensorflow as tf
a = tf.constant(6, name = 'constant_a')
b = tf.constant(3, name = 'constant_b')
c = tf.constant(10, name = 'constant_c')
d = tf.constant(5, name = 'constant_d')
mul = tf.multiply(a, b, name = 'mul')
div = tf.divide(c, d, name = 'div')
这一行给了我错误
addn = tf.add(mul, div)
InvalidArgumentError Traceback (most recent call last)
<ipython-input-8-387060d1ddd5> in <module>()
----> 1 addn = tf.add(mul, div)
~/.local/lib/python3.5/site-packages/tensorflow_core/python/ops/gen_math_ops.py in add(x, y, name)
341 raise
342 except _core._NotOkStatusException as e:
--> 343 _ops.raise_from_not_ok_status(e, name)
344 # Add nodes to the TensorFlow graph.
345 try:
~/.local/lib/python3.5/site-packages/tensorflow_core/python/framework/ops.py in raise_from_not_ok_status(e, name)
6604 message = e.message + (" name: " + name if name is not None else "")
6605 # pylint: disable=protected-access
-> 6606 six.raise_from(core._status_to_exception(e.code, message), None)
6607 # pylint: enable=protected-access
6608
~/.local/lib/python3.5/site-packages/six.py in raise_from(value, from_value)
InvalidArgumentError: cannot compute Add as input #1(zero-based) was expected to be a int32 tensor but is a double tensor [Op:Add]
解决方案
addn = tf.add(mul, tf.cast(div, tf.int32))
这有助于
tf.cast(
x,
dtype,
name=None
)
很抱歉问这个问题是否愚蠢!
推荐阅读
- android - Revenuecat 在 Android Studio 中检查订阅者状态和购买时出现错误
- unity3d - 无法在 EditorWindow 中更改按钮 GUIStyle 背景
- python - 外键可以连接到多个表吗?
- oracle - 在oracle中将表与Sum(column)按列分组
- java - Antlr 可以将嵌入式语法路由到单独的文件进行处理吗?
- node.js - 无法在 facebook 登录中使用 localhost 中的节点加载 url 错误
- c# - 创建产品捆绑包 Dynamics 365 CRM 时未设置默认价目表
- c# - CKFinder NLog 不记录
- python - 如何销毁Tkinter中所有同类标签?
- scala - Spark 执行器中内存数据结构的预期行为是什么?