I am using a loss function:
meanAbsoluteError = tf.keras.losses.MeanAbsoluteError()
And a model:
model.compile(optimizer=tf.keras.optimizers.Adam(0.001), loss=meanAbsoluteError)
Now I train it and get the history:
history = model.fit(train_features, train_labels, epochs=100, validation_data=(test_features, test_labels))
I can check how low the loss has become at the end:
print(history.history["val_loss"][-1])
Now I try to compute the loss for the test data manually, using the same data as passed in validation_data
when training:
print(meanAbsoluteError(model(test_features), test_labels).numpy())
But this gives me a completely different number! In my opinion this should equal the loss I got from the previous expression. But that's not the case.
Why the two numbers are not equal, and which one is the correct loss?
I checked how to call the loss function here: https://www.tensorflow.org/api_docs/python/tf/keras/losses/Loss#call
I edited this to use history.history["val_loss"]
instead of history.history["loss"]
as suggested, but it doesn't fix it.
CodePudding user response:
Your problem seems to be caused by the batch size.
The loss in history['val_loss']
is calculated on the batch. If your test_features
has a different size than your training batch, it will give you a larger loss.
You should try:
print(meanAbsoluteError(model(test_features[:batch_size]), test_labels[:batch_size]).numpy())