[Python] Validation loss increases after 3 epochs but validation accuracy keeps increasing

Discussão em 'Python' iniciado por Stack, Dezembro 19, 2024.

  1. Stack

    Stack Membro Participativo

    Training and validation is healthy for 2 epochs but after 2-3 epochs the Val_loss keeps increasing while the Val_acc keeps increasing.

    I'm trying to train a CNN model to classify a given review to a single class of 1-5. Therefore, I considered it as a multi-class classification. I've divided the dataset to 3 sets - 70% training, 20% testing and 10% validation.

    Distribution of training data for 5 classes as follows.

    1 - 31613, 2 - 32527, 3 - 61044, 4 - 140005, 5 - 173023.


    Therefore I've added class weights as follows.

    {1: 5.47, 2: 5.32, 3: 2.83, 4: 1.26, 5: 1}


    Model structure is as below:

    input_layer = Input(shape=(max_length, ), dtype='int32')

    embedding = Embedding(vocab_size, 200, input_length=max_length)(input_layer)

    channel1 = Conv1D(filters=100, kernel_size=2, padding='valid', activation='relu', strides=1)(embedding)
    channel1 = GlobalMaxPooling1D()(channel1)

    channel2 = Conv1D(filters=100, kernel_size=3, padding='valid', activation='relu', strides=1)(embedding)
    channel2 = GlobalMaxPooling1D()(channel2)

    channel3 = Conv1D(filters=100, kernel_size=4, padding='valid', activation='relu', strides=1)(embedding)
    channel3 = GlobalMaxPooling1D()(channel3)

    merged = concatenate([channel1, channel2, channel3], axis=1)

    merged = Dense(256, activation='relu')(merged)
    merged = Dropout(0.6)(merged)
    merged = Dense(5)(merged)
    output = Activation('softmax')(merged)
    model = Model(inputs=[input_layer], outputs=[output])

    model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['categorical_accuracy'])

    model.fit(final_X_train, final_Y_train, epochs=5, batch_size=512, validation_data=(final_X_val, final_Y_val), callbacks=callback, class_weight=class_weights)



    1/5 - loss: 1.8733 - categorical_accuracy: 0.5892 - val_loss: 0.7749 - val_categorical_accuracy: 0.6558

    2/5 - loss: 1.3908 - categorical_accuracy: 0.6917 - val_loss: 0.7421 - val_categorical_accuracy: 0.6784

    3/5 - loss: 0.9587 - categorical_accuracy: 0.7734 - val_loss: 0.7595 - val_categorical_accuracy: 0.6947

    4/5 - loss: 0.6402 - categorical_accuracy: 0.8370 - val_loss: 0.7921 - val_categorical_accuracy: 0.7216

    5/5 - loss: 0.4520 - categorical_accuracy: 0.8814 - val_loss: 0.8556 - val_categorical_accuracy: 0.7331

    Final accuracy = 0.7328754744261703

    This seems to be an overfitting behavior, but I've tried adding dropout layers which didn't help. I've also tried increasing the data, which made the results even worst.

    Any suggestions?

    Continue reading...

Compartilhe esta Página