[英]Tensorflow concat ragged tensor returning wrong shape
我需要连接两个参差不齐的张量,将最后一个维度保持为固定大小2
。
检查model.output_shape
我得到了想要的(None, None, 2)
。 但是当我调用模型时,我得到(batch_size, None, None)
。 如何获得正确的形状? 代码:
import tensorflow as tf
a_input = tf.keras.layers.Input([None, 2], ragged=True)
b_input = tf.keras.layers.Input([None, 2], ragged=True)
output = tf.concat([a_input, b_input], axis=1)
model = tf.keras.Model([a_input, b_input], output)
a = tf.ragged.constant([
[[1, 2], [3, 4], [5, 6]],
[[1, 2], [3, 4]],
[[1, 2]],
])
b = tf.ragged.constant([
[[1, 2]],
[[1, 2], [3, 4], [5, 6], [7, 8]],
[[1, 2], [3, 4]],
])
print(model.output_shape)
# (None, None, 2)
print(model([a, b]).shape)
# (3, None, None)
我找到了。 tf.ragged.constant
不认为最后一个维度是统一维度。 所以a.shape
是(3, None, None)
。 要解决这个问题,我需要使用ragged_rank
参数:
a = tf.ragged.constant([
[[1, 2], [3, 4], [5, 6]],
[[1, 2], [3, 4]],
[[1, 2]],
], ragged_rank=1)
print(a.shape)
# (3, None, 2)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.