如何使用Python和TensorFlow查看向量化数据的样本?

如何使用Python和TensorFlow查看向量化数据的样本?

TensorFlow是由Google提供的机器学习框架。它是一个开源框架,与Python一起使用来实现算法、深度学习应用等等。它被用于研究和生产目的。

可以使用以下代码在Windows上安装’tensorflow’软件包:

pip install tensorflow
Python

Tensor是TensorFlow中使用的一种数据结构。它有助于连接流程图中的边缘。这个流程图被称为“数据流图”。张量就是多维数组或列表。

我们将使用Illiad数据集,该数据集包含William Cowper、Edward(Earl of Derby)和Samuel Butler所翻译的三本书的文本数据。该模型被训练用于在给出单行文本时识别翻译者。使用的文本文件已经进行过预处理。这包括删除文档页眉和页脚、行号和章节标题。

我们使用Google Colaboratory运行下面的代码。Google Colab或Colaboratory帮助在浏览器上运行Python代码,要求零配置且具有免费使用GPU(图形处理器)的权限。Colaboratory建立在Jupyter Notebook之上。

更多Python相关文章,请阅读:Python 教程

示例

print("Look at sample data after processing it")
example_text, example_label = next(iter(all_labeled_data))

print("The sentence is : ", example_text.numpy())
vectorized_text, example_label = preprocess_text(example_text, example_label)

print("The vectorized sentence is : ", vectorized_text.numpy())
print("Run the pre-process function on the data")

all_encoded_data = all_labeled_data.map(preprocess_text)
Python

代码来源 – https://www.tensorflow.org/tutorials/load_data/text

输出

Look at sample data after processing it
The sentence is : b'But I have now both tasted food, and given'
The vectorized sentence is : [ 20 21 58 49 107 3497 909 2 4 540]
Run the pre-process function on the data
Python

解释

  • 一旦数据被矢量化,所有标记都会被转换为整数。

  • 它们被转换为整数,以便模型可以解释所提供的输入。

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

登录

注册