Keras NN shows 0 accuracy

I’m using Keras through R, and at every epoch it says it has 0 accuracy (accuracy: 0.0000e+00) even through the mae is decreasing. A minimal example of my code is below

x = rnorm(1000)+10
y = x*2

model <- keras_model_sequential() %>%
    layer_dense(units = 50, activation = 'relu', input_shape = 1) %>%
    layer_dense(units = 50, activation = 'relu') %>%
    layer_dense(units = 50, activation = 'relu') %>% 
    layer_dense(units = 1)
  
model %>% compile(optimizer = 'rmsprop', 
                    loss = 'mse', 
                    metrics = c('accuracy', 'mae'))

model %>% summary()

model %>% fit(
  x = x, 
  y = y,
  epochs = 50, 
  batch_size = 32, 
  validation_split = 0.2
)

and when training, I get

Epoch 48/50
25/25 [==============================] - 0s 7ms/step - loss: 5.9464 - accuracy: 0.0000e+00 - mae: 1.9209 - val_loss: 6.8600 - val_accuracy: 0.0000e+00 - val_mae: 2.0744
Epoch 49/50
25/25 [==============================] - 0s 7ms/step - loss: 5.9864 - accuracy: 0.0000e+00 - mae: 1.9232 - val_loss: 7.0626 - val_accuracy: 0.0000e+00 - val_mae: 2.1130
Epoch 50/50
25/25 [==============================] - 0s 7ms/step - loss: 5.9315 - accuracy: 0.0000e+00 - mae: 1.9250 - val_loss: 6.9852 - val_accuracy: 0.0000e+00 - val_mae: 2.1013

is this something to do with it being a single neuron input and single neuron output NN? I have no idea what’s going on. Any help would be great, thanks.

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')

So, if I remove metrics=['accuracy'], the model no longer prints the accuracy at each epoch…

So are you saying that accuracy is not meaningful for regression models?

Thanks @Bhack, that’s helpful.

1 Like