[英]Tensorflow reports 'TypeError: List of Tensors when single Tensor expected' when using tf.cond()
I am using Tensorflow to code a model. 我正在使用Tensorflow编码模型。 Part of my conditional statement like:
我的条件语句的一部分如下:
new_shape = tf.cond(tf.equal(tf.shape(src_shape)[0], 2), lambda: src_shape, lambda: tf.constant([1, src_shape[0]]))
and src_shape
is the result of tf.shape()
. src_shape
是tf.shape()
的结果。
It reports TypeError: List of Tensors when single Tensor expected
. TypeError: List of Tensors when single Tensor expected
它将报告TypeError: List of Tensors when single Tensor expected
。 I know it is because tf.constant([1, src_shape[0]])
is a list of tensors, but I don't know how to implement my code in a legal way. 我知道是因为
tf.constant([1, src_shape[0]])
是张量的列表,但我不知道如何以合法方式实现代码。
I have try to remove tf.constant()
like 我已经尝试删除
tf.constant()
new_shape = tf.cond(tf.equal(tf.shape(src_shape)[0], 2), lambda: src_shape, lambda: [1, src_shape[0]])
but it reports ValueError: Incompatible return values of true_fn and false_fn: The two structures don't have the same nested structure.
但它报告
ValueError: Incompatible return values of true_fn and false_fn: The two structures don't have the same nested structure.
One way would be to use tf.stack, which stacks a list of rank-R tensors into one rank-(R+1) tensor. 一种方法是使用tf.stack,它将一个等级R张量的列表堆叠到一个等级R(R + 1)张量中。
lambda: tf.stack([1, src_shape[0]], axis=0)
Another solution would be using tf.concat using the right tf.reshape commands. 另一种解决方案是使用正确的tf.reshape命令使用tf.concat。
I have tried that tf.convert_to_tensor([1, src_shape[0]])
works. 我已经尝试过
tf.convert_to_tensor([1, src_shape[0]])
起作用。 It is an alternative solution. 这是一个替代解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.