My model doesn't seem to work, as accuracy and loss are 0

会有一股神秘感。 提交于 2020-05-17 06:42:26

问题


I tried to design an LSTM network using keras but the accuracy is 0.00 while the loss value is 0.05 the code which I wrote is below.

model = tf.keras.models.Sequential()
model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(128, activation = tf.nn.relu))
model.add(tf.keras.layers.Dense(128, activation = tf.nn.relu))
model.add(tf.keras.layers.Dense(1, activation = tf.nn.relu))



def percentage_difference(y_true, y_pred):
    return K.mean(abs(y_pred/y_true - 1) * 100)



model.compile(optimizer='sgd', 
             loss='mse',
             metrics = ['accuracy', percentage_difference])

model.fit(x_train, y_train.values, epochs = 10)


my input train and test data set have been imported using the pandas' library. The number of features is 5 and the number of target is 1. All endeavors will be appreciated.


回答1:


From what I see is that you're using a neural network applied for a regression problem.

Regression is the task of predicting continuous values by learning from various independent features.

So, in the regression problem we don't have metrics like accuracy because this is for classification branch of the supervised learning.

The equivalent of accuracy for regression could be coefficient of determination or R^2 Score.

from keras import backend as K

def coeff_determination(y_true, y_pred):
    SS_res =  K.sum(K.square( y_true-y_pred ))
    SS_tot = K.sum(K.square( y_true - K.mean(y_true) ) )
    return ( 1 - SS_res/(SS_tot + K.epsilon()) )

model.compile(optimizer='sgd', 
         loss='mse',
         metrics = [coeff_determination])


来源:https://stackoverflow.com/questions/60038871/my-model-doesnt-seem-to-work-as-accuracy-and-loss-are-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!