Machine Learning 简明教程

Machine Learning - Epoch

在机器学习中,纪元指在模型训练过程中对整个训练数据集进行一次完整迭代。简单来说,就是算法在训练阶段遍历整个数据集的次数。

In machine learning, an epoch refers to a complete iteration over the entire training dataset during the model training process. In simpler terms, it is the number of times the algorithm goes through the entire dataset during the training phase.

在训练过程中,算法针对训练数据进行预测,计算损失并更新模型参数以减少损失。而目标是通过最小化损失函数优化模型的性能。当模型针对所有训练数据进行预测后,整个纪元就算完成了。

During the training process, the algorithm makes predictions on the training data, computes the loss, and updates the model parameters to reduce the loss. The objective is to optimize the model’s performance by minimizing the loss function. One epoch is considered complete when the model has made predictions on all the training data.

纪元是训练过程中的一项基本参数,因为它们能显著影响模型的性能。将纪元数量设置得太低会导致欠拟合模型,而将其设置得太高又会导致过拟合。

Epochs are an essential parameter in the training process as they can significantly affect the performance of the model. Setting the number of epochs too low can result in an underfit model, while setting it too high can lead to overfitting.

欠拟合发生在模型未能捕捉数据中的底层模式、并且针对训练和测试数据集的性能低下时。当模型过于简单或训练不足时,这种情况就会发生。在这种情况下,增加纪元数量能帮助模型更多地学习数据并提升其性能。

Underfitting occurs when the model fails to capture the underlying patterns in the data and performs poorly on both the training and testing datasets. It happens when the model is too simple or not trained enough. In such cases, increasing the number of epochs can help the model learn more from the data and improve its performance.

另一方面,当模型学习了训练数据中的噪声且针对训练集表现良好,但在测试数据上的表现很差时,过拟合便发生了。当模型过于复杂或者针对太多的纪元接受训练时,便会出现这种情况。为避免过拟合,必须限制纪元的数量,并使用早期停止或辍学之类的规则化技术。

Overfitting, on the other hand, happens when the model learns the noise in the training data and performs well on the training set but poorly on the testing data. It occurs when the model is too complex or trained for too many epochs. To avoid overfitting, the number of epochs must be limited, and other regularization techniques like early stopping or dropout should be used.

Implementation in Python

在 Python 中,纪元数量在机器学习模型的训练循环中确定。例如,当使用 Keras 库训练神经网络时,你可以使用“fit”方法中的“纪元”参数来设置纪元数量。

In Python, the number of epochs is specified in the training loop of the machine learning model. For example, when training a neural network using the Keras library, you can set the number of epochs using the "epochs" argument in the "fit" method.

Example

# import necessary libraries
import numpy as np
from keras.models import Sequential
from keras.layers import Dense

# generate some random data for training
X_train = np.random.rand(100, 10)
y_train = np.random.randint(0, 2, size=(100,))

# create a neural network model
model = Sequential()
model.add(Dense(16, input_dim=10, activation='relu'))
model.add(Dense(1, activation='sigmoid'))

# compile the model with binary cross-entropy loss and adam optimizer
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])

# train the model with 10 epochs
model.fit(X_train, y_train, epochs=10)

在此示例中,我们为训练生成一些随机数据,并使用一层输入层、一层隐藏层和一个输出层创建一个简单的神经网络模型。我们在“fit”方法中使用二进制交叉熵损失和亚当优化器来编译模型,并将纪元数量设置为 10。

In this example, we generate some random data for training and create a simple neural network model with one input layer, one hidden layer, and one output layer. We compile the model with binary cross-entropy loss and the Adam optimizer and set the number of epochs to 10 in the "fit" method.

在训练过程中,模型针对训练数据进行预测,计算损失并更新权重以最小化损失。完成 10 个纪元后,模型被认为接受了训练,我们可以使用它对新的、未见数据进行预测。

During the training process, the model makes predictions on the training data, computes the loss, and updates the weights to minimize the loss. After completing 10 epochs, the model is considered trained, and we can use it to make predictions on new, unseen data.

当你执行此代码时,它会生成如下输出:

When you execute this code, it will produce an output like this −

Epoch 1/10
4/4 [==============================] - 31s 2ms/step - loss: 0.7012 - accuracy: 0.4976
Epoch 2/10
4/4 [==============================] - 0s 1ms/step - loss: 0.6995 - accuracy: 0.4390
Epoch 3/10
4/4 [==============================] - 0s 1ms/step - loss: 0.6921 - accuracy: 0.5123
Epoch 4/10
4/4 [==============================] - 0s 1ms/step - loss: 0.6778 - accuracy: 0.5474
Epoch 5/10
4/4 [==============================] - 0s 1ms/step - loss: 0.6819 - accuracy: 0.5542
Epoch 6/10
4/4 [==============================] - 0s 1ms/step - loss: 0.6795 - accuracy: 0.5377
Epoch 7/10
4/4 [==============================] - 0s 1ms/step - loss: 0.6840 - accuracy: 0.5303
Epoch 8/10
4/4 [==============================] - 0s 1ms/step - loss: 0.6795 - accuracy: 0.5554
Epoch 9/10
4/4 [==============================] - 0s 1ms/step - loss: 0.6706 - accuracy: 0.5545
Epoch 10/10
4/4 [==============================] - 0s 1ms/step - loss: 0.6722 - accuracy: 0.5556