'training = False in Keras Sequential API

We can pass the training = False argument while calling the pre-trained model when using Keras Functional API as shown in this tutorial.

How to implement the same in Keras Sequential API?

Here's the code which I am trying to replicate using Sequential API:

inputs = tf.keras.Input( shape = ( TARGET_SIZE[0], TARGET_SIZE[1], 3 ) )
base_model = Xception( include_top = False, pooling = 'avg' )
base_model.trainable = False
x = base_model( inputs, training = False ) 
x = Dense( 512, activation = 'relu' )( x )
x = Dense( 256, activation = 'relu' )( x )
x = Dense( 128, activation = 'relu' )( x )
outputs = Dense( 6, activation = 'softmax' )( x )

Below is the code implementing this whole model without training = False using Sequential API like below:

model = Sequential()
model.add( Xception( include_top = False, pooling = 'avg', input_shape = ( TARGET_SIZE[0], TARGET_SIZE[1], 3 ) ) )
model.add( Dense( units = 512, activation = 'relu' ) )
model.add( Dense( units = 256, activation = 'relu' ) )
model.add( Dense( units = 128, activation = 'relu' ) )
model.add( Dense( 6, activation = 'softmax' ) )

But, I am unable to squeeze in the training = False argument with it.



Solution 1:[1]

You can try following, it worked for me:

load any base model, include_top = False, then (as example):

 flat1 = Flatten()(base_model.layers[-1].output, training=False)
    
 class1 = Dense(5, activation='relu')(flat1)
    
 output = Dense(1, activation='sigmoid')(class1) 

 model = Model(inputs=base_model.inputs, outputs=output) 

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Vahid Vajihinejad