[英]Tensor has shape [?, 0] — how to reshape to [?,]
When src
has shape [?]
, tf.gather(src, tf.where(src != 0))
returns a tensor with shape [?, 0]
. 当
src
具有[?]
形状时, tf.gather(src, tf.where(src != 0))
返回具有[?, 0]
形状的张量。 I'm not sure how a dimension can have size 0, and I'm especially unsure how to change the tensor back. 我不确定尺寸的大小如何为0,并且我尤其不确定如何将张量改回来。 I didn't find anything in the documentation to explain this, either.
我也没有在文档中找到任何可以解释这一点的内容。
I tried to tf.transpose(tensor)[0]
, but the first dimension of the transposed tensor has size 0 and cannot be accessed! 我尝试了
tf.transpose(tensor)[0]
,但是转置张量的第一个维度的大小为0,无法访问! What's wrong? 怎么了?
I think you should use tf.not_equal
to perform elementwise comparison on the tensor. 我认为您应该使用
tf.not_equal
在张量上执行元素比较。
src = tf.constant([0, 1, 1, 0], dtype=tf.int8)
tf.gather(src, tf.where(tf.not_equal(src, 0))).eval(session=tf.Session())
array([[1],
[1]], dtype=int8)
You can also shorten this a bit and use tf.boolean_mask
instead of tf.where
and tf.gather
: 您也可以将其缩短一点,并使用
tf.boolean_mask
代替tf.where
和tf.gather
:
tf.boolean_mask(src, tf.not_equal(src, 0)).eval(session=tf.Session())
array([1, 1], dtype=int8)
Note the difference in the shape of the outputs. 注意输出形状的差异。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.