简体   繁体   English

Tensorflow concat 参差不齐的张量返回错误的形状

[英]Tensorflow concat ragged tensor returning wrong shape

I need to concat two ragged tensors keeping the last dimension with a fixed size 2 .我需要连接两个参差不齐的张量,将最后一个维度保持为固定大小2
Checking model.output_shape I get the desired (None, None, 2) .检查model.output_shape我得到了想要的(None, None, 2) But when I call the model, I get (batch_size, None, None) .但是当我调用模型时,我得到(batch_size, None, None) How do I get the right shape?如何获得正确的形状? Code:代码:

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)

I found it.我找到了。 The tf.ragged.constant does not consider the last dimension a uniform dimension. tf.ragged.constant不认为最后一个维度是统一维度。 So a.shape is (3, None, None) .所以a.shape(3, None, None) To fix that I need to use ragged_rank parameter:要解决这个问题,我需要使用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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM