In machine learning, We have to first train the model and then we have to check that if the model is working properly or not. Does the model is efficient or not to predict further result.
We can evaluate the model by various metrics like accuracy, f1 score, etc.
So this recipe is a short example of how to evaluate a keras model?
import pandas as pd import numpy as np from keras.datasets import mnist from sklearn.model_selection import train_test_split from keras.models import Sequential from keras.layers import Dense from keras.layers import Dropout
We have imported pandas, numpy, mnist(which is the dataset), train_test_split, Sequential, Dense and Dropout. We will use these later in the recipe.
Here we have used the inbuilt mnist dataset and stored the train data in X_train and y_train. We have used X_test and y_test to store the test data.
(X_train, y_train), (X_test, y_test) = mnist.load_data()
We have created an object model for sequential model. We can use two args i.e layers and name.
model = Sequential()
Now, We are adding the layers by using 'add'. We can specify the type of layer, activation function to be used and many other things while adding the layer.
Here we have added four layers which will be connected one after other.
We can compile a model by using compile attribute. Let us first look at its parameters before using it.
model.compile(optimizer='Adam', loss='categorical_crossentropy', metrics=['accuracy'])
We can fit a model on the data we have and can use the model after that. Here we are using the data which we have split i.e the training data for fitting the model.
While fitting we can pass various parameters like batch_size, epochs, verbose, validation_data and so on.
After fitting a model we want to evaluate the model. Here we are using model.evaluate to evaluate the model and it will give us the loss and the accuracy. Here we have also printed the score.
score = model.evaluate(X_test, y_test, verbose=0)
print('Test loss:', score)
print('Test accuracy:', score)
As an output we get:
Epoch 1/2 469/469 [==============================] - 6s 14ms/step - loss: 0.3202 - accuracy: 0.9022 - val_loss: 0.1265 - val_accuracy: 0.9610 Epoch 2/2 469/469 [==============================] - 6s 14ms/step - loss: 0.1542 - accuracy: 0.9541 - val_loss: 0.0916 - val_accuracy: 0.9718 Test loss: 0.09163221716880798 Test accuracy: 0.9718000292778015