I want to pad a tensor. In the example below I flatten the input tensor and then pad it to size 900. Padding x works fine when using an literal integer for the padding, e.g.:
num_units = 900
print('*** inputTensor')
print(inputTensor)
x = tf.keras.layers.Flatten()(inputTensor)
print('*** x before padding')
print(x)
paddings = [[0, 0], [0, 584]] # OK
x = tf.pad(x, paddings)
print('*** x after padding')
print(x)
Result:
*** inputTensor
KerasTensor(type_spec=TensorSpec(shape=(None, 79, 4), dtype=tf.float32, name='input_1'), name='input_1', description="created by layer 'input_1'")
*** x before padding
KerasTensor(type_spec=TensorSpec(shape=(None, 316), dtype=tf.float32, name=None), name='flatten/Reshape:0', description="created by layer 'flatten'")
*** x after padding
KerasTensor(type_spec=TensorSpec(shape=(None, 900), dtype=tf.float32, name=None), name='tf.compat.v1.pad/Pad:0', description="created by layer 'tf.compat.v1.pad'")
But it does not work if I calculate the padding value at runtime. I have tried multiple ways of setting the value to replace the 584 in the line marked OK above, but none work:
paddings = [[0, 0], [0, num_units - tf.shape(x)[1]]] # no good
paddings = [[0, 0], [0, num_units - tf.size(x)]] # no good
Both of the above (and other approaches) all change the final shape of x to be (None, None):
*** x after padding
KerasTensor(type_spec=TensorSpec(shape=(None, None), dtype=tf.float32, name=None), name='tf.compat.v1.pad/Pad:0', description="created by layer 'tf.compat.v1.pad'")
It seems that tf.pad requires integer values while the calculation results are tensors. What am I missing?
UPDATE
Got it working using the technique in this thread:
num_units = 900
print('inputTensor.shape', inputTensor.shape)
x = tf.keras.layers.Flatten()(inputTensor)
print('after flatten', x.shape)
x = tf.keras.layers.Reshape([-1, 1])(x)
print('after reshape', x.shape)
pad_amount = num_units - x.shape[1]
print('pad_amount', pad_amount)
x = tf.keras.layers.ZeroPadding1D(padding=(0, pad_amount))(x)
print('after padding', x.shape)
x = tf.keras.layers.Reshape([-1])(x)
print('after reshape', x.shape)
Output:
inputTensor.shape (None, 79, 4)
after flatten (None, 316)
after reshape (None, 316, 1)
pad_amount 584
after padding (None, 900, 1)
after reshape (None, 900)