• 文件 >
  • torchtext.data.functional
捷徑

torchtext.data.functional

generate_sp_model

torchtext.data.functional.generate_sp_model(filename, vocab_size=20000, model_type='unigram', model_prefix='m_user')[原始碼]

訓練 SentencePiece 分詞器。

參數:
  • filename – 用於訓練 SentencePiece 模型的資料檔案。

  • vocab_size – 詞彙量大小 (預設值:20,000)。

  • model_type – SentencePiece 模型的類型,包括 unigram、bpe、char、word。

  • model_prefix – 儲存模型和詞彙表檔案的前綴。

輸出
模型和詞彙表儲存在兩個獨立的檔案中,前綴為

model_prefix。

範例

>>> from torchtext.data.functional import generate_sp_model
>>> generate_sp_model('test.csv', vocab_size=23456, model_prefix='spm_user')

load_sp_model

torchtext.data.functional.load_sp_model(spm)[原始碼]

從檔案載入 sentencepiece 模型。

參數:

spm – 儲存 sentencepiece 模型的檔案路徑或檔案物件。

輸出

輸出:一個 SentencePiece 模型。

範例

>>> from torchtext.data.functional import load_sp_model
>>> sp_model = load_sp_model("m_user.model")
>>> sp_model = load_sp_model(open("m_user.model", 'rb'))

sentencepiece_numericalizer

torchtext.data.functional.sentencepiece_numericalizer(sp_model)[原始碼]
一個 sentencepiece 模型,用於將文字句子數值化為

id 的生成器。

參數:

sp_model – 一個 SentencePiece 模型。

輸出
輸出:一個生成器,輸入文字句子,輸出基於 SentencePiece 模型的對應 id。

範例

>>> from torchtext.data.functional import sentencepiece_numericalizer
>>> sp_id_generator = sentencepiece_numericalizer(sp_model)
>>> list_a = ["sentencepiece encode as pieces", "examples to   try!"]
>>> list(sp_id_generator(list_a))
    [[9858, 9249, 1629, 1305, 1809, 53, 842],
     [2347, 13, 9, 150, 37]]

sentencepiece_tokenizer

torchtext.data.functional.sentencepiece_tokenizer(sp_model)[原始碼]
一個 sentencepiece 模型,用於將文字句子標記化為

token 的生成器。

參數:

sp_model – 一個 SentencePiece 模型。

輸出
輸出:一個生成器,輸入文字句子,輸出基於 SentencePiece 模型的對應 id。

基於 SentencePiece 模型的對應 token。

範例

>>> from torchtext.data.functional import sentencepiece_tokenizer
>>> sp_tokens_generator = sentencepiece_tokenizer(sp_model)
>>> list_a = ["sentencepiece encode as pieces", "examples to   try!"]
>>> list(sp_tokens_generator(list_a))
    [['_sentence', 'piece', '_en', 'co', 'de', '_as', '_pieces'],
     ['_example', 's', '_to', '_try', '!']]

custom_replace

torchtext.data.functional.custom_replace(replace_pattern)[原始碼]

用於轉換文字字串的轉換器。

範例

>>> from torchtext.data.functional import custom_replace
>>> custom_replace_transform = custom_replace([(r'S', 's'), (r'\s+', ' ')])
>>> list_a = ["Sentencepiece encode  aS  pieces", "exampleS to   try!"]
>>> list(custom_replace_transform(list_a))
    ['sentencepiece encode as pieces', 'examples to try!']

simple_space_split

torchtext.data.functional.simple_space_split(iterator)[原始碼]

以空格分割文字字串的轉換器。

範例

>>> from torchtext.data.functional import simple_space_split
>>> list_a = ["Sentencepiece encode as pieces", "example to try!"]
>>> list(simple_space_split(list_a))
    [['Sentencepiece', 'encode', 'as', 'pieces'], ['example', 'to', 'try!']]

numericalize_tokens_from_iterator

torchtext.data.functional.numericalize_tokens_from_iterator(vocab, iterator, removed_tokens=None)[來源]

使用詞彙表從 token 迭代器產生 id 列表。

參數:
  • vocab – 將 token 轉換為 id 的詞彙表。

  • iterator – 產生 token 列表的迭代器。

  • removed_tokens – 從輸出資料集中移除的 token(預設值:None)

範例

>>> from torchtext.data.functional import simple_space_split
>>> from torchtext.data.functional import numericalize_tokens_from_iterator
>>> 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])
>>> [0, 2, 3]
>>> [2, 3]

filter_wikipedia_xml

torchtext.data.functional.filter_wikipedia_xml(text_iterator)[來源]

根據 https://github.com/facebookresearch/fastText/blob/master/wikifil.pl 過濾 wikipedia xml 行。

參數:

text_iterator – 產生字串的迭代器類型物件。範例包括字串列表、文字 IO、產生器等。

範例

>>> from torchtext.data.functional import filter_wikipedia_xml
>>> from torchtext.datasets import EnWik9
>>> data_iter = EnWik9(split='train')
>>> filter_data_iter = filter_wikipedia_xml(data_iter)
>>> file_name = '.data/EnWik9/enwik9'
>>> filter_data_iter = filter_wikipedia_xml(open(file_name,'r'))

to_map_style_dataset

torchtext.data.functional.to_map_style_dataset(iter_data)[來源]

將可迭代樣式資料集轉換為映射樣式資料集。

參數:

iter_data – 迭代器類型物件。範例包括可迭代資料集、字串列表、文字 IO、產生器等。

範例

>>> from torchtext.datasets import IMDB
>>> from torchtext.data import to_map_style_dataset
>>> train_iter = IMDB(split='train')
>>> train_dataset = to_map_style_dataset(train_iter)
>>> file_name = '.data/EnWik9/enwik9'
>>> data_iter = to_map_style_dataset(open(file_name,'r'))

文件

存取 PyTorch 的完整開發人員文件

查看文件

教學

取得適用於初學者和進階開發人員的深入教學

查看教學

資源

尋找開發資源並取得問題解答

查看資源