I am trying to understand what is the accuracy "acc" shown in the keras progress bar at the end of epoch:
13/13 [==============================] - 0s 76us/step - loss: 0.7100 - acc: 0.4615
At the end of an epoch it should be the accuracy of the model predictions of all training samples. However when the model is evaluated on the same training samples, the actual accuracy can be very different.
Below is adapted example of MLP for binary classification from keras webpage. A simple sequential neural net is doing binary classification of randomly generated numbers. The batch size is the same as the number of training examples (13), so that every epoch contain only one step. Since loss is set to binary_crossentropy, for the accuracy calculation is used binary_accuracy defined in metrics.py. MyEval class defines callback, which is called at the end of each epoch. It uses two ways of calculating the accuracy of the training data a) model evaluate and b) model predict to get prediction and then almost the same code as is used in keras binary_accuracy function. These two accuracies are consistent, but most of the time are different to the one in the progress bar. Why they are different? Is is possible to calculate the same accuracy as is in the progress bar? Or have I made a mistake in my assumptions?
import numpy as np from keras.models import Sequential from keras.layers import Dense, Dropout from keras import callbacks np.random.seed(1) # fix random seed for reproducibility # Generate dummy data x_train = np.random.random((13, 20)) y_train = np.random.randint(2, size=(13, 1)) model = Sequential() model.add(Dense(64, input_dim=20, activation='relu')) model.add(Dropout(0.5)) model.add(Dense(64, activation='relu')) model.add(Dropout(0.5)) model.add(Dense(1, activation='sigmoid')) model.compile(loss='binary_crossentropy', optimizer='rmsprop', metrics=['accuracy']) class MyEval(callbacks.Callback): def on_epoch_end(self, epoch, logs=None): my_accuracy_1 = self.model.evaluate(x_train, y_train, verbose=0)[1] y_pred = self.model.predict(x_train) my_accuracy_2 = np.mean(np.equal(y_train, np.round(y_pred))) print("my accuracy 1: {}".format(my_accuracy_1)) print("my accuracy 2: {}".format(my_accuracy_2)) my_eval = MyEval() model.fit(x_train, y_train, epochs=5, batch_size=13, callbacks=[my_eval], shuffle=False) The output of the above code:
13/13 [==============================] - 0s 25ms/step - loss: 0.7303 - acc: 0.5385 my accuracy 1: 0.5384615659713745 my accuracy 2: 0.5384615384615384 Epoch 2/5 13/13 [==============================] - 0s 95us/step - loss: 0.7412 - acc: 0.4615 my accuracy 1: 0.9230769276618958 my accuracy 2: 0.9230769230769231 Epoch 3/5 13/13 [==============================] - 0s 77us/step - loss: 0.7324 - acc: 0.3846 my accuracy 1: 0.9230769276618958 my accuracy 2: 0.9230769230769231 Epoch 4/5 13/13 [==============================] - 0s 72us/step - loss: 0.6543 - acc: 0.5385 my accuracy 1: 0.9230769276618958 my accuracy 2: 0.9230769230769231 Epoch 5/5 13/13 [==============================] - 0s 76us/step - loss: 0.6459 - acc: 0.6923 my accuracy 1: 0.8461538553237915 my accuracy 2: 0.8461538461538461 using: Python 3.5.2, tensorflow-gpu==1.14.0 Keras==2.2.4 numpy==1.15.2