keras 验证损失不减反增是否正常

cig3rfwq  于 2022-12-27  发布在  其他
关注(0)|答案(2)|浏览(152)

训练后,我注意到-如下所示-验证损失在增加。这正常吗?还有,它的上面一个。

下面是我的代码:

# I omit data loading 

from sklearn.utils import shuffle
# shuffle input 
class_image, class_label = shuffle(class_image, class_label , random_state=0)

inputs = tf.keras.layers.Input(shape=(IMAGE_SIZE_X, IMAGE_SIZE_Y, 3), name="input_image")
x = keras.applications.resnet50.preprocess_input(inputs)
base_model  = tf.keras.applications.ResNet50(input_tensor=x,  weights=None, include_top=False, 
                                             input_shape=(IMAGE_SIZE_X, IMAGE_SIZE_Y, 3) )

x=base_model.output
x = tf.keras.layers.GlobalAveragePooling2D( name="avg_pool")(x) 
x = tf.keras.layers.Dense(2, activation='softmax', name="predications")(x)

model =  keras.models.Model(inputs=base_model.input,  outputs=x)

base_learning_rate =  0.0001 
loss = tf.keras.losses.SparseCategoricalCrossentropy()
model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=base_learning_rate),
             
            loss=loss,
         
              metrics=[   'accuracy'])

history = model.fit(x=class_image ,
                    y=class_label,
                    epochs=30 
                    ,batch_size=1
                    , validation_split=0.2
                  )



# evlaute 
import matplotlib.pyplot as plt
acc = history.history['accuracy']
val_acc = history.history['val_accuracy']
loss = history.history['loss']
val_loss = history.history['val_loss']
epochs = range(len(acc))
plt.plot(epochs, acc, 'r', label='Training accuracy')
plt.plot(epochs, val_acc, 'b', label='Validation accuracy')

plt.plot(epochs,loss, 'y', label='Training loss')
plt.plot(epochs, val_loss, 'g', label='Validation loss')

plt.title('Training and validation accuracy')
plt.legend(loc=0)
plt.figure()
lrpiutwd

lrpiutwd1#

当模型过拟合时经常会发生这种情况。您可能没有足够的训练数据来训练如此大的模型(ResNet 50非常大),导致模型只是记住您的数据集,而不是实际学习泛化。训练损失直接为零也证明了这一点。请尝试较小的模型或更多/变化的训练数据。

kknvjkwl

kknvjkwl2#

我在尝试微调序列分类的distilbert模型时遇到了类似的问题,对我有效的解决方案是将我的学习率从1 e-5降低到1 e-6。

相关问题