简体   繁体   English

当使用tf.cond()时,Tensorflow报告'TypeError:需要单个张量时的张量列表'

[英]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_shapetf.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.

相关问题 Tensorflow - 使用 tf.cond() 时出现类型错误 - Tensorflow - TypeError when using tf.cond() 类型错误:预期单个张量时的张量列表 - 将 const 与 tf.random_normal 一起使用时 - TypeError: List of Tensors when single Tensor expected - when using const with tf.random_normal 使用tf.cond()时,Tensorflow会为不必要的占位符请求输入 - Tensorflow asks inputs for unnecessary placeholders when using tf.cond() 我的 TensorFlow 输入函数中的错误:“TypeError: List of Tensor when single Tensor expected” - Error in my TensorFlow input function: "TypeError: List of Tensors when single Tensor expected" TypeError:由于 tensor_scatter_update 而预期单个张量时的张量列表 - TypeError: List of Tensors when single Tensor expected due to tensor_scatter_update 随机值张量的计算(例如tf.greater和tf.cond)未按预期工作 - Computations (such as tf.greater and tf.cond) on random value tensors not working as expected 错误:计算多项式时“单个张量预期时的张量列表” - Error: “List of Tensors when single Tensor expected” in computing a polynomial Tensorflow:tf.cond,如何返回多暗淡张量而不是简单值? - Tensorflow: tf.cond, how to return multi dim tensors instead of simple values? tf.cond:将元素添加到列表 - tf.cond : adding an element to a list Tensorflow:如果我的条件不是while循环中tf.cond的标量怎么办? - Tensorflow: What if my cond is not a scalar in tf.cond in while loop?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM