is the accuracy printed by keras model.fit function related to validation set or training set?

Question:

I am training a CNN model using tf.Keras, I splatted the training set into training and validation set, I want to visualize the accuracy on the validation set.

Here is the code, so please tell me whether the printed accuracy is related to training or validation set?

model.compile(optimizer=opt, loss='categorical_crossentropy', metrics=['accuracy'])
model.fit( x= X_train.reshape(X_train.shape[0],280,252,1),
           y= train_Y_one_hot,           
           epochs=20,
           batch_size=64,
           validation_data=(X_val.reshape(X_val.shape[0],280,252,1),val_Y_one_hot),
           verbose=1)

output:
Train on 103658 samples, validate on 25915 samples
Epoch 1/20
28288/103658 [=======>………………….] – ETA: 40:01 – loss: 0.5309 – accuracy: 0.9063

Asked By: Yacine

||

Answers:

It is the training loss and accuracy. You’ll get results for validation set at the end of epoch.

Answered By: AlexM4

If you want to visualize accuracy as a plot, you can get the list of validation accuracy and loss for each epoch as follows (I ran only 5 epochs, so i get 5 values for each val_accuracy and val_loss)

model.fit(x_train, y_train, epochs=5,validation_data=(x_test,y_test))
model.evaluate(x_test, y_test)


# access validation accuracy for each epoch
acc = model.history.history['val_accuracy']
print(acc) # [0.9573, 0.9696, 0.9754, 0.9762, 0.9784]

# access validation loss for each epoch
loss_val = model.history.history['val_loss']
print(loss_val) # [0.13892182569280268, 0.10223265058882534, 0.08262962606661022, 0.08026109958332964, 0.07378015918848105]

The accuracy of the model solely depends on the training data, pre-processing done in the training data and the type of classifier and its’s parameters.

Answered By: Dreamer12

Accessing training accuracy and validation accuracy

accuracy = model.history.history["accuracy"]
val_accuracy = model.history.history["val_accuracy"]

Accessing training loss and validation loss

loss = model.history.history["loss"]
val_loss = model.history.history["val_loss"]

Plotting training accuracy and validation accuracy

plt.plot(accuracy, label="accuracy")
plt.plot(val_accuracy, label="validation accuracy")
plt.ylabel("Accuracy")
plt.xlabel("Epoch")
plt.legend()
plt.show()

training accuracy and validation accuracy graph

Plotting training loss and validation loss

plt.plot(loss, label="loss")
plt.plot(val_loss, label="validation loss")
plt.ylabel("Loss")
plt.xlabel("Epoch")
plt.legend()
plt.show()

training loss and validation loss graph

Answered By: devp