代码拉取完成,页面将自动刷新
torchtext.data.functional.numericalize_tokens_from_iterator(
vocab,
iterator,
removed_tokens=None
)
更多内容详见torchtext.data.functional.numericalize_tokens_from_iterator。
class mindspore.dataset.text.transforms.Lookup(
vocab,
unknown_token=None,
data_type=mstype.int32
)
更多内容详见mindspore.dataset.text.transforms.Lookup。
PyTorch:从分词迭代器中生成词汇表对应的id列表,输入为词汇与id对应的映射表、词汇迭代器,返回创建好的迭代器对象,可从中获取对应词汇的id。
MindSpore:依据词汇与id的映射表,查找词汇对应的id。
import mindspore.dataset as ds
from mindspore.dataset import text
import torch as T
from torchtext.data.functional import simple_space_split, numericalize_tokens_from_iterator
# In MindSpore, return id of given word with looking up the vocab.
Vocab_file_path = '/path/to/testVocab/vocab_list.txt'
vocab = text.Vocab.from_file(Vocab_file_path, ",", None, ["<pad>", "<unk>"], True)
lookup = text.Lookup(vocab)
text_file_dataset_dir = '/path/to/testVocab/words.txt'
text_file_dataset = ds.TextFileDataset(dataset_files=text_file_dataset_dir)
text_file_dataset = text_file_dataset.map(operations=lookup, input_columns=["text"])
for d in text_file_dataset.create_dict_iterator(num_epochs=1, output_numpy=True):
print(d["text"])
# Out:
# 14
# 12
# 13
# 10
# 15
# 11
# In torch, return the ids iterator with looking up the vocab.
vocab = {'Sentencepiece' : 0, 'encode' : 1, 'as' : 2, 'pieces' : 3}
ids_iter = numericalize_tokens_from_iterator(vocab, simple_space_split(["Sentencepiece as pieces", "as pieces"]))
for ids in ids_iter:
print([num for num in ids])
# Out:
# [0, 2, 3]
# [2, 3]
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。