张量-未知形状的垫张量



我正试图在Keras模型中将一个形状未知(但秩固定(的张量填充到一个固定形状。张量的类型是string,所以我不能使用tf.keras.preprocessing.sequence.pad_sequencestf.image.pad_to_bounding_box

我想使用tf.pad,但问题是我不知道张量的形状,所以我必须动态计算填充长度。我该怎么做?

我目前的方法是这样的,但不起作用:

def pad_to_length(t: tf.Tensor, target_length: int):
return tf.pad(t, [0, target_length-t.shape[0]], 'REFLECT')

因为t可以具有动态形状,所以t.shape将是(None,),所以我得到了错误:TypeError: unsupported operand type(s) for -: 'int' and 'NoneType'

使用动态形状tf.shape,而不是像那样使用静态形状.shape

def pad_to_length(t: tf.Tensor, target_length: int):
return tf.pad(t, [[0, target_length - tf.shape(t)[0]]], 'REFLECT')

对于秩为1的张量CCD_ 11。

最新更新