Python 在keras中多次拟合模型

Python 在keras中多次拟合模型,python,tensorflow,keras,deep-learning,model,Python,Tensorflow,Keras,Deep Learning,Model,我使用了几次model.fit(),每次都负责训练一个层块,其他层被冻结 代码 奇怪的是,在第二次拟合中,第一个历元产生的精度远远低于第一次拟合的最后一个历元的精度 结果 纪元40/40 6286/6286[============================================================-14s 2ms/样本-损耗:0.2370-精度:0.9211-val_损耗:1.3579-val_精度:0.6762 874/874[==================

我使用了几次
model.fit()
,每次都负责训练一个层块,其他层被冻结

代码 奇怪的是,在第二次拟合中,第一个历元产生的精度远远低于第一次拟合的最后一个历元的精度

结果 纪元40/40 6286/6286[============================================================-14s 2ms/样本-损耗:0.2370-精度:0.9211-val_损耗:1.3579-val_精度:0.6762 874/874[=======================================]2秒/样本-损耗:0.4122-精度:0.8764

培训6286个样本,验证1572个样本 纪元1/40 6286/6286[================================================-60s 9ms/样本-损失:5.9343-准确度:0.5655-val_损失:2.4981-val_准确度:0.5115


我认为第二次试穿的权重不是取自第一次试穿的


提前感谢

我认为这是使用不同优化器的结果。你在第一次试穿中使用了亚当,在第二次试穿中使用了SGD。尝试在第二次拟合中使用Adam,看看它是否正确运行

我通过删除第二个编译器解决了这个问题。

不,它没有:\Hmm到我的知识模型。编译不应更改权重尝试不编译第二次,然后只做model.fit第二次以防万一。看看这是否有效如果你真的想使用不同的优化器,那么你必须编译,所以在第一次拟合运行模型结束时,保存权重。然后编译。编译run model.load_权重后,再进行第二次拟合。
 # 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)