Mandallaz
Mandallaz

Reputation: 55

Difference between va_loss from fit and score from evaluate?

I am learning to use tf and Keras with a regression problem.

I have written this function for cross-validate differents models, in order to compare them.

def cv(X, y, model, n_splits=5, epochs=5, batch_size=1024,
       random_state=42, verbose=0):
    # kf = KFold(n_splits=n_splits, shuffle=True, random_state=random_state)
    kf = KFold(n_splits=n_splits, shuffle=False, random_state=random_state)
    histories = []
    for s in kf.split(X):
        X_train = X.iloc[s[0]].to_numpy()
        y_train = y.iloc[s[0]]['Target'].to_numpy()
        X_test = X.iloc[s[1]].to_numpy()
        y_test = y.iloc[s[1]]['Target'].to_numpy()

        h = model.fit(X_train, y_train,
                      epochs=epochs,
                      batch_size=batch_size,
                      validation_data=(X_test, y_test),
                      verbose=verbose)
        histories.append(h)

        score = model.evaluate(X_test, y_test, verbose=verbose)
        cvscores.append(score)
    return histories, cvscores

I found that the score from model.evaluate is always egals to the val_loss inside the history object from model.fit.

Is-it really the same thing ?

Thanks !

Upvotes: 0

Views: 51

Answers (1)

asti205
asti205

Reputation: 174

The val_loss is the same for model.fit and model.evaluate, because you use the same dataset X_test for both validation during training and testing after training.

In the documentation of model.fit you can see that this method also returns the validation loss.

If you would e.g. do a k-fold cross-validation, you would split the training data into actual training data and validation data to get an accuracy after each epoch. Because you split up the training data, you would still have another, untouched test dataset which you can use after the complete training. Then you would have a different val_loss between validation and test.

Upvotes: 1

Related Questions