Why is the accuracy for my Keras model always 0 when training?

Your model seems to correspond to a regression model for the following reasons:

  • You are using linear (the default one) as an activation function in the output layer (and relu in the layer before).

  • Your loss is loss='mean_squared_error'.

However, the metric that you use- metrics=['accuracy'] corresponds to a classification problem. If you want to do regression, remove metrics=['accuracy']. That is, use

model.compile(optimizer='adam',loss='mean_squared_error')

Here is a list of keras metrics for regression and classification (taken from this blog post):

Keras Regression Metrics

•Mean Squared Error: mean_squared_error, MSE or mse

•Mean Absolute Error: mean_absolute_error, MAE, mae

•Mean Absolute Percentage Error: mean_absolute_percentage_error, MAPE, mape

•Cosine Proximity: cosine_proximity, cosine

Keras Classification Metrics

•Binary Accuracy: binary_accuracy, acc

•Categorical Accuracy: categorical_accuracy, acc

•Sparse Categorical Accuracy: sparse_categorical_accuracy

•Top k Categorical Accuracy: top_k_categorical_accuracy (requires you specify a k parameter)

•Sparse Top k Categorical Accuracy: sparse_top_k_categorical_accuracy (requires you specify a k parameter)


Add following to get metrics:

   history = model.compile(optimizer='adam', loss='mean_squared_error', metrics=['mean_squared_error'])
   # OR
   history = model.compile(optimizer='adam', loss='mean_absolute_error', metrics=['mean_absolute_error'])
   history.history.keys()
   history.history