分詞器
加入 Hugging Face 社群
並獲得增強的文件體驗
模型
BPE
class tokenizers.models.BPE
( vocab = None merges = None cache_capacity = None dropout = None unk_token = None continuing_subword_prefix = None end_of_word_suffix = None fuse_unk = None byte_fallback = False ignore_merges = False )
引數
- vocab (
Dict[str, int]
, 可選) — 一個字串鍵及其 ID 的字典,例如 {"am": 0,...}
- merges (
List[Tuple[str, str]]
, 可選) — 一個詞元對 (Tuple[str, str]
) 的列表,例如 [("a", "b"),...]
- cache_capacity (
int
, 可選) — BPE 快取可以包含的單詞數量。快取透過保留一定數量單詞的合併操作結果來加速處理過程。 - dropout (
float
, 可選) — 一個介於 0 和 1 之間的浮點數,表示要使用的 BPE dropout。 - unk_token (
str
, 可選) — 模型使用的未知詞元。 - continuing_subword_prefix (
str
, 可選) — 附加到不表示單詞開頭的子詞單元的字首。 - end_of_word_suffix (
str
, 可選) — 附加到表示單詞結尾的子詞單元的字尾。 - fuse_unk (
bool
, 可選) — 是否將任何連續的未知詞元合併成一個。 - byte_fallback (
bool
, 可選) — 是否使用 spm 位元組回退技巧(預設為 False) - ignore_merges (
bool
, 可選) — 是否在應用合併規則前先將詞元與詞彙表匹配。
BPE(位元組對編碼)演算法的實現
from_file
( vocab merge **kwargs ) → BPE
引數
- vocab (
str
) — vocab.json
檔案的路徑 - merges (
str
) — merges.txt
檔案的路徑
從這些檔案載入的 BPE 例項
從給定檔案例項化 BPE 模型。
此方法大致等同於執行
vocab, merges = BPE.read_file(vocab_filename, merges_filename)
bpe = BPE(vocab, merges)
如果您不需要保留 vocab, merges
的值,此方法比手動呼叫 read_file()
來初始化 BPE 更為最佳化
read_file
( vocab merges ) → 包含詞彙表和合並規則的 元組
引數
- vocab (
str
) — vocab.json
檔案的路徑 - merges (
str
) — merges.txt
檔案的路徑
載入到記憶體中的詞彙表和合並規則
讀取 vocab.json
和 merges.txt
檔案
此方法提供了一種讀取和解析這些檔案內容的方式,返回相關的資料結構。如果您想從記憶體中例項化一些 BPE 模型,此方法能從標準檔案中為您提供預期的輸入。
Model
class tokenizers.models.Model
( )
所有模型的基類
模型代表實際的分詞演算法。這部分將包含和管理學習到的詞彙表。
此類不能直接構造。請使用具體的模型之一。
get_trainer
( ) → Trainer
獲取關聯的 Trainer
檢索與此 Model 關聯的 Trainer
。
save
( folder prefix ) → List[str]
引數
- folder (
str
) — 目標資料夾的路徑,用於儲存各種檔案 - prefix (
str
, 可選) — 一個可選字首,用於為每個檔名新增字首
已儲存檔案的列表
儲存當前模型
將當前模型儲存在給定的資料夾中,並使用給定的字首為建立的各種檔案命名。此資料夾中任何已存在的同名檔案都將被覆蓋。
token_to_id
( tokens ) → int
引數
- token (
str
) — 要轉換為 ID 的詞元
與詞元關聯的 ID
獲取與詞元關聯的 ID
tokenize
( sequence ) → Token
的 List
對序列進行分詞
Unigram
class tokenizers.models.Unigram
( vocab unk_id byte_fallback )
引數
- vocab (
List[Tuple[str, float]]
, 可選, 可選) — 詞彙項及其相對得分的列表 [("am", -0.2442),...]
Unigram 演算法的實現
WordLevel
class tokenizers.models.WordLevel
( vocab unk_token )
引數
- vocab (
str
, 可選) — 一個字串鍵及其 ID 的字典,例如 {"am": 0,...}
- unk_token (
str
, 可選) — 模型使用的未知詞元。
WordLevel 演算法的實現
最簡單的分詞器模型,基於將詞元對映到其對應 ID。
from_file
( vocab unk_token ) → WordLevel
引數
- vocab (
str
) — vocab.json
檔案的路徑
從檔案載入的 WordLevel 例項
從給定檔案例項化 WordLevel 模型
此方法大致等同於執行
vocab = WordLevel.read_file(vocab_filename)
wordlevel = WordLevel(vocab)
如果您不需要保留 vocab
的值,此方法比手動呼叫 read_file()
來初始化 WordLevel 更為最佳化
read_file
( vocab ) → Dict[str, int]
引數
- vocab (
str
) — vocab.json
檔案的路徑
詞彙表,格式為 字典
讀取 vocab.json
檔案
此方法提供了一種讀取和解析詞彙表文件內容的方式,返回相關的資料結構。如果您想從記憶體中例項化一些 WordLevel 模型,此方法能從標準檔案中為您提供預期的輸入。
WordPiece
class tokenizers.models.WordPiece
( vocab unk_token max_input_chars_per_word )
引數
- vocab (
Dict[str, int]
, 可選) — 一個字串鍵及其 ID 的字典,例如 {"am": 0,...}
- unk_token (
str
, 可選) — 模型使用的未知詞元。 - max_input_chars_per_word (
int
, 可選) — 單個單詞允許的最大字元數。
WordPiece 演算法的實現
from_file
( vocab **kwargs ) → WordPiece
引數
- vocab (
str
) — vocab.txt
檔案的路徑
從檔案載入的 WordPiece 例項
從給定檔案例項化 WordPiece 模型
此方法大致等同於執行
vocab = WordPiece.read_file(vocab_filename)
wordpiece = WordPiece(vocab)
如果您不需要保留 vocab
的值,此方法比手動呼叫 read_file()
來初始化 WordPiece 更為最佳化
read_file
( vocab ) → Dict[str, int]
引數
- vocab (
str
) — vocab.txt
檔案的路徑
詞彙表,格式為 字典
讀取 vocab.txt
檔案
此方法提供了一種讀取和解析 WordPiece 模型使用的標準 vocab.txt 檔案內容的方式,返回相關的資料結構。如果您想從記憶體中例項化一些 WordPiece 模型,此方法能從標準檔案中為您提供預期的輸入。
< > 在 GitHub 上更新
© . This site is unofficial and not affiliated with Hugging Face, Inc.