Keras 简明教程
Keras - Model Evaluation and Model Prediction
本章介绍 Keras 中的模型评估和模型预测。
This chapter deals with the model evaluation and model prediction in Keras.
让我们从了解模型评估开始。
Let us begin by understanding the model evaluation.
Model Evaluation
评估是模型开发过程中的一个过程,用于检查模型是否最适合给定的问题和对应数据。Keras 模型提供了一个函数,evaluate,它对模型进行评估。它有三个主要参数:
Evaluation is a process during development of the model to check whether the model is best fit for the given problem and corresponding data. Keras model provides a function, evaluate which does the evaluation of the model. It has three main arguments,
-
Test data
-
Test data label
-
verbose - true or false
让我们评估在上一章中使用测试数据创建的模型。
Let us evaluate the model, which we created in the previous chapter using test data.
score = model.evaluate(x_test, y_test, verbose = 0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])
执行上述代码将输出以下信息。
Executing the above code will output the below information.
0
测试准确率为 98.28%。我们创建了一个最佳模型来识别手写数字。从积极的一面来说,我们仍然可以改善我们的模型。
The test accuracy is 98.28%. We have created a best model to identify the handwriting digits. On the positive side, we can still scope to improve our model.
Model Prediction
Prediction 是最后一步,也是我们对模型生成的预期结果。Keras 提供了一个方法,predict,以获取训练模型的预测。predict 方法的特征如下:
Prediction is the final step and our expected outcome of the model generation. Keras provides a method, predict to get the prediction of the trained model. The signature of the predict method is as follows,
predict(
x,
batch_size = None,
verbose = 0,
steps = None,
callbacks = None,
max_queue_size = 10,
workers = 1,
use_multiprocessing = False
)
此处,除了第一个参数(引用未知输入数据)以外,所有参数都是可选的。应该保持形状以获得正确的预测。
Here, all arguments are optional except the first argument, which refers the unknown input data. The shape should be maintained to get the proper prediction.
让我们使用以下代码对在上一章中创建的 MPL 模型进行预测 −
Let us do prediction for our MPL model created in previous chapter using below code −
pred = model.predict(x_test)
pred = np.argmax(pred, axis = 1)[:5]
label = np.argmax(y_test,axis = 1)[:5]
print(pred)
print(label)
在此,
Here,
-
Line 1 call the predict function using test data.
-
Line 2 gets the first five prediction
-
Line 3 gets the first five labels of the test data.
-
Line 5 - 6 prints the prediction and actual label.
上述应用程序的输出如下 −
The output of the above application is as follows −
[7 2 1 0 4]
[7 2 1 0 4]
这两个数组的输出是相同的,这表明我们的模型正确预测了前五个图像。
The output of both array is identical and it indicate that our model predicts correctly the first five images.