9951 explained code solutions for 126 technologies


python-kerasHow do I evaluate a model in Python using Keras?


Evaluating a model in Python using Keras is relatively straightforward. The following example code block demonstrates how to evaluate a model using the model.evaluate() function:

# evaluate the model
scores = model.evaluate(X_test, y_test)
print("\n%s: %.2f%%" % (model.metrics_names[1], scores[1]*100))

This code will output the accuracy score of the model on the test set:

acc: 98.00%

The code consists of the following parts:

  1. model.evaluate(): This is a Keras function that evaluates the model on a given dataset. It takes two arguments: the test data (X_test) and the test labels (y_test).

  2. model.metrics_names[1]: This is a list of the metrics that the model is evaluated on, with the first element being the name of the metric (in this case, acc for accuracy).

  3. scores[1]: This is the score of the model on the given metric (in this case, accuracy).

  4. print(): This prints the metric name and score to the console.

More information on evaluating models in Keras can be found in the Keras Documentation.

Edit this code on GitHub