如何使用Python评估使用Keras恢复的模型?
Tensorflow是由谷歌提供的机器学习框架,它是一个开源框架,与Python一起用于实现算法、深度学习应用等等。它被用于研究和生产目的。它拥有优化技术,可以快速地执行复杂的数学运算。
“tensorflow”包可以使用以下代码行在Windows上安装:
pip install tensorflow
Keras是作为ONEIROS(开放的神经电子智能机器人操作系统)项目的一部分开发的。Keras是一个深度学习API,用Python编写。它是一个高级API,具有有效的接口,可以帮助解决机器学习问题。
它在Tensorflow框架上运行。它的构建旨在帮助快速实验。它提供了关键的抽象和构建块,这些构建块在开发和封装机器学习解决方案时是必不可少的。它高度可扩展,并具有跨平台功能。这意味着Keras可以在TPU或GPU集群上运行。Keras模型也可以导出到网页浏览器或移动手机上。
Keras已经存在于Tensorflow包中。它可以使用以下代码行访问:
import tensorflow
from tensorflow import keras
我们使用Google Colaboratory来运行以下代码。Google Colab或Colaboratory可以在浏览器上运行Python代码,不需要任何配置,并免费访问GPU(图形处理单元)。Colaboratory是建立在Jupyter Notebook之上的。以下是代码:
更多Python相关文章,请阅读:Python 教程
示例
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://www.tensorflow.org/tutorials/keras/save_and_load
输出
解释
- 使用“evaluate”方法评估恢复的模型。
- 确定训练过程中的准确性和损失。
- 在控制台上显示这些值。
- 使用“predict”方法进行预测。
- 在控制台上显示测试数据的尺寸。
- 使用“create_model”方法创建另一个新实例的模型。
- 将模型适应于7个周期的训练数据。