简体   繁体   English

重塑 Tensorflow RaggedTensor

[英]Reshape Tensorflow RaggedTensor

I have a 4D RaggedTensor of shape (batch_size, None, None, 100), I want to create from this a tensor of shape (batch_size, None, 100).我有一个形状为 (batch_size, None, None, 100) 的 4D RaggedTensor,我想从中创建一个形状为 (batch_size, None, 100) 的张量。 So basically merging 1st and 2nd dimensions, but not including any padding ([1,2,3], [4] => [1,2,3,4]) and not converting to a dense tensor first.所以基本上合并第一个和第二个维度,但不包括任何填充([1,2,3],[4] => [1,2,3,4])并且不首先转换为密集张量。 Is there a way to do this?有没有办法做到这一点? If not what could be a work around?如果不是什么可以解决?

After some more reading and trying, I have found the answer, which requires using row_starts twice for each of the two dimensions.经过更多的阅读和尝试,我找到了答案,这需要对两个维度中的每一个使用row_starts两次。 The result loos like this:结果如下所示:

row_starts = [my_ragged_tensor.values.row_starts()[row_start]
                      for row_start in my_ragged_tensor.row_starts()]

my_ragged_tensor_flat = tf.RaggedTensor.from_row_starts(my_ragged_tensor.flat_values, row_starts)

This will change the shape of the "my_ragged_tensor" from (batch_size, None, None, 100) to (batch_size, None, 100) merging the two middle dimensions.这会将“my_ragged_tensor”的形状从 (batch_size, None, None, 100) 更改为 (batch_size, None, 100) 合并两个中间维度。

Edit:编辑:

much easier way my_ragged_tensor.merge_dims(1,2)更简单的方法my_ragged_tensor.merge_dims(1,2)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM