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

后端 未结 4 871
粉色の甜心
粉色の甜心 2020-12-23 09:36

I\'m pretty new to keras I have built a simple network to try:

import numpy as np;

from keras.models import Sequential;
from keras.layers import Dense,Activ         


        
相关标签:
4条回答
  • 2020-12-23 09:57

    Try this one.

    while trying to solve the Titanic problem from kaggle, I forgot to fill the missing data from the Dataframe, because of which the missing data was filled with "nan".

    The model threw a similar output

    #------------------------------------------------------
    
    Epoch 1/50
    
    891/891 [==============================] - 3s 3ms/step - loss: 9.8239 - acc: 0.0000e+00
    
    Epoch 2/50
    
    891/891 [==============================] - 1s 2ms/step - loss: 9.8231 - acc: 0.0000e+00
    
    Epoch 3/50
    
    891/891 [==============================] - 1s 1ms/step - loss: 9.8231 - acc: 0.0000e+00
    
    Epoch 4/50
    
    891/891 [==============================] - 1s 1ms/step - loss: 9.8231 - acc: 0.0000e+00
    
    Epoch 5/50
    
    891/891 [==============================] - 1s 1ms/step - loss: 9.8231 - acc: 0.0000e+00
    
    #------------------------------------------------------
    

    Make sure you prepare your data before feeding it to the model.

    In my case I had to do the following changes

    +++++++++++++++++++++++++++++++++++
    
    dataset[['Age']] = dataset[['Age']].fillna(value=dataset[['Age']].mean())
    
    dataset[['Fare']] = dataset[['Fare']].fillna(value=dataset[['Fare']].mean())
    
    dataset[['Embarked']] = dataset[['Embarked']].fillna(value=dataset['Embarked'].value_counts().idxmax())
    
    0 讨论(0)
  • 2020-12-23 10:07

    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
    
    0 讨论(0)
  • 2020-12-23 10:13

    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)

    0 讨论(0)
  • 2020-12-23 10:20

    I would like to point out something that is very important and has been unfortunately neglected: mean_squared_error is not an invalid loss function for classification.

    The mathematical properties of cross_entropy in conjunction with the assumptions of mean_squared_error(both of which I will not expand upon in this comment) make the latter inappropriate or worse than the cross_entropy when it comes to training on classification problems.

    0 讨论(0)
提交回复
热议问题