如何使用Tensorflow和Python整理预处理的数据?
本文向大家介绍如何使用Tensorflow和Python整理预处理的数据?,包括了如何使用Tensorflow和Python整理预处理的数据?的使用技巧和注意事项,需要的朋友参考一下
Tensorflow是Google提供的一种机器学习框架。它是一个开放源代码框架,与Python结合使用以实现算法,深度学习应用程序等等。它用于研究和生产目的。它具有优化技术,可帮助快速执行复杂的数学运算。这是因为它使用了NumPy和多维数组。这些多维数组也称为“张量”。该框架支持使用深度神经网络。
可以使用下面的代码行在Windows上安装'tensorflow'软件包-
pip install tensorflow
Tensor是TensorFlow中使用的数据结构。它有助于连接流程图中的边缘。该流程图称为“数据流程图”。张量不过是多维数组或列表。
我们将使用Illiad的数据集,其中包含来自William Cowper,Edward(德比伯爵)和Samuel Butler的三本翻译作品的文本数据。当给出单行文本时,训练模型以识别翻译器。使用的文本文件已经过预处理。这包括删除文档的页眉和页脚,行号和章节标题。
我们正在使用Google合作实验室来运行以下代码。Google Colab或Colaboratory可以帮助通过浏览器运行Python代码,并且需要零配置和对GPU(图形处理单元)的免费访问。合作已建立在Jupyter Notebook的基础上。
示例
以下是代码片段-
print("Combine the labelled dataset and reshuffle it") BUFFER_SIZE = 50000 BATCH_SIZE = 64 VALIDATION_SIZE = 5000 all_labeled_data = labeled_data_sets[0] for labeled_dataset in labeled_data_sets[1:]: all_labeled_data = all_labeled_data.concatenate(labeled_dataset) all_labeled_data = all_labeled_data.shuffle( BUFFER_SIZE, reshuffle_each_iteration=False) print("Displaying a few samples of input data") for text, label in all_labeled_data.take(8): print("句子是: ", text.numpy()) print("标签是:", label.numpy())
代码信用-https://www.tensorflow.org/tutorials/load_data/text
输出结果
Combine the labelled dataset and reshuffle it Displaying a few samples of input data 句子是: b'But I have now both tasted food, and given' 标签是: 0 句子是: b'All these shall now be thine: but if the Gods' 标签是: 1 句子是: b'Their spiry summits waved. There, unperceived' 标签是: 0 句子是: b'"I pray you, would you show your love, dear friends,' 标签是: 1 句子是: b'Entering beneath the clavicle the point' 标签是: 0 句子是: b'But grief, his father lost, awaits him now,' 标签是: 1 句子是: b'in the fore-arm where the sinews of the elbow are united, whereon he' 标签是: 2 句子是: b'For, as I think, I have already chased' 标签是: 0
解释
在对数据进行预处理之后,来自数据集的一些样本将显示在控制台上。
数据未分组,这意味着“ all_labeled_data”中的每个条目都映射到一个数据点。