当使用Keras Functional API调用预训练模型时,我们可以传递training = False
参数,如此教程所示。
如何在Keras Sequential API中实现相同的效果?
以下是我正试图使用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 )
以下是使用顺序API实现整个模型的代码,
training = False
没有使用,如下所示: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' ) )
但是,我无法在其中加入training = False
参数。
training
属性而不是trainable
。 - Mrityutraining=False
应该是默认值。 - Andreytraining
参数通常用于使层在训练和评估中表现不同。据我所知,对于同一模型来说,它不应该有所不同。例如,通过添加Dropout()
和training=False
,实际上只是从模型中删除了该层。你能举个例子吗?什么情况下使用training=False
会有意义呢? - Andreytraining = False
的原因说明。 - Mrityu