如何使用Tensorflow和伊利亚特数据集,并用Python检查测试数据的性能?


Tensorflow 是 Google 提供的一个机器学习框架。它是一个开源框架,与 Python 结合使用以实现算法、深度学习应用程序等等。它用于研究和生产目的。它具有优化技术,有助于快速执行复杂的数学运算。这是因为它使用了 NumPy 和多维数组。这些多维数组也称为“张量”。

该框架支持使用深度神经网络。它具有高度可扩展性,并附带许多流行的数据集。它使用 GPU 计算并自动管理资源。它附带大量机器学习库,并且得到了良好的支持和记录。该框架能够运行深度神经网络模型、训练它们,并创建预测各个数据集相关特征的应用程序。

张量是 TensorFlow 中使用的数据结构。它有助于连接数据流图中的边。此数据流图称为“数据流图”。张量只不过是多维数组或列表。

我们将使用伊利亚特的数据集,其中包含威廉·考珀、爱德华(德比伯爵)和塞缪尔·巴特勒的三部翻译作品的文本数据。该模型经过训练,可以在给出一行文本时识别翻译者。使用的文本文件已进行预处理。这包括删除文档标题和页脚、行号和章节标题。

我们正在使用 Google Colaboratory 运行以下代码。Google Colab 或 Colaboratory 帮助通过浏览器运行 Python 代码,并且无需任何配置即可免费访问 GPU(图形处理单元)。Colaboratory 建立在 Jupyter Notebook 之上。

示例

以下是代码片段:

print("Testing the model on new data")
inputs = [
   "the allies, and his armour flashed about him so that he seemed to all",
   "And with loud clangor of his arms he fell.",
   "Join'd to th' Ionians with their flowing robes,",
]
print("The predict method is being called")
predicted_scores = export_model.predict(inputs)
predicted_labels = tf.argmax(predicted_scores, axis=1)
for input, label in zip(inputs, predicted_labels):
   print("The question is : ", input)
   print("The predicted label is : ", label.numpy())

代码来源:https://tensorflowcn.cn/tutorials/load_data/text

输出

Testing the model on new data
The predict method is being called
The question is : the allies, and his armour flashed about him so that he seemed to all
The predicted label is : 2
The question is : And with loud clangor of his arms he fell.
The predicted label is : 0
The question is : Join'd to th' Ionians with their flowing robes,
The predicted label is : 1

解释

  • 数据编译完成后,并拟合训练数据后,会在从未见过的數據上进行测试。

  • 在测试数据上调用“predict”方法。

  • 显示一些预测标签的示例及其对应的問題。

更新于:2021年1月19日

59 次查看

开启你的职业生涯

通过完成课程获得认证

开始学习
广告

© . All rights reserved.