[英]How to reshape a ragged tensor?
假设您将两个 3 维嵌入序列堆叠到一个不规则张量中:
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.