Deep Learning With Keras 简明教程

Evaluating Model Performance

要评估模型性能,我们调用 evaluate 方法如下 −

To evaluate the model performance, we call evaluate method as follows −

loss_and_metrics = model.evaluate(X_test, Y_test, verbose=2)

要评估模型性能,我们调用 evaluate 方法如下 −

To evaluate the model performance, we call evaluate method as follows −

loss_and_metrics = model.evaluate(X_test, Y_test, verbose=2)

我们将使用以下两个语句打印损失和准确率 −

We will print the loss and accuracy using the following two statements −

print("Test Loss", loss_and_metrics[0])
print("Test Accuracy", loss_and_metrics[1])

运行以上语句时,您会看到以下输出 −

When you run the above statements, you would see the following output −

Test Loss 0.08041584826191042
Test Accuracy 0.9837

这显示了 98% 的测试准确率,这对于我们来说是可接受的。这对于我们来说意味着,在 2% 的情况下,手写数字将无法正确分类。我们还将绘制准确率和损失指标,以查看模型在测试数据上的表现。

This shows a test accuracy of 98%, which should be acceptable to us. What it means to us that in 2% of the cases, the handwritten digits would not be classified correctly. We will also plot accuracy and loss metrics to see how the model performs on the test data.

Plotting Accuracy Metrics

我们在训练期间记录了 history 来获取准确性指标的绘图。以下代码将在每个时期绘制准确性。我们挑选训练数据准确率 (“acc”) 和验证数据准确率 (“val_acc”) 来作图。

We use the recorded history during our training to get a plot of accuracy metrics. The following code will plot the accuracy on each epoch. We pick up the training data accuracy (“acc”) and the validation data accuracy (“val_acc”) for plotting.

plot.subplot(2,1,1)
plot.plot(history.history['acc'])
plot.plot(history.history['val_acc'])
plot.title('model accuracy')
plot.ylabel('accuracy')
plot.xlabel('epoch')
plot.legend(['train', 'test'], loc='lower right')

输出图如下所示:

The output plot is shown below −

plotting accuracy metrics

正如您在图表中所看到的,准确性在前两个历元中迅速提高,表明网络正在快速学习。之后,曲线变平,表明训练模型无需太多历元。通常,如果训练数据准确性(“acc”)持续提高,而验证数据准确性(“val_acc”)变差,则表明遇到过度拟合。它表明模型开始记住数据。

As you can see in the diagram, the accuracy increases rapidly in the first two epochs, indicating that the network is learning fast. Afterwards, the curve flattens indicating that not too many epochs are required to train the model further. Generally, if the training data accuracy (“acc”) keeps improving while the validation data accuracy (“val_acc”) gets worse, you are encountering overfitting. It indicates that the model is starting to memorize the data.

我们还将绘制损失指标以检查模型的性能。

We will also plot the loss metrics to check our model’s performance.

Plotting Loss Metrics

同样,我们绘制训练(“loss”)和测试(“val_loss”)数据集上的损失。这是使用以下代码完成的:

Again, we plot the loss on both the training (“loss”) and test (“val_loss”) data. This is done using the following code −

plot.subplot(2,1,2)
plot.plot(history.history['loss'])
plot.plot(history.history['val_loss'])
plot.title('model loss')
plot.ylabel('loss')
plot.xlabel('epoch')
plot.legend(['train', 'test'], loc='upper right')

此代码的输出如下所示:

The output of this code is shown below −

plotting loss metrics

正如您在图表中所看到的,训练集上的损失在前两个历元中迅速下降。对于测试集,损失不会像训练集那样以相同速率下降,而是保持在多个历元中几乎持平。这意味着我们的模型可以很好地推广到看不见的数据。

As you can see in the diagram, the loss on the training set decreases rapidly for the first two epochs. For the test set, the loss does not decrease at the same rate as the training set, but remains almost flat for multiple epochs. This means our model is generalizing well to unseen data.

现在,我们将使用训练好的模型来预测测试数据中的数字。

Now, we will use our trained model to predict the digits in our test data.