0

我想在 Tensorflow 2 中将一维张量转换为二维形状,并给出每行中的元素数。我找到了一个像这样使用 RaggedTensor 的解决方案

numbers = tf.constant([1,3,2,5,4])
c0 = tf.ones(shape=(15,)) # the tensor need to be reshape
rc0 = tf.RaggedTensor.from_row_lengths(values=c0, row_lengths=numbers)
c1 = rc0.to_tensor()

c1 的最终值应该是

<tf.Tensor: shape=(5, 5), dtype=float32, numpy=
array([[1., 0., 0., 0., 0.],
       [1., 1., 1., 0., 0.],
       [1., 1., 0., 0., 0.],
       [1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 0.]], dtype=float32)>

我发现当输入张量的大小很大时它会拒绝工作,并且性能不够好。

还有其他高性能解决方案吗?

4

0 回答 0