Search code examples
pythontensorflowkerasdeep-learningrecurrent-neural-network

Why does this neural network have zero accuracy and very low loss?


I have network :

Tensor("input_1:0", shape=(?, 5, 1), dtype=float32)
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_1 (InputLayer)         (None, 5, 1)              0         
_________________________________________________________________
bidirectional_1 (Bidirection (None, 5, 64)             2176      
_________________________________________________________________
activation_1 (Activation)    (None, 5, 64)             0         
_________________________________________________________________
bidirectional_2 (Bidirection (None, 5, 128)            16512     
_________________________________________________________________
activation_2 (Activation)    (None, 5, 128)            0         
_________________________________________________________________
bidirectional_3 (Bidirection (None, 1024)              656384    
_________________________________________________________________
activation_3 (Activation)    (None, 1024)              0         
_________________________________________________________________
dense_1 (Dense)              (None, 1)                 1025      
_________________________________________________________________
p_re_lu_1 (PReLU)            (None, 1)                 1         
=================================================================
Total params: 676,098
Trainable params: 676,098
Non-trainable params: 0
_________________________________________________________________
None
Train on 27496 samples, validate on 6875 samples

I fit and compile it by:

model.compile(loss='mse',optimizer=Adamx,metrics=['accuracy'])
model.fit(x_train,y_train,batch_size=100,epochs=10,validation_data=(x_test,y_test),verbose=2)

When I run it and also evaluate it on unseen data,it returns 0.0 Accuracy with very low loss. I can't figure out what's the problem.

Epoch 10/10
 - 29s - loss: 1.6972e-04 - acc: 0.0000e+00 - val_loss: 1.7280e-04 - val_acc: 0.0000e+00

Solution

  • What you are getting is expected. Your model is working correctly, it is your metrics of measure that is incorrect. The aim of the optimization function is to minimize loss, not to increase accuracy.

    Since you are using PRelu as the activation function of your last layer, you always get float output from the network. Comparing these float output with actual label for measure of accuracy doesn't seem the right option. Since the outputs and labels are continuous random variable the joint probability for specific value will be zero. Therefore, even if the model predicts values very close to the true label value the model accuracy still will be zero unless the model predicts exactly the same value as true label - which is improbable.

    e.g if y_true is 1.0 and the model predicts 0.99999 still this value does not add value to accuracy of the model since 1.0 != 0.99999

    Update The choice of metrics function depends on the type of problem. Keras also provides functionality for implementing custom metrics. Assuming the problem on question is linear regression and two values are equal if difference between the two values is less than 0.01, the custom loss metrics can be defined as:-

    import keras.backend as K
    import tensorflow as tf
    accepted_diff = 0.01
    def linear_regression_equality(y_true, y_pred):
        diff = K.abs(y_true-y_pred)
        return K.mean(K.cast(diff < accepted_diff, tf.float32))
    

    Now you can use this metrics for your model

    model.compile(loss='mse',optimizer=Adamx,metrics=[linear_regression_equality])