Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keras: How come 'accuracy' is higher than 'val_acc'?

I've compiled a model with metrics=['accuracy'] and the value I get is always higher than validation accuracy val_acc. For instance:

Epoch 19/20
53/53 [==============================] - 280s - loss: 0.3931 - acc: 0.8238 - val_loss: 0.5002 - val_acc: 0.7757
Epoch 20/20
53/53 [==============================] - 278s - loss: 0.3957 - acc: 0.8255 - val_loss: 0.5009 - val_acc: 0.7754
accuracy: 0.790697674418604

Anyone know how the two values are computed differently?

UPDATE

I'm on Keras 2.0.8. By 'accuracy', I'm referring to the last line accuracy: 0.790697674418604. I didn't provide it with a testing set, so I wonder what it tests on.

like image 346
John M. Avatar asked Jul 14 '18 04:07

John M.


People also ask

Why does the validation accuracy start higher than the training accuracy?

The training loss is higher because you've made it artificially harder for the network to give the right answers. However, during validation all of the units are available, so the network has its full computational power - and thus it might perform better than in training.

What if test accuracy is higher than training?

Test accuracy should not be higher than train since the model is optimized for the latter. Ways in which this behavior might happen: you did not use the same source dataset for test. You should do a proper train/test split in which both of them have the same underlying distribution.

Why my training accuracy is lower than validation?

If your model's accuracy on your testing data is lower than your training or validation accuracy, it usually indicates that there are meaningful differences between the kind of data you trained the model on and the testing data you're providing for evaluation.

What is the difference between accuracy and Val_accuracy?

Context in source publicationval_accuracy indicates the accuracy of the predictions of a randomly separated validation set after each training period. However, the general accuracy continued to increase (see Figure 2).

How do you get a higher validation accuracy?

One of the easiest ways to increase validation accuracy is to add more data. This is especially useful if you don't have many training instances. If you're working on image recognition models, you may consider increasing the diversity of your available dataset by employing data augmentation.


2 Answers

During training, the samples are again split into 2 internal subsets. One which is used for actual training and other which is used for validation after each epoch. The ratio of split can be controlled by the parameter 'validation_split' like below (Example from Keras)

h = model.fit(X_train, Y_train, batch_size=200, epochs=50, verbose=2, validation_split=0.2)

Now, coming to the log, 'acc' refers to accuracy of what was trained against. 'val_acc' refers to validation set. Note that val_acc refers to a set of samples that was not shown to the network during training and hence refers to how much your model works in general for cases outside the training set.

It is common for validation accuracy to be lower than accuracy. But ideally, you should strive to keep those values at the same level. If validation accuracy is much lower than accuracy, you are certainly over fitting (like in the example above) - where the accuracy is 84 and validation accuracy is 77.

Edit: Regarding 'accuracy' in the last line of the log, that is the accuracy of the network after all epochs run against the test data set. This is usually more closer to 'val_acc' than accuracy (like in the case above it is 79). This just means that the samples in test data alinged more closely than samples in validation data in the last epoch run (remember both these sets are not used for training)

In any case, I think you should tweak to make sure that 'acc' and 'val_acc' and final 'accuracy' are more closer to each other

like image 152
user007 Avatar answered Oct 29 '22 18:10

user007


The validation accuracy is computed on a dataset that is separate from your training set. The validation accuracy can be lower for multiple reasons - I'd look at imbalanced classes, overfitting, not enough data if there is a large discrepancy between the training set. The accuracy you cite is likely the training accuracy.As to why training accuracy is being calculated at all - you feed your input aka features into the network and initialize the NN with random weights. You let the NN figure out/guess the label and then compare it with the actual label. The error.i.e. the difference between the guess and the actual label is computed and backpropagate the error. You do this for all samples/batches. Keras handles all of this automatically and outputs metrics for how the training is proceeding - usually loss and the accuracy as the samples/batches are added up. That's the accuracy metric you see.

like image 30
Varsha Venkatesh Avatar answered Oct 29 '22 20:10

Varsha Venkatesh