简体   繁体   English

验证损失和验证准确率都高于训练损失和acc和波动

[英]Validation loss and validation accuracy both are higher than training loss and acc and fluctuating

I am trying to train my model using transfer learning, for this I am using VGG16 model, stripped the top layers and froze first 2 layers for using imagenet initial weights.我正在尝试使用转移学习来训练我的模型,为此我使用 VGG16 模型,剥离顶层并冻结前 2 层以使用 imagenet 初始权重。 For fine tuning them I am using learning rate 0.0001, activation softmax, dropout 0.5, loss categorical crossentropy, optimizer SGD, classes 46.为了微调它们,我使用了学习率 0.0001、激活 softmax、dropout 0.5、损失分类交叉熵、优化器 SGD、46 级。

I am just unable to understand the behavior while training.我只是无法理解训练时的行为。 Train loss and acc both are fine (loss is decreasing, acc is increasing).训练损失和 acc 都很好(损失在减少,acc 在增加)。 Val loss is decreasing and acc is increasing as well, BUT they are always higher than the train loss and acc. Val loss 正在减少,acc 也在增加,但它们总是高于 train loss 和 acc。

Assuming its overfitting I made the model less complex, increased the dropout rate, added more samples to val data, but nothing seemed to work.假设它过度拟合,我使模型不那么复杂,增加了辍学率,向 val 数据添加了更多样本,但似乎没有任何效果。 I am a newbie so any kind of help is appreciated.我是一个新手,所以任何形式的帮助表示赞赏。

26137/26137 [==============================] - 7446s 285ms/step - loss: 1.1200 - accuracy: 0.3810 - val_loss: 3.1219 - val_accuracy: 0.4467
Epoch 2/50
26137/26137 [==============================] - 7435s 284ms/step - loss: 0.9944 - accuracy: 0.4353 - val_loss: 2.9348 - val_accuracy: 0.4694
Epoch 3/50
26137/26137 [==============================] - 7532s 288ms/step - loss: 0.9561 - accuracy: 0.4530 - val_loss: 1.6025 - val_accuracy: 0.4780
Epoch 4/50
26137/26137 [==============================] - 7436s 284ms/step - loss: 0.9343 - accuracy: 0.4631 - val_loss: 1.3032 - val_accuracy: 0.4860
Epoch 5/50
26137/26137 [==============================] - 7358s 282ms/step - loss: 0.9185 - accuracy: 0.4703 - val_loss: 1.4461 - val_accuracy: 0.4847
Epoch 6/50
26137/26137 [==============================] - 7396s 283ms/step - loss: 0.9083 - accuracy: 0.4748 - val_loss: 1.4093 - val_accuracy: 0.4908
Epoch 7/50
26137/26137 [==============================] - 7424s 284ms/step - loss: 0.8993 - accuracy: 0.4789 - val_loss: 1.4617 - val_accuracy: 0.4939
Epoch 8/50
26137/26137 [==============================] - 7433s 284ms/step - loss: 0.8925 - accuracy: 0.4822 - val_loss: 1.4257 - val_accuracy: 0.4978
Epoch 9/50
26137/26137 [==============================] - 7445s 285ms/step - loss: 0.8868 - accuracy: 0.4851 - val_loss: 1.5568 - val_accuracy: 0.4953
Epoch 10/50
26137/26137 [==============================] - 7387s 283ms/step - loss: 0.8816 - accuracy: 0.4874 - val_loss: 1.4534 - val_accuracy: 0.4970
Epoch 11/50
26137/26137 [==============================] - 7374s 282ms/step - loss: 0.8779 - accuracy: 0.4894 - val_loss: 1.4605 - val_accuracy: 0.4912
Epoch 12/50
26137/26137 [==============================] - 7411s 284ms/step - loss: 0.8733 - accuracy: 0.4915 - val_loss: 1.4694 - val_accuracy: 0.5030

Yes, you are facing over-fitting issue.是的,您正面临过度拟合的问题。 To mitigate, you can try to implement below steps为了缓解,您可以尝试实施以下步骤

1. Shuffle the Data , by using shuffle=True in VGG16_model.fit. 1. ShuffleData ,通过使用shuffle=True在VGG16_model.fit。 Code is shown below:代码如下所示:

history = VGG16_model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, verbose=1,
                   validation_data=(x_validation, y_validation), shuffle = True)

2.Use Early Stopping . 2.使用Early Stopping Code is shown below代码如下所示

callback = tf.keras.callbacks.EarlyStopping(monitor='val_loss', patience=15)

3.Use Regularization. 3.使用正则化。 Code for Regularization is shown below (You can try l1 Regularization or l1_l2 Regularization as well):正则化代码如下所示(您也可以尝试 l1 正则化或 l1_l2 正则化):

from tensorflow.keras.regularizers import l2

Regularizer = l2(0.001)

VGG16_model.add(Conv2D(96,11, 11, input_shape = (227,227,3),strides=(4,4), padding='valid', activation='relu', data_format='channels_last', 
                    activity_regularizer=Regularizer, kernel_regularizer=Regularizer))

VGG16_model.add(Dense(units = 2, activation = 'sigmoid', 
                    activity_regularizer=Regularizer, kernel_regularizer=Regularizer))

4.You can try using BatchNormalization . 4.您可以尝试使用BatchNormalization

5.Perform Image Data Augmentation using ImageDataGenerator . 5.使用ImageDataGenerator图像数据增强。 Refer this link for more info about that.有关更多信息,请参阅此链接

6.If the Pixels are not Normalized , Dividing the Pixel Values with 255 also helps 6.如果像素未Normalized ,将像素值除以255也有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM