I am on tensorflow v2 on google colab using tf.keras. I am trying to use embedding with masking follow by global average. Here's my code:
vocab_size = 1500
inputs = Input(shape=(None,), dtype=tf.int32, name='word_sequence')
x = Embedding(input_dim=vocab_size, output_dim=16, mask_zero=True)(inputs)
outputs = tf.keras.layers.GlobalAveragePooling1D()(x)
model = Model(inputs, outputs)
But I got this error:
TypeError: Failed to convert object of type to Tensor. Contents: [-1, None, 1]. Consider casting elements to a supported type.
If I provide an explicit length of sequence Input(shape=(10,), .....), then it seems to have no error (although I havent tested it with sample data). I wonder why you would need to specify an explicit sequence length, I thought this could be done lazily at runtime when the layer first encounters the data.
Furthermore, the following works (taken from "masking and padding" tf tutorial):
inputs = tf.keras.Input(shape=(None,), dtype='int32')
x = layers.Embedding(input_dim=5000, output_dim=16, mask_zero=True)(inputs)
outputs = layers.LSTM(32)(x)
model = tf.keras.Model(inputs, outputs)
For LSTM, it seems like it is happy with input shape of None during functional api construction of model.
Could someone pls explain how is this bad with GlobalAveragePooling1D, or that this should work but I did something wrong?
Thanks.