在Keras中训练分类问题时,神经网络精度始终为0 [英] Neural Network accuracy is always 0 while training classification problem in Keras

查看:135
本文介绍了在Keras中训练分类问题时,神经网络精度始终为0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个用于泰坦尼克分类问题的神经网络,但我的训练精度始终为0.我检查了其他解决方案,但找不到有效的解决方案.损耗减少,但精度为0.

I am making a neural network for the titanic classification problem but my training accuracy is always 0. I checked other solutions but couldn't find a solution that works. The loss reduces but accuracy is 0.

model= keras.Sequential(
    [
     layers.Dense(10,activation="relu",input_shape=(8,)),
     layers.Dense(10,activation="relu"),
     layers.Dense(1,activation="sigmoid")
    ]
)

model.compile(optimizer='adam',loss='binary_crossentropy',metrics=['Accuracy'])
model.fit(X_train,y_train,batch_size=64,epochs=200,verbose=2)

输入没有任何空值.

Survived     0
Age          0
Fare         0
Total_mem    0
female       0
Q            0
S            0
2            0
3            0
dtype: int64

有些值显示0精度.

Epoch 1/200
12/12 - 0s - loss: 0.7219 - accuracy: 0.0000e+00
Epoch 2/200
12/12 - 0s - loss: 0.7028 - accuracy: 0.0000e+00
Epoch 3/200
12/12 - 0s - loss: 0.6879 - accuracy: 0.0000e+00
Epoch 4/200
12/12 - 0s - loss: 0.6749 - accuracy: 0.0000e+00
Epoch 5/200
12/12 - 0s - loss: 0.6626 - accuracy: 0.0000e+00
Epoch 6/200
12/12 - 0s - loss: 0.6515 - accuracy: 0.0000e+00
Epoch 7/200
12/12 - 0s - loss: 0.6397 - accuracy: 0.0000e+00
Epoch 8/200
12/12 - 0s - loss: 0.6272 - accuracy: 0.0000e+00
Epoch 9/200
12/12 - 0s - loss: 0.6143 - accuracy: 0.0000e+00
Epoch 10/200
12/12 - 0s - loss: 0.6005 - accuracy: 0.0000e+00
Epoch 11/200
12/12 - 0s - loss: 0.5871 - accuracy: 0.0000e+00
Epoch 12/200
12/12 - 0s - loss: 0.5750 - accuracy: 0.0000e+00

推荐答案

首先,您错误地使用了 metrics = ['accuracy'] .其次,这指向了一个更深层次的错误,我认为这是无意的.我为此在Tensorflow回购上提出了一个问题.希望有人回应.

First, you are incorrectly using metrics=['accuracy']. Second, this points to a much deeper bug which I think is unintentional. I have raised an Issue for this on tensorflow repo. Let's hope someone responds.

Keras无法识别指标准确性 .Keras无法调用此处正确要求的MeanMetricWrapper.

Keras doesn't identify the metric Accuracy. Keras fails to call the MeanMetricWrapper which is required here properly.

修复开始显示该指标的正确值.

Fixing that starts showing proper values for the metric.

from tensorflow import keras
from tensorflow.keras import layers

X_train = np.random.random((100,8))
y_train = np.random.randint(0,2,(100,))

model = keras.Sequential(
    [
     layers.Dense(10,activation="relu",input_shape=(8,)),
     layers.Dense(10,activation="relu"),
     layers.Dense(1,activation="sigmoid")
    ]
)

model.compile(optimizer='adam',loss='binary_crossentropy',metrics=['accuracy'])
model.fit(X_train,y_train,batch_size=64,epochs=5,verbose=2)

Epoch 1/5
2/2 - 0s - loss: 0.6926 - accuracy: 0.5500
Epoch 2/5
2/2 - 0s - loss: 0.6915 - accuracy: 0.5500
Epoch 3/5
2/2 - 0s - loss: 0.6909 - accuracy: 0.5600
Epoch 4/5
2/2 - 0s - loss: 0.6900 - accuracy: 0.5700
Epoch 5/5
2/2 - 0s - loss: 0.6894 - accuracy: 0.5600
<tensorflow.python.keras.callbacks.History at 0x7f90e5f7d250>


了解并跟踪问题

@MathiasMüller提出了一个有效的担忧,即如果Keras无法识别大写指标,那么它应该抛出错误而不是运行.


Understanding and tracing the issue

@Mathias Müller brought out a valid concern that if Keras doesn't identify the uppercase metric, then it should throw an error instead of running.

我跟踪了GitHub实现,以查看当前代码的工作方式.步骤如下.(我已经链接了GitHub存储库中突出显示的代码行)

I traced the GitHub implementation to see how the current code works. Here are the steps. (I have linked highlighted lines of code from GitHub repo)

  1. 在编译期间,当传递度量标准时,参数存储在 _ get_metric_object (来自 compile_utils.py ).此功能的工作是获取输入并返回该度量标准类的度量标准对象.
  2. 此功能要做的第一件事是该类的工作是用均值"度量包装无状态度量函数.这将计算您添加的指标的平均值.
  3. 如果 NO ,则它将调用反序列化功能,其工作称为函数调用
  1. During compile, when a metric is passed, the parameter is stored in a MetricsContainer object.
  2. This container class then calls a function called _get_metric_object from compile_utils.py. The job of this function is to take the input and return a metric object of that metric's class.
  3. One of the first things this function does is to check if the input belongs to list ['accuracy', 'acc', 'crossentropy', 'ce'] or not.
    • If YES, then it directly fetches the classes from the metrics.py and calls the MeanMetricWrapper class. The job of this class is to wrap a stateless metric function with the Mean metric. This calculates the mean of the metric you have added.
    • If NO, then it calls the get function from metrics.py. The get function further calls a deserialize function whose job is call a function call deserialize_keras_object function from utils.generic_utils.py. This function's job is to take the string and retrieve the actual object!

现在让我们看看这两种情况.

Let's see the 2 scenarios now.

#With lower case accuracy
model.compile(optimizer='adam',loss='binary_crossentropy',metrics=['accuracy'])
print(model.metrics)

[<tensorflow.python.keras.metrics.Mean at 0x7f90c7ea10d0>,
 <tensorflow.python.keras.metrics.MeanMetricWrapper at 0x7f90c7d07e20>]

由于提供的指标属于 ['accuracy','acc','crossentropy','ce'] ,因此 _get_metric_object 函数获取tf.keras.metrics.Accuracy 类,并将其显式传递给 tf.keras.metrics.MeanMetricWrapper .这样可以按预期计算出平均准确度.

Since the metric provided belongs to the ['accuracy', 'acc', 'crossentropy', 'ce'], the _get_metric_object function fetches the tf.keras.metrics.Accuracy class and explicitly passes it to the tf.keras.metrics.MeanMetricWrapper. This calculates the mean accuracy as expected.

#With upper case Accuracy
model.compile(optimizer='adam',loss='binary_crossentropy',metrics=['Accuracy'])
print(model.metrics)

[<tensorflow.python.keras.metrics.Mean at 0x7f90e7285e20>,
 <tensorflow.python.keras.metrics.Accuracy at 0x7f90e72fceb0>]

一些有趣的事情在这里发生.由于准确性"不属于列表, _get_metric_object 调用 metrics.get()-> metrics.deserialize()-> generic_utils.deserialize_keras_object()函数可以简单地提取 tf.keras.metrics.Accuracy 并直接返回,而无需调用 tf.keras.metrics.MeanMetricWrapper.

Something interesting happens here. Since the "Accuracy" doesnt belong to the list, _get_metric_object calls the metrics.get() ->metrics.deserialize()-> generic_utils.deserialize_keras_object() function which simply pulls up the tf.keras.metrics.Accuracy and returns that directly, instead of calling tf.keras.metrics.MeanMetricWrapper.

这就是为什么您会获得不正确的值,但它不会引发错误的原因!

This is why you get incorrect values for accuracy, but it does not throw an error!!

这篇关于在Keras中训练分类问题时,神经网络精度始终为0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆