Python 如何理解 Keras 模型拟合中的 loss acc val_loss val_acc

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47299624/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 18:08:23  来源:igfitidea点击:

How to understand loss acc val_loss val_acc in Keras model fitting

pythonmachine-learningneural-networkdeep-learningkeras

提问by Rocco

I'm new on Keras and have some questions on how to understanding my model results. Here is my result:(for your convenience, I only paste the loss acc val_loss val_acc after each epoch here)

我是 Keras 的新手,对如何理解我的模型结果有一些疑问。这是我的结果:(为方便起见,我只在此处粘贴每个时期后的损失 acc val_loss val_acc)

Train on 4160 samples, validate on 1040 samples as below:

训练 4160 个样本,验证 1040 个样本如下:

Epoch 1/20
4160/4160 - loss: 3.3455 - acc: 0.1560 - val_loss: 1.6047 - val_acc: 0.4721

Epoch 2/20
4160/4160 - loss: 1.7639 - acc: 0.4274 - val_loss: 0.7060 - val_acc: 0.8019

Epoch 3/20
4160/4160 - loss: 1.0887 - acc: 0.5978 - val_loss: 0.3707 - val_acc: 0.9087

Epoch 4/20
4160/4160 - loss: 0.7736 - acc: 0.7067 - val_loss: 0.2619 - val_acc: 0.9442

Epoch 5/20
4160/4160 - loss: 0.5784 - acc: 0.7690 - val_loss: 0.2058 - val_acc: 0.9433

Epoch 6/20
4160/4160 - loss: 0.5000 - acc: 0.8065 - val_loss: 0.1557 - val_acc: 0.9750

Epoch 7/20
4160/4160 - loss: 0.4179 - acc: 0.8296 - val_loss: 0.1523 - val_acc: 0.9606

Epoch 8/20
4160/4160 - loss: 0.3758 - acc: 0.8495 - val_loss: 0.1063 - val_acc: 0.9712

Epoch 9/20
4160/4160 - loss: 0.3202 - acc: 0.8740 - val_loss: 0.1019 - val_acc: 0.9798

Epoch 10/20
4160/4160 - loss: 0.3028 - acc: 0.8788 - val_loss: 0.1074 - val_acc: 0.9644

Epoch 11/20
4160/4160 - loss: 0.2696 - acc: 0.8923 - val_loss: 0.0581 - val_acc: 0.9856

Epoch 12/20
4160/4160 - loss: 0.2738 - acc: 0.8894 - val_loss: 0.0713 - val_acc: 0.9837

Epoch 13/20
4160/4160 - loss: 0.2609 - acc: 0.8913 - val_loss: 0.0679 - val_acc: 0.9740

Epoch 14/20
4160/4160 - loss: 0.2556 - acc: 0.9022 - val_loss: 0.0599 - val_acc: 0.9769

Epoch 15/20
4160/4160 - loss: 0.2384 - acc: 0.9053 - val_loss: 0.0560 - val_acc: 0.9846

Epoch 16/20
4160/4160 - loss: 0.2305 - acc: 0.9079 - val_loss: 0.0502 - val_acc: 0.9865

Epoch 17/20
4160/4160 - loss: 0.2145 - acc: 0.9185 - val_loss: 0.0461 - val_acc: 0.9913

Epoch 18/20
4160/4160 - loss: 0.2046 - acc: 0.9183 - val_loss: 0.0524 - val_acc: 0.9750

Epoch 19/20
4160/4160 - loss: 0.2055 - acc: 0.9120 - val_loss: 0.0440 - val_acc: 0.9885

Epoch 20/20
4160/4160 - loss: 0.1890 - acc: 0.9236 - val_loss: 0.0501 - val_acc: 0.9827

Here are my understandings:

以下是我的理解:

  1. The two losses (both loss and val_loss) are decreasing and the tow acc (acc and val_acc) are increasing. So this indicates the modeling is trained in a good way.

  2. The val_acc is the measure of how good the predictions of your model are. So for my case, it looks like the model was trained pretty well after 6 epochs, and the rest training is not necessary.

  1. 两个损失(loss 和 val_loss)都在减少,而拖曳 acc(acc 和 val_acc)在增加。所以这表明模型训练得很好。

  2. val_acc 是衡量模型预测效果的指标。所以就我而言,看起来模型在 6 个 epoch 后训练得很好,其余的训练是没有必要的。

My Questions are:

我的问题是:

  1. The acc (the acc on training set) is always smaller, actually much smaller, than val_acc. Is this normal? Why this happens?In my mind, acc should usually similar to better than val_acc.

  2. After 20 epochs, the acc is still increasing. So should I use more epochs and stop when acc stops increasing? Or I should stop where val_acc stops increasing, regardless of the trends of acc?

  3. Is there any other thoughts on my results?

  1. acc(训练集上的 acc)总是比 val_acc 小,实际上小得多。这是正常的吗?为什么会发生这种情况?在我看来,acc 通常应该比 val_acc 更好。

  2. 在 20 个 epoch 之后,acc 仍在增加。那么我应该使用更多的纪元并在 acc 停止增加时停止吗?或者我应该在 val_acc 停止增加的地方停止,不管 acc 的趋势如何?

  3. 对我的结果还有其他想法吗?

Thanks!

谢谢!

回答by Ioannis Nasios

Answering your questions:

回答您的问题:

  1. As described on official keras FAQ
  1. 如官方keras 常见问题解答中所述

the training loss is the average of the losses over each batch of training data. Because your model is changing over time, the loss over the first batches of an epoch is generally higher than over the last batches. On the other hand, the testing loss for an epoch is computed using the model as it is at the end of the epoch, resulting in a lower loss.

训练损失是每批训练数据损失的平均值。由于您的模型随着时间的推移而发生变化,因此一个 epoch 的第一批的损失通常高于最后一批的损失。另一方面,一个 epoch 的测试损失是使用模型计算的,因为它在 epoch 结束时,导致较低的损失。

  1. Training should be stopped when val_acc stops increasing, otherwise your model will probably overffit. You can use earlystopping callback to stop training.

  2. Your model seems to achieve very good results. Keep up the good work.

  1. 当 val_acc 停止增加时应停止训练,否则您的模型可能会过度拟合。您可以使用 earlystopping 回调来停止训练。

  2. 您的模型似乎取得了非常好的结果。保持良好的工作。

回答by Nicolas Gervais

  1. What are lossand val_loss?
  1. 什么是lossval_loss

In deep learning, the lossis the value that a neural network is trying to minimize. That is how a neural network learns—by adjusting weights and biases in a manner that reduces the loss.

在深度学习中,损失是神经网络试图最小化的值。这就是神经网络的学习方式——通过以减少损失的方式调整权重和偏差。

For instance, in regressiontasks, you have a continuous target, e.g., height. What you want to minimize is the difference between your predictions, and the actual height. You can use mean_absolute_erroras loss so the neural network knows this what it needs to minimize.

例如,在回归任务中,您有一个连续的目标,例如高度。您想要最小化的是您的预测与实际高度之间的差异。您可以将其mean_absolute_error用作损失,以便神经网络知道它需要最小化什么。

In classification, it's a little more complicated, but very similar. Predicted classes are based on probability. The loss is therefore also based on probability. In classification, the neural network minimizes the likelihood to assign a low probability to the actual class. The loss is typically categorical_crossentropy.

分类中,它有点复杂,但非常相似。预测类别基于概率。因此,损失也是基于概率的。在分类中,神经网络最小化为实际类别分配低概率的可能性。损失通常为categorical_crossentropy

lossand val_lossdiffer because the former is applied to the train set, and the latter the test set. As such, the latter is a good indication of how the model performs on unseen data. You can get a validation set by using validation_data=[x_test, y_test]or validation_split=0.5.

lossval_loss不同,因为前者是施加到列车组,而后者的测试集。因此,后者很好地表明了模型如何处理看不见的数据。您可以使用validation_data=[x_test, y_test]或获取验证集validation_split=0.5

It's best to rely on the val_lossto prevent overfitting. Overfittingis when the model fits the training data too closely, and the losskeeps decreasing while the val_lossis stale, or increases.

最好依靠val_loss来防止过拟合。过度拟合是指模型与训练数据的拟合过于紧密,并且在过时或增加时loss不断减少val_loss

In Keras, you can use EarlyStoppingto stop training when the val_lossstops decreasing. Read here.

在 Keras 中,您可以使用停止减少EarlyStopping时停止训练val_loss在这里阅读。

Read more about deep learning losses here: Loss and Loss Functions for Training Deep Learning Neural Networks.

在此处阅读有关深度学习损失的更多信息:用于训练深度学习神经网络的损失和损失函数

  1. What are accand val_acc?
  1. 什么是accval_acc

Accuracy is a metric only for classification. It gives the percentage of instances that are correctly classified.

准确度是仅用于分类的度量。它给出了正确分类的实例的百分比。

Once again, accis on the training data, and val_accis on the validation data. It's best to rely on val_accfor a fair representation of model performance because a good neural network will end up fitting the training data at 100%, but would perform poorly on unseen data.

再一次,acc是在训练数据val_acc上,在验证数据上。最好依靠val_acc模型性能的公平表示,因为一个好的神经网络最终会以 100% 拟合训练数据,但在看不见的数据上表现不佳。