简体   繁体   English

张量的形状为[?,0]-如何重塑为[?,]

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

相关问题 如何将形状(3、1、2)的3D张量重塑为(1、2、3) - How reshape 3D tensor of shape (3, 1, 2) to (1, 2, 3) pytorch 关于张量形状的问题。 以及如何重塑张量 - pytorch question about tensor shape. And how to reshape a tensor reshape 的输入是一个有 37632 个值的张量,但请求的形状有 150528 - Input to reshape is a tensor with 37632 values, but the requested shape has 150528 重塑的输入是张量为40804的张量,但请求的形状为10201 - Input to reshape is a tensor with 40804 values, but the requested shape has 10201 InvalidArgumentError:重塑的输入是一个178802值的张量,但请求的形状有89401 - InvalidArgumentError: Input to reshape is a tensor with 178802 values, but the requested shape has 89401 无效参数:reshape 的输入是一个有 64 个值的张量,但请求的形状有 4 个 - Invalid argument: Input to reshape is a tensor with 64 values, but the requested shape has 4 InvalidArgumentError:reshape 的输入是一个值为 0 的张量,但请求的形状有 54912 - InvalidArgumentError: Input to reshape is a tensor with 0 values, but the requested shape has 54912 InvalidArgumentError:reshape 的输入是一个具有 405000 个值的张量,但请求的形状有 1920000 [Op:Reshape] - InvalidArgumentError:Input to reshape is a tensor with 405000 values, but the requested shape has 1920000[Op:Reshape] InvalidArgumentError:reshape 的输入是具有 27000 个值的张量,但请求的形状有 810000 [Op:Reshape] - InvalidArgumentError: Input to reshape is a tensor with 27000 values, but the requested shape has 810000 [Op:Reshape] reshape 的输入是一个有 128 个值的张量,但请求的形状有 32 个 [Op:Reshape] - Input to reshape is a tensor with 128 values, but the requested shape has 32 [Op:Reshape]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM