当损耗函数是均为正方形误差时,如何定义准确性?是平均绝对百分比错误?
我使用的模型具有输出激活线性,并用 lose = mean_squared_error
编译
model.add(Dense(1))
model.add(Activation('linear')) # number
model.compile(loss='mean_squared_error', optimizer='adam', metrics=['accuracy'])
,并且输出看起来像这样:
Epoch 99/100
1000/1000 [==============================] - 687s 687ms/step - loss: 0.0463 - acc: 0.9689 - val_loss: 3.7303 - val_acc: 0.3250
Epoch 100/100
1000/1000 [==============================] - 688s 688ms/step - loss: 0.0424 - acc: 0.9740 - val_loss: 3.4221 - val_acc: 0.3701
那么eg val_acc:0.3250是什么意思? MANE_SQUARED_ERROR应该是标量而非百分比 - 不是吗?那么val_acc是平均平方错误还是平均百分比错误或其他功能?
根据MSE在Wikipedia上的定义: https://en.wikipedia.org/wikipedia.org/wiki/wiki/wiki/mean_squared_squared_eror_error
MSE是估算质量的量度,它始终是
非负,值接近零。
这是否意味着 val_acc的值:0.0
比 val_acc:0.325
更好?
编辑:训练时准确度量的输出的更多示例 - 随着我的训练,精度会提高。而损失功能 - MSE应减少。 MSE的准确性是否良好 - 如何在Keras中定义?
lAllocator: After 14014 get requests, put_count=14032 evicted_count=1000 eviction_rate=0.0712657 and unsatisfied allocation rate=0.071714
1000/1000 [==============================] - 453s 453ms/step - loss: 17.4875 - acc: 0.1443 - val_loss: 98.0973 - val_acc: 0.0333
Epoch 2/100
1000/1000 [==============================] - 443s 443ms/step - loss: 6.6793 - acc: 0.1973 - val_loss: 11.9101 - val_acc: 0.1500
Epoch 3/100
1000/1000 [==============================] - 444s 444ms/step - loss: 6.3867 - acc: 0.1980 - val_loss: 6.8647 - val_acc: 0.1667
Epoch 4/100
1000/1000 [==============================] - 445s 445ms/step - loss: 5.4062 - acc: 0.2255 - val_loss: 5.6029 - val_acc: 0.1600
Epoch 5/100
783/1000 [======================>.......] - ETA: 1:36 - loss: 5.0148 - acc: 0.2306
How is Accuracy defined when the loss function is mean square error? Is it mean absolute percentage error?
The model I use has output activation linear and is compiled with loss= mean_squared_error
model.add(Dense(1))
model.add(Activation('linear')) # number
model.compile(loss='mean_squared_error', optimizer='adam', metrics=['accuracy'])
and the output looks like this:
Epoch 99/100
1000/1000 [==============================] - 687s 687ms/step - loss: 0.0463 - acc: 0.9689 - val_loss: 3.7303 - val_acc: 0.3250
Epoch 100/100
1000/1000 [==============================] - 688s 688ms/step - loss: 0.0424 - acc: 0.9740 - val_loss: 3.4221 - val_acc: 0.3701
So what does e.g. val_acc: 0.3250 mean? Mean_squared_error should be a scalar not a percentage - shouldnt it? So is val_acc - mean squared error, or mean percentage error or another function?
From definition of MSE on wikipedia:https://en.wikipedia.org/wiki/Mean_squared_error
The MSE is a measure of the quality of an estimator—it is always
non-negative, and values closer to zero are better.
Does that mean a value of val_acc: 0.0
is better than val_acc: 0.325
?
edit: more examples of the output of accuracy metric when I train - where the accuracy is increase as I train more. While the loss function - mse should decrease. Is Accuracy well defined for mse - and how is it defined in Keras?
lAllocator: After 14014 get requests, put_count=14032 evicted_count=1000 eviction_rate=0.0712657 and unsatisfied allocation rate=0.071714
1000/1000 [==============================] - 453s 453ms/step - loss: 17.4875 - acc: 0.1443 - val_loss: 98.0973 - val_acc: 0.0333
Epoch 2/100
1000/1000 [==============================] - 443s 443ms/step - loss: 6.6793 - acc: 0.1973 - val_loss: 11.9101 - val_acc: 0.1500
Epoch 3/100
1000/1000 [==============================] - 444s 444ms/step - loss: 6.3867 - acc: 0.1980 - val_loss: 6.8647 - val_acc: 0.1667
Epoch 4/100
1000/1000 [==============================] - 445s 445ms/step - loss: 5.4062 - acc: 0.2255 - val_loss: 5.6029 - val_acc: 0.1600
Epoch 5/100
783/1000 [======================>.......] - ETA: 1:36 - loss: 5.0148 - acc: 0.2306
发布评论
评论(3)
您的问题至少有两个单独的问题。
现在应该从史努比博士的评论和另一个答案的评论中清楚地表明第一个:准确性是在回归问题(例如您的回归问题)中毫无意义的;另请参阅patyork在
澄清了这一点后,另一个问题是:
因为Keras确实确实返回了“准确性”,即使在回归环境中,它到底是什么,它是如何计算的?
在这里发光,向公共数据集(由于您没有提供有关数据的任何详细信息),即波士顿房屋价格数据集(当地保存为
housing.csv
),并运行一个简单的实验,如下所示:与您的情况一样,模型拟合历史记录(不适合)此处显示)显示损失的减少,精度大致增加。现在,使用适当的KERAS内置功能在同一训练集中评估模型性能:
Score
数组的确切内容取决于我们在模型汇编过程中确切要求的内容;在我们的情况下,第一个要素是损失(MSE),第二个要素是“准确性”。在这一点。
然后检查一下其中有多少等于true标签
y_true
,然后才能获得平均值。让我们使用普通Python&复制此操作。在我们的情况下,numpy代码为
y
:嗯,宾果游戏!实际上,这是
分数[1]
返回的相同值...以简短的故事:因为您(错误地)请求
Metrics = ['fecice']
在您的模型汇编中,keras将尽力而为为了满足您的需求,确实会返回一些“准确性”,如上所示,尽管在您的环境中完全没有意义。在许多设置中,凯拉斯(Keras)在引擎盖下执行毫无意义的操作,而无需给出任何提示或警告。我碰巧遇到的两个是:
在多级设置中,一个碰巧
lose ='binary_crossentropy'
(而不是codecorical_crossentropy )带有
metrics = ['准确性']
- 请参阅 keras binary_crossentropy vs cancorical_crossentropy绩效?和为什么binary_crossentropopy比exporical_crossentropy在keras中的多类分类?在极端情况下,当一个人要求辍学率为1.0时 - 请参阅 (放下所有输入单元)不如预期
There are at least two separate issues with your question.
The first one should be clear by now from the comments by Dr. Snoopy and the other answer: accuracy is meaningless in a regression problem, such as yours; see also the comment by patyork in this Keras thread. For good or bad, the fact is that Keras will not "protect" you or any other user from putting not-meaningful requests in your code, i.e. you will not get any error, or even a warning, that you are attempting something that does not make sense, such as requesting the accuracy in a regression setting.
Having clarified that, the other issue is:
Since Keras does indeed return an "accuracy", even in a regression setting, what exactly is it and how is it calculated?
To shed some light here, let's revert to a public dataset (since you do not provide any details about your data), namely the Boston house price dataset (saved locally as
housing.csv
), and run a simple experiment as follows:As in your case, the model fitting history (not shown here) shows a decreasing loss, and an accuracy roughly increasing. Let's evaluate now the model performance in the same training set, using the appropriate Keras built-in function:
The exact contents of the
score
array depend on what exactly we have requested during model compilation; in our case here, the first element is the loss (MSE), and the second one is the "accuracy".At this point, let us have a look at the definition of Keras
binary_accuracy
in themetrics.py
file:So, after Keras has generated the predictions
y_pred
, it first rounds them, and then checks to see how many of them are equal to the true labelsy_true
, before getting the mean.Let's replicate this operation using plain Python & Numpy code in our case, where the true labels are
Y
:Well, bingo! This is actually the same value returned by
score[1]
above...To make a long story short: since you (erroneously) request
metrics=['accuracy']
in your model compilation, Keras will do its best to satisfy you, and will return some "accuracy" indeed, calculated as shown above, despite this being completely meaningless in your setting.There are quite a few settings where Keras, under the hood, performs rather meaningless operations without giving any hint or warning to the user; two of them I have happened to encounter are:
Giving meaningless results when, in a multi-class setting, one happens to request
loss='binary_crossentropy'
(instead ofcategorical_crossentropy
) withmetrics=['accuracy']
- see my answers in Keras binary_crossentropy vs categorical_crossentropy performance? and Why is binary_crossentropy more accurate than categorical_crossentropy for multiclass classification in Keras?Disabling completely Dropout, in the extreme case when one requests a dropout rate of 1.0 - see my answer in Dropout behavior in Keras with rate=1 (dropping all input units) not as expected
损耗函数(在这种情况下均方根误差)用于指示您的预测与目标值偏离多远。在训练阶段,根据此数量进行重量更新。如果您要处理分类问题,那么定义一个称为准确性的额外指标很常见。它在预测正确类别的情况下监视了多少个情况。这表示为百分比值。因此,值为0.0意味着没有正确的决定,而仅1.0仅正确的决定。
当您的网络正在训练时,损失正在减少,通常的准确性会增加。
请注意,与损失相反,准确性通常不用于更新网络的参数。它有助于监视网络的学习进度和当前的性能。
The loss function (Mean Square Error in this case) is used to indicate how far your predictions deviate from the target values. In the training phase, the weights are updated based on this quantity. If you are dealing with a classification problem, it is quite common to define an additional metric called accuracy. It monitors in how many cases the correct class was predicted. This is expressed as a percentage value. Consequently, a value of 0.0 means no correct decision and 1.0 only correct decisons.
While your network is training, the loss is decreasing and usually the accuracy increases.
Note, that in contrast to loss, the accuracy is usally not used to update the parameters of your network. It helps to monitor the learning progress and the current performane of the network.
@desertnaut很清楚地说。
请考虑以下两部分代码
compile code < /a>
binary_accuracy code
您的Labels应该要整数,因为Keras不圆形 y_true ,并且您获得了很高的精度.......
@desertnaut has said it very clearly.
Consider the following two pieces of code
compile code
binary_accuracy code
Your labels should be integer,Because keras does not round y_true, and you get high accuracy.......