如何使用Tensorflow使用Python下载和浏览Illiad数据集?

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

可以使用下面的代码行在Windows上安装'tensorflow'软件包-

pip install tensorflow

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

可以使用三个主要属性来标识它们-

  • 等级-讲述张量的维数。可以理解为张量的顺序或已定义的张量中的维数。

  • 类型-它告诉与张量元素关联的数据类型。它可以是一维,二维或n维张量。

  • 形状-它是行和列的总数。

我们将使用Illiad的数据集,其中包含来自William Cowper,Edward(德比伯爵)和Samuel Butler的三本翻译作品的文本数据。当给出单行文本时,训练模型以识别翻译器。使用的文本文件已经过预处理。这包括删除文档的页眉和页脚,行号和章节标题。

我们正在使用Google合作实验室来运行以下代码。Google Colab或Colaboratory可以帮助通过浏览器运行Python代码,并且需要零配置和对GPU(图形处理单元)的免费访问。协作已建立在Jupyter Notebook的基础上。以下是代码片段-

示例

print("Loading the Illiad dataset")
DIRECTORY_URL = 'https://storage.googleapis.com/download.tensorflow.org/data/illiad/'
FILE_NAMES = ['cowper.txt', 'derby.txt', 'butler.txt']

print("Iterating through the name of the files")
for name in FILE_NAMES:
   text_dir = utils.get_file(name, origin=DIRECTORY_URL + name)

parent_dir = pathlib.Path(text_dir).parent
print("The list of files in the directory")
print(list(parent_dir.iterdir()))

代码信用-https://www.tensorflow.org/tutorials/load_data/text

输出结果

Loading the Illiad dataset
Iterating through the name of the files
Downloading data from
https://storage.googleapis.com/download.tensorflow.org/data/illiad/cowper.txt
819200/815980 [==============================] - 0s 0us/step
Downloading data from
https://storage.googleapis.com/download.tensorflow.org/data/illiad/derby.txt
811008/809730 [==============================] - 0s 0us/step
Downloading data from
https://storage.googleapis.com/download.tensorflow.org/data/illiad/butler.txt
811008/807992 [==============================] - 0s 0us/step
The list of files in the directory
[PosixPath('/root/.keras/datasets/derby.txt'), PosixPath('/root/.keras/datasets/cowper.txt'),
PosixPath('/root/.keras/datasets/butler.txt')]
[ ]

解释

  • “ tf.data.TextLineDataset”用于从文本文件加载示例。

  • “ tf.text”用于预处理数据。

  • 首先,下载并浏览数据集。

猜你喜欢