简体   繁体   English

标准的 Keras 模型输出是什么意思? Keras 中的 epoch 和 loss 是什么?

[英]What does the standard Keras model output mean? What is epoch and loss in Keras?

I have just built my first model using Keras and this is the output.我刚刚使用 Keras 构建了我的第一个模型,这是输出。 It looks like the standard output you get after building any Keras artificial neural network.它看起来像是构建任何 Keras 人工神经网络后获得的标准输出。 Even after looking in the documentation, I do not fully understand what the epoch is and what the loss is which is printed in the output.即使在查看文档后,我也不完全了解时代是什么以及输出中打印的损失是什么。

What is epoch and loss in Keras? Keras 中的 epoch 和 loss 是什么?

(I know it's probably an extremely basic question, but I couldn't seem to locate the answer online, and if the answer is really that hard to glean from the documentation I thought others would have the same question and thus decided to post it here.) (我知道这可能是一个非常基本的问题,但我似乎无法在网上找到答案,如果答案真的很难从文档中收集,我认为其他人也会有同样的问题,因此决定将其发布在这里.)

Epoch 1/20
1213/1213 [==============================] - 0s - loss: 0.1760     
Epoch 2/20
1213/1213 [==============================] - 0s - loss: 0.1840     
Epoch 3/20
1213/1213 [==============================] - 0s - loss: 0.1816     
Epoch 4/20
1213/1213 [==============================] - 0s - loss: 0.1915     
Epoch 5/20
1213/1213 [==============================] - 0s - loss: 0.1928     
Epoch 6/20
1213/1213 [==============================] - 0s - loss: 0.1964     
Epoch 7/20
1213/1213 [==============================] - 0s - loss: 0.1948     
Epoch 8/20
1213/1213 [==============================] - 0s - loss: 0.1971     
Epoch 9/20
1213/1213 [==============================] - 0s - loss: 0.1899     
Epoch 10/20
1213/1213 [==============================] - 0s - loss: 0.1957     
Epoch 11/20
1213/1213 [==============================] - 0s - loss: 0.1923     
Epoch 12/20
1213/1213 [==============================] - 0s - loss: 0.1910     
Epoch 13/20
1213/1213 [==============================] - 0s - loss: 0.2104     
Epoch 14/20
1213/1213 [==============================] - 0s - loss: 0.1976     
Epoch 15/20
1213/1213 [==============================] - 0s - loss: 0.1979     
Epoch 16/20
1213/1213 [==============================] - 0s - loss: 0.2036     
Epoch 17/20
1213/1213 [==============================] - 0s - loss: 0.2019     
Epoch 18/20
1213/1213 [==============================] - 0s - loss: 0.1978     
Epoch 19/20
1213/1213 [==============================] - 0s - loss: 0.1954     
Epoch 20/20
1213/1213 [==============================] - 0s - loss: 0.1949

Just to answer the questions more specifically, here's a definition of epoch and loss:为了更具体地回答这些问题,这里是 epoch 和 loss 的定义:

Epoch : A full pass over all of your training data. Epoch :对所有训练数据的完整传递。

For example, in your view above, you have 1213 observations.例如,在上面的视图中,您有 1213 个观察值。 So an epoch concludes when it has finished a training pass over all 1213 of your observations.因此,当完成对您所有 1213 次观察的训练时,一个 epoch 就结束了。

Loss : A scalar value that we attempt to minimize during our training of the model.损失:我们在模型训练期间试图最小化的标量值。 The lower the loss, the closer our predictions are to the true labels.损失越低,我们的预测就越接近真实标签。

This is usually Mean Squared Error (MSE) as David Maust said above, or often in Keras, Categorical Cross Entropy这通常是上面 David Maust 所说的均方误差 (MSE),或者通常是 Keras, Categorical Cross Entropy


What you'd expect to see from running fit on your Keras model, is a decrease in loss over n number of epochs.您期望从 Keras 模型上的运行拟合中看到的是损失在 n 个时期内的减少。 Your training run is rather abnormal, as your loss is actually increasing.你的训练运行很不正常,因为你的损失实际上在增加。 This could be due to a learning rate that is too large, which is causing you to overshoot optima.可能是由于学习率太大,导致您超出了最优值。

As jaycode mentioned, you will want to look at your model's performance on unseen data, as this is the general use case of Machine Learning.正如 jaycode 所提到的,您需要查看模型在未见数据上的性能,因为这是机器学习的一般用例。

As such, you should include a list of metrics in your compile method, which could look like:因此,您应该在 compile 方法中包含一个指标列表,它可能如下所示:

model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy'])

As well as run your model on validation during the fit method, such as:以及在 fit 方法期间运行您的模型进行验证,例如:

model.fit(data, labels, validation_split=0.2)

There's a lot more to explain, but hopefully this gets you started.还有很多要解释的,但希望这能让你开始。

One epoch ends when your model had run the data through all nodes in your network and ready to update the weights to reach optimal loss value.当您的模型通过网络中的所有节点运行数据并准备更新权重以达到最佳损失值时,一个时期结束。 That is, smaller is better.也就是说,越小越好。 In your case, as there are higher loss scores on higher epoch, it "seems" the model is better on first epoch.在您的情况下,由于较高时期的损失分数较高,因此“似乎”该模型在第一个时期更好。

I said "seems" since we can't actually tell for sure yet as the model has not been tested using proper cross validation method ie it is evaluated only against its training data.我说“似乎”是因为我们实际上还不能确定,因为该模型尚未使用适当的交叉验证方法进行测试,即仅根据其训练数据对其进行评估。

Ways to improve your model:改进模型的方法:

  • Use cross validation in your Keras model in order to find out how the model actually perform, does it generalize well when predicting new data it has never seen before?在您的 Keras 模型中使用交叉验证以了解模型的实际执行情况,它在预测以前从未见过的新数据时是否能很好地概括?
  • Adjust your learning rate, structure of neural network model, number of hidden units / layers, init, optimizer, and activator parameters used in your model among myriad other things.调整你的学习率、神经网络模型的结构、隐藏单元/层的数量、初始化、优化器和模型中使用的激活器参数等等。

Combining sklearn's GridSearchCV with Keras can automate this process.将 sklearn 的 GridSearchCV 与 Keras 相结合,可以使这个过程自动化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM