python - 使用 tf.cond() 时,Tensorflow 报告“TypeError:预期单个张量时的张量列表”
问题描述
我正在使用 Tensorflow 编写模型。我的条件语句的一部分,例如:
new_shape = tf.cond(tf.equal(tf.shape(src_shape)[0], 2), lambda: src_shape, lambda: tf.constant([1, src_shape[0]]))
并且src_shape
是 的结果tf.shape()
。
它报告TypeError: List of Tensors when single Tensor expected
。我知道这是因为tf.constant([1, src_shape[0]])
是张量列表,但我不知道如何以合法的方式实现我的代码。
我试图删除tf.constant()
喜欢
new_shape = tf.cond(tf.equal(tf.shape(src_shape)[0], 2), lambda: src_shape, lambda: [1, src_shape[0]])
但它报告ValueError: Incompatible return values of true_fn and false_fn: The two structures don't have the same nested structure.
解决方案
一种方法是使用 tf.stack,它将 rank-R 张量列表堆叠成一个 rank-(R+1) 张量。
lambda: tf.stack([1, src_shape[0]], axis=0)
另一种解决方案是使用 tf.concat 使用正确的 tf.reshape 命令。
推荐阅读
- flutter - 从 dart/flutter 中的异步方法生成静态列表
- google-colaboratory - 如何使用 Android Management API 安装私有应用程序?
- java - Mac 设备访问在 C 中有效,但在 Java/JNA 中的等效代码不可用
- python - Selenium Python,我尝试关闭一个 cookie 按钮,但它结束了点击广告
- java - 对于可以用 1byte 表示的字符,字符和字节流转换为字符之间的区别
- python-3.x - 如何重建一个稳定的环境(IPython)
- python - 带有文件的 Python 程序可以在任何计算机上运行吗?
- python - Python,解压和重新排序有序字典列表
- c# - 如何将双引号添加到作为字符串内容的 xml 标记属性
- c++ - 不能在 std::function #2 中使用复杂的 C++ 函数