Linux
Ascend
GPU
CPU
Data Preparation
Beginner
Intermediate
Expert
The mindspore.dataset
module provided by MindSpore enables users to customize their data fetching strategy from disk. At the same time, data processing and tokenization operators are applied to the data. Pipelined data processing produces a continuous flow of data to the training network, improving overall performance.
In addition, MindSpore supports data loading in distributed scenarios. Users can define the number of shards while loading. For more details, see Loading the Dataset in Data Parallel Mode.
This tutorial briefly demonstrates how to load and process text data using MindSpore.
Prepare the following text data.
Welcome to Beijing!
北京欢迎您!
我喜欢English!
Create the tokenizer.txt
file, copy the text data to the file, and save the file under ./datasets
directory. The directory structure is as follow.
import os
if not os.path.exists('./datasets'):
os.mkdir('./datasets')
file_handle=open('./datasets/tokenizer.txt',mode='w')
file_handle.write('Welcome to Beijing \n北京欢迎您! \n我喜欢English! \n')
file_handle.close()
tree ./datasets
./datasets
└── tokenizer.txt
0 directories, 1 file
Import the mindspore.dataset
and mindspore.dataset.text
modules.
import mindspore.dataset as ds
import mindspore.dataset.text as text
MindSpore supports loading common datasets in the field of text processing that come in a variety of on-disk formats. Users can also implement custom dataset class to load customized data. For detailed loading methods of various datasets, please refer to the Loading Dataset chapter in the Programming Guide.
The following tutorial demonstrates loading datasets using the TextFileDataset
in the mindspore.dataset
module.
Configure the dataset directory as follows and create a dataset object.
DATA_FILE = "./datasets/tokenizer.txt"
dataset = ds.TextFileDataset(DATA_FILE, shuffle=False)
Create an iterator then obtain data through the iterator.
for data in dataset.create_dict_iterator(output_numpy=True):
print(text.to_str(data['text']))
The output without tokenization:
Welcome to Beijing!
北京欢迎您!
我喜欢English!
For the data processing operators currently supported by MindSpore and their detailed usage methods, please refer to the Processing Data chapter in the Programming Guide
The following tutorial demonstrates how to construct a pipeline and perform operations such as shuffle
and RegexReplace
on the text dataset.
Shuffle the dataset.
ds.config.set_seed(58)
dataset = dataset.shuffle(buffer_size=3)
for data in dataset.create_dict_iterator(output_numpy=True):
print(text.to_str(data['text']))
The output is as follows:
我喜欢English!
Welcome to Beijing!
北京欢迎您!
Perform RegexReplace
on the dataset.
replace_op1 = text.RegexReplace("Beijing", "Shanghai")
replace_op2 = text.RegexReplace("北京", "上海")
dataset = dataset.map(operations=replace_op1)
dataset = dataset.map(operations=replace_op2)
for data in dataset.create_dict_iterator(output_numpy=True):
print(text.to_str(data['text']))
The output is as follows:
我喜欢English!
Welcome to Shanghai!
上海欢迎您!
For the data tokenization operators currently supported by MindSpore and their detailed usage methods, please refer to the Tokenizer chapter in the Programming Guide.
The following tutorial demonstrates how to use the WhitespaceTokenizer
to tokenize words with space.
Create a tokenizer
.
tokenizer = text.WhitespaceTokenizer()
Apply the tokenizer
.
dataset = dataset.map(operations=tokenizer)
Create an iterator and obtain data through the iterator.
for data in dataset.create_dict_iterator(output_numpy=True):
print(text.to_str(data['text']).tolist())
The output after tokenization is as follows:
['我喜欢English!']
['Welcome', 'to', 'Shanghai!']
['上海欢迎您!']
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。