After you have trained a machine learning model, it is important to evaluate its performance. This will help you to understand how well the model is working and whether it is ready to be used in production.
There are a number of different metrics that you can use to evaluate a machine learning model. Some of the most common metrics include:
- Accuracy: This is the percentage of predictions that the model makes correctly.
- Precision: This is the percentage of positive predictions that are actually positive.
- Recall: This is the percentage of positive instances that are correctly predicted as positive.
- F1 score: This is a measure of accuracy, precision, and recall that takes into account all three metrics.
To evaluate your model, you can use the evaluate() method. This method takes two arguments: the data that you want to evaluate the model on, and the labels for that data.
Once you have trained a model, it is important to evaluate its performance on a held-out dataset. This will help you to ensure that your model is not overfitting to the training data and that it is able to generalize to new data.
Keras provides a number of functions for evaluating a model. The most basic function is evaluate, which takes the model and a dataset as input and returns the loss and metrics for the model on the dataset.
For example, the following code evaluates a model on the MNIST dataset:
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense
# Load the MNIST dataset
(x_train, y_train), (x_test, y_test) = mnist.load_data()
# Create a model
model = Sequential()
model.add(Dense(128, activation='relu', input_shape=(784,)))
model.add(Dense(10, activation='softmax'))
# Compile the model
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
# Train the model
model.fit(x_train, y_train, epochs=10)
# Evaluate the model
loss, accuracy = model.evaluate(x_test, y_test)
print('Loss:', loss)
print('Accuracy:', accuracy)