I am trying to figure out how to use a Masking layer with the functional API in Keras.
Using the non-functional Keras syntax I can easily create a GRU model that masks away all zero values like this:
model = tf.keras.Sequential()
model.add( tf.keras.layers.Masking( mask_value = 0.0, input_shape = ( nTimeSteps, nVariables ) ) )
model.add( tf.keras.layers.GRU( 32 ) )
model.add( tf.keras.layers.Dense( 10, activation = "softmax" ) )
opt = tf.keras.optimizers.SGD( learning_rate = 0.001 )
model.compile( loss = 'categorical_crossentropy', optimizer = opt, metrics = ['accuracy'] )
My attempt at replicating this model using the functional API so far looks like this:
x = tf.keras.layers.Masking( mask_value = 0.0, input_shape = ( nTimeSteps, nVariables ) )
x = tf.keras.layers.GRU( 32 )( x )
z = tf.keras.layers.Dense( numberOfOutputs, activation = "softmax" )( x )
model = tf.keras.Model( inputs = x, outputs = z )
opt = tf.keras.optimizers.SGD( learning_rate = 0.001 )
model.compile( loss = 'categorical_crossentropy', optimizer = opt, metrics = ['accuracy'] )
However, it does not work – it produces the following error:
AttributeError: 'Masking' object has no attribute 'shape'
What is the correct way of using a masking layer with the functional API?