Search code examples
tensorflowkerastime-series

Discrepancies between the plot and the historical data object values of loss and valid_loss


I'm running an encoder-decoder LSTM model using kears. the values printed in the console through the model trainig illustated in this image:

console and the plot is shown in this figure :

plot figure I can see the values in the console don't match the plot. I'm confused by this. My code is:

    es = EarlyStopping(monitor='val_loss', mode='min', verbose=1, patience=CONFIG['patience'])
    model = Sequential()
    model.add(LSTM(200, activation='relu', input_shape=(n_timesteps, n_features)))
    model.add(RepeatVector(n_outputs))
    model.add(LSTM(200, activation='relu', return_sequences=True))
    model.add(TimeDistributed(Dense(100, activation='relu')))
    model.add(TimeDistributed(Dense(1)))
    model.compile(loss='mse', optimizer='adam')
    history= model.fit(X_train,y_train, epochs=CONFIG['epoch'], batch_size=batch_size,   
                   validation_data=(X_valid, y_valid), shuffle=False,
                   callbacks=[TerminateOnNaN(), es])
    plt.plot(history.history['loss'])
    plt.plot(history.history['val_loss'])
    plt.title('model loss')
    plt.ylabel('loss')
    plt.xlabel('epoch')
    plt.legend(['loss', 'val_loss'], loc='upper left')
    plt.show()

Solution

  • Keras' epoch indexing is 1-based (and not 0-based like the plot). Therefore, the reported loss values are likely off by 1 epoch compared to the console.

    However, besides that, there doesn't seem to be much of a discrepancy. There seems to be a small spike at x=38 (which is consistent with Epoch 39 in the log) but it is hard to judge from the plot. You should print out the history instead of plotting it to get a better understanding of what is happening.