如何使用 Keras 和 Python 评估模型?


Tensorflow 是谷歌提供的机器学习框架。它是一个开源框架,与 Python 结合使用以实现算法、深度学习应用程序等等。它用于研究和生产目的。

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("An instance of the model is created")
model = create_model()

print("The model is being evaluated")
loss, acc = model.evaluate(test_images, test_labels, verbose=2)
print("This is an untrained model, with accuracy: {:5.3f}%".format(100 * acc))

代码来源:https://tensorflowcn.cn/tutorials/keras/save_and_load

输出

An Instance of the model is created 
The Model is being evaluated 32/32 -0s - loss: - spare_categories_accurancy: 0.0930 
This is an untrained model, with accuracy: 9.300%

Learn Python in-depth with real-world projects through our Python certification course. Enroll and become a certified expert to boost your career.

解释

  • 创建模型实例。

  • 这是一个新的、未经训练的模型,在测试集上进行评估。

  • “evaluate”方法用于检查模型在新数据上的表现。

  • 此外,还确定了模型训练时的损失和模型的准确率。

  • 损失和准确率打印在控制台上。

更新于:2021年1月20日

134 次查看

开启您的 职业生涯

通过完成课程获得认证

开始学习
广告