How to visualize the training process in Keras
Hey there my fellow machine learning enthusiasts, today let’s talk about how to visualize the training process in Keras with corresponding Python code example.
We all know that Keras is a machine learning framework.
It has a great number of built-in functions to facilitate programming.
Now, let us talk about the visualization of the training process.
But, why do we need it anyway?
Why do we need to Visualize the training process?
We are fond of visualization because we want to see how well our model works, by tracing the error or accuracy of the model we can tune the parameters and the hyperparameters.
With it we can:
- If Underfitting or Overfitting:
- -> Well for a model we want to have the best fit.
- -> A model is underfitting then it will have a high training error.
- -> A model is overfitting then it won’t perform as good on the validation set as it did on the training set.
- ->Hence we want to evaluate a relation among various errors to find out the way to get the best fit.
- -> And for that we need visualization of data.
- To tune the Parameters:
- -> Well, for a model with overfitting or underfitting we need to tune our parameters accordingly.
- -> We need to adjust the values of parameters like weights, number of layers, number of nodes, etc.
- -> We need to adjust the values of the hyperparameters like learning rate, the number of epochs, etc.
- -> By tuning them we can reach the best fit for the model.
- -> And to know how much to tune we need to visualize the model accuracy.
Ok, we get it, but how do we do this:
How do we Visualize a model?
We basically plot the graph among the loss and accuracy of the training and validation set to visualize a model correctly.
Well for this we require Matplotlib library to plot the graphs.
Moreover, for recording the loss or accuracy Keras has a built-in function.
This function automatically records the loss or accuracy for the respective epochs.
It is called the “History” function.
Now, let us see how it works…
We first use the history function and then we use the recorded data to plot the graphs.
model = keras.Sequential([ keras.layers.Flatten(input_shape=(3,)), keras.layers.Dense(4, activation=tf.nn.relu), keras.layers.Dense(6, activation=tf.nn.relu), keras.layers.Dense(2, activation=tf.nn.relu), keras.layers.Dense(8, activation=tf.nn.relu), keras.layers.Dense(1, activation=tf.nn.sigmoid), ]) model.compile(optimizer='adam', loss='mse', metrics=['accuracy']) history = model.fit(X_train, y_train, epochs=20, batch_size=1, validation_data=(X_val, y_val))
Now, we plot the train vs validation loss graph.
plt.plot(history.history['loss'], 'r', label='Training loss') plt.plot(history.history['val_loss'], 'g', label='Validation loss') plt.title('Training VS Validation loss') plt.xlabel('No. of Epochs') plt.ylabel('Loss') plt.legend() plt.show()
Let’s see how it looks:
Now, let’s see how we get the accuracy for train and validations sets:
plt.plot(history.history['acc'], 'r', label='Training accuracy') plt.plot(history.history['val_acc'], 'g', label='Validation accuracy') plt.title('Training Vs Validation Accuracy') plt.xlabel('No. of Epochs') plt.ylabel('Accuracy') plt.legend() plt.show()
Let’s see how it looks:
And there you have it, “How to visualize the training process in Keras“.
I hope you loved the read.
Thanx for reading.