如何使用 Python 和 Keras 评估已恢复的模型?
Tensorflow 是 Google 提供的一个机器学习框架。它是一个开源框架,与 Python 结合使用,可以实现算法、深度学习应用程序等等。它用于研究和生产目的。它具有优化技术,有助于快速执行复杂的数学运算。
可以使用以下代码行在 Windows 上安装“tensorflow”包:
pip install tensorflow
Keras 是作为 ONEIROS(开放式神经电子智能机器人操作系统)项目研究的一部分开发的。Keras 是一个用 Python 编写的深度学习 API。它是一个高级 API,具有高效的界面,有助于解决机器学习问题。
它运行在 Tensorflow 框架之上。它的构建是为了帮助快速进行实验。它提供了开发和封装机器学习解决方案所必需的基本抽象和构建块。它具有高度的可扩展性,并具有跨平台能力。这意味着 Keras 可以运行在 TPU 或 GPU 集群上。Keras 模型也可以导出到 Web 浏览器或移动电话上运行。
Keras 已经存在于 Tensorflow 包中。可以使用以下代码行访问它。
import tensorflow from tensorflow import keras
我们正在使用 Google Colaboratory 来运行以下代码。Google Colab 或 Colaboratory 帮助在浏览器上运行 Python 代码,无需任何配置即可免费访问 GPU(图形处理单元)。Colaboratory 建立在 Jupyter Notebook 之上。以下是代码:
示例
print("The restored model is evaluated") loss, acc = new_model.evaluate(test_images, test_labels, verbose=2) print('This is the restored model, with accuracy: {:5.3f}%'.format(100 * acc)) print("Predictions are being made, the dimensions of the predictions are") print(new_model.predict(test_images).shape) print("A new model instance is created") model = create_model() print("The model is fit to the training data") model.fit(train_images, train_labels, epochs=7)
代码来源:https://tensorflowcn.cn/tutorials/keras/save_and_load
输出
Learn Python in-depth with real-world projects through our Python certification course. Enroll and become a certified expert to boost your career.
解释
使用“evaluate”方法评估恢复的模型。
确定其在训练期间的准确性和损失。
这些值显示在控制台上。
使用“predict”方法进行预测。
测试数据的维度显示在控制台上。
使用“create_model”方法创建模型的另一个新实例。
模型在 7 个 epoch 中适应训练数据。