[Python] CNN-1D for time-series data returns strange accuracy

Discussão em 'Python' iniciado por Stack, Março 7, 2025.

  1. Stack

    Stack Membro Participativo

    I am using keras to train a 1D CNN with time-series as input data to perform a binary classification. The model part of the code is the following:

    modelo = Sequential()
    modelo.add(Conv1D(filters=32, kernel_size=5, activation='relu', input_shape=(max_size, 1)))
    modelo.add(BatchNormalization())
    modelo.add(Conv1D(filters=32, kernel_size=5, activation='relu'))
    modelo.add(BatchNormalization())
    modelo.add(Dropout(0.3))
    modelo.add(Flatten())
    modelo.add(Dense(64, activation='relu'))
    modelo.add(Dropout(0.3))
    modelo.add(Dense(1, activation='sigmoid'))

    modelo.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])

    early_stopping = callbacks.EarlyStopping(monitor='loss', patience=1, restore_best_weights=True)

    modelo.fit(X, etiquetas, epochs=100, batch_size=5, callbacks=[early_stopping])


    What I don't understand is that, when the fit function is executed, 9 epochs are computed, all of them with an accuracy higher than 80%. Then, due to early_stopping, training stops at the end of epoch 9. The resulting accuracy is only 54%. Why is there a so high discrepancy between the training accuracies during the epochs and the accuracy printed at the end? I would expect to get a final accuracy around the average of the training epochs accuracy. At least that is the case when I use an LSTM or an MLP.

    Fortunately, I get very good results with the LSTM and MLP. This means that I don't need the CNN to perform good. However, I would like to know why the CNN does not perform so good or if there is something wrong.

    Continue reading...

Compartilhe esta Página