假设你将两个三维嵌入序列堆叠成一个粗糙张量:
import tensorflow as tf
def foo(*args):
n_elements = tf.reduce_prod(args)
return tf.range(n_elements, dtype=tf.float32).reshape(args)
c = tf.ragged.stack((foo(2, 3), foo(5, 3)), axis=0)
assert c.shape == [2, None, None]
如何将c
投射到形状[2, None, 3]
(因为你知道这个张量是这个形状)?
尝试使用tf.RaggedTensor.from_row_splits
:
tf.RaggedTensor.from_row_splits(
values=c.merge_dims(0, 1).to_tensor(),
row_splits=[0, 3, 7]).shape
(2, None, 3)