[英]fitting model several times in keras
I am using model.fit()
several times, each time is responsible for training a block of layers where other layers are freezed我多次使用
model.fit()
,每次都负责训练一个其他层被冻结的层块
# create the base pre-trained model
base_model = efn.EfficientNetB0(input_tensor=input_tensor,weights='imagenet', include_top=False)
# add a global spatial average pooling layer
x = base_model.output
x = GlobalAveragePooling2D()(x)
# add a fully-connected layer
x = Dense(x.shape[1], activation='relu',name='first_dense')(x)
x=Dropout(0.5)(x)
x = Dense(x.shape[1], activation='relu',name='output')(x)
x=Dropout(0.5)(x)
no_classes=10
predictions = Dense(no_classes, activation='softmax')(x)
# this is the model we will train
model = Model(inputs=base_model.input, outputs=predictions)
# first: train only the top layers (which were randomly initialized)
# i.e. freeze all convolutional layers
for layer in base_model.layers:
layer.trainable = False
#FIRST COMPILE
model.compile(optimizer='Adam', loss=loss_function,
metrics=['accuracy'])
#FIRST FIT
model.fit(features[train], labels[train],
batch_size=batch_size,
epochs=top_epoch,
verbose=verbosity,
validation_split=validation_split)
# Generate generalization metrics
scores = model.evaluate(features[test], labels[test], verbose=1)
print(scores)
#Let all layers be trainable
for layer in model.layers:
layer.trainable = True
from tensorflow.keras.optimizers import SGD
#FIRST COMPILE
model.compile(optimizer=SGD(lr=0.0001, momentum=0.9), loss=loss_function,
metrics=['accuracy'])
#SECOND FIT
model.fit(features[train], labels[train],
batch_size=batch_size,
epochs=no_epochs,
verbose=verbosity,
validation_split=validation_split)
What is weird is that in the second fit , accuracy resulted from first epoch is much lower that the accuracy of the last epoch of the first fit .奇怪的是,在第二次拟合中,第一个 epoch的准确度远低于第一次拟合的最后一个 epoch的准确度。
Epoch 40/40 6286/6286 [==============================] - 14s 2ms/sample - loss: 0.2370 - accuracy: 0.9211 - val_loss: 1.3579 - val_accuracy: 0.6762 874/874 [==============================] - 2s 2ms/sample - loss: 0.4122 - accuracy: 0.8764 Epoch 40/40 6286/6286 [==============================] - 14s 2ms/sample - 损失:0.2370 - 准确度: 0.9211 - val_loss: 1.3579 - val_accuracy: 0.6762 874/874 [==============================] - 2s 2ms/sample - 损失:0.4122 - 准确度:0.8764
Train on 6286 samples, validate on 1572 samples Epoch 1/40 6286/6286 [==============================] - 60s 9ms/sample - loss: 5.9343 - accuracy: 0.5655 - val_loss: 2.4981 - val_accuracy: 0.5115在 6286 个样本上训练,在 1572 个样本上验证 Epoch 1/40 6286/6286 [==============================] - 60s 9 毫秒/样本 - 损失:5.9343 - 准确度:0.5655 - val_loss:2.4981 - val_accuracy:0.5115
I think the weights of the second fit are not taken from the first fit我认为第二次拟合的权重不是取自第一次拟合
Thanks in advance!!!提前致谢!!!
I think this is the result of using a different optimizer.我认为这是使用不同优化器的结果。 You used Adam in the first fit and SGD in the second fit.
您在第一次拟合中使用了 Adam,在第二次拟合中使用了 SGD。 Try using Adam in the second fit and see if it works correctly
尝试在第二次拟合中使用 Adam,看看它是否工作正常
我通过删除第二个编译器解决了这个问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.