0
$\begingroup$

Could someone explain why the loss returned is different than the loss printed during the evaluation?

They are the same in the Tensorflow documentation https://www.tensorflow.org/guide/keras/train_and_evaluate

Code:

results = model.evaluate(test_data, test_target, verbose=2)
print("test loss, test acc:", results)

Output:

45/1 - 0s - loss: 1.2592 - mae: 0.7602
test loss, test acc: [1.05335361427731, 0.76020277]
$\endgroup$
0
$\begingroup$

The model.evaluate function predicts the output for the given input and then computes the metrics function specified in the model.compile.Based on y_true and y_pred and returns the computed metric value as the output.

You can use model.metrics_names property of your model to find out what each of those values corresponds to.

$\endgroup$
3
  • $\begingroup$ I have the following metrics ['loss', 'mae'] but it doesnt explain why the loss printed in verbose is different than the loss in the returned object $\endgroup$ – Marc Aug 25 '20 at 12:38
  • $\begingroup$ check this link $\endgroup$ – Shiv Aug 25 '20 at 17:01
  • $\begingroup$ I don't think it's related, my issue is not the different between evaluate and predict but between the loss printed with verbose by evaluate and the loss returned by evaluate $\endgroup$ – Marc Aug 27 '20 at 11:45
0
$\begingroup$

I had the same problem and I couldn't understand why they were different. The problem is that the ProgbarLogger prints an average of the values (loss, regularization loss, other metrics), which are the values shown in the stdout like this:

45/1 - 0s - loss: 1.2592 - mae: 0.7602

While the values inside the History for the fit, or the scalar or list of scalars for evaluate, are the real values computed on your model. This can be changed with the stateful_metrics parameter of the ProgbarLogger, which will return the real values and not the averaged ones.

In your example could be done like this for loss:

results = model.evaluate(test_data,
                         test_target,
                         verbose=2,
                         callbacks=[
                             tf.keras.callbacks.ProgbarLogger(
                                 count_mode="steps",
                                 stateful_metrics=["loss"]
                             )
                         ])
New contributor
Giacomo is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.