[英](tensorflow 2.4.1)how to get a ragged tensor has determinate last dimension shape?
like this:像这样:
''' '''
a = [1.0, 2.0, 3.0]
b = [[a, a, a, a], [a, a, a], [a, a, a, a, a, a, a], [a, a]]
c = tf.ragged.constant(b, dtype=tf.float32)
''' I got a tensor with shape: [4, None, None], but i expect [4, None, 3], ''' 我得到了一个形状为 [4, None, None] 的张量,但我期望 [4, None, 3],
According to the documentation ( https://www.tensorflow.org/api_docs/python/tf/RaggedTensor#uniform_inner_dimensions_2 ), to get a uniform inner dimension on a ragged tensor, you need to start from a multidimensional tensor of values so TensorFlow knows that dimension is uniform According to the documentation ( https://www.tensorflow.org/api_docs/python/tf/RaggedTensor#uniform_inner_dimensions_2 ), to get a uniform inner dimension on a ragged tensor, you need to start from a multidimensional tensor of values so TensorFlow knows该维度是统一的
For your example case, you would want to start by making b
a 16x3 matrix and then using one of the "from_row_" methods (eg tf.RaggedTensor.from_row_lengths()
) to partition it into your ragged splits.对于您的示例情况,您首先要使
b
成为 16x3 矩阵,然后使用“from_row_”方法之一(例如tf.RaggedTensor.from_row_lengths()
)将其划分为参差不齐的分割。 Eg something like例如像
import numpy as np
import tensorflow as tf
a = np.array([1.0, 2.0, 3.0])
b = np.tile(a, (16,1))
c = tf.RaggedTensor.from_row_lengths(values= b, row_lengths = [4,3,7,2])
should get you the ragged tensor you want with shape [4,None,3]
.应该为您提供形状为
[4,None,3]
的参差不齐的张量。
I know I'm too late, but for the next person that encounters this issue: You can specify how many ragged dimensions the tensor should have using ragged_rank
.我知道我为时已晚,但是对于遇到此问题的下一个人:您可以使用
ragged_rank
指定张量应具有多少参差不齐的维度。 You can do as follows您可以执行以下操作
a = [1.0, 2.0, 3.0]
b = [[a, a, a, a], [a, a, a], [a, a, a, a, a, a, a], [a, a]]
c = tf.ragged.constant(b, dtype=tf.float32, ragged_rank=1) #<--here
c.shape
>>> TensorShape([4, None, 3])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.