Keras 3 API 文件 / KerasNLP / 標記器 / compute_word_piece_vocabulary 函式

compute_word_piece_vocabulary 函式

[來源]

compute_word_piece_vocabulary 函式

keras_nlp.tokenizers.compute_word_piece_vocabulary(
    data,
    vocabulary_size,
    vocabulary_output_file=None,
    lowercase=False,
    strip_accents=False,
    split=True,
    split_on_cjk=True,
    suffix_indicator="##",
    reserved_tokens=["[PAD]", "[CLS]", "[SEP]", "[UNK]", "[MASK]"],
)

訓練 WordPiece 詞彙表的工具。

從輸入資料集或檔案名稱清單訓練 WordPiece 詞彙表。

對於自訂資料載入和預標記化(split=False),輸入 data 應該是 tf.data.Dataset。如果 data 是檔案名稱清單,則檔案格式必須是純文字檔案,並且在訓練期間會逐行讀取文字。

引數

  • datatf.data.Dataset 或檔案名稱清單。
  • vocabulary_size:int。要訓練的詞彙表最大大小。
  • vocabulary_output_file:str。用於寫入詞彙表檔案的位置。預設為 None
  • lowercase:bool。如果為 True,則在標記化之前,輸入文字將會轉換為小寫。預設為 False
  • strip_accents:bool。如果為 True,則在標記化之前,將會從文字中移除所有重音符號。預設為 False
  • split:bool。如果為 True,則輸入將會根據空格和標點符號進行分割,並且所有標點符號將會保留為標記。如果為 False,則在呼叫標記器之前,應該先分割(「預先標記化」)輸入,並作為完整單字的密集或不規則張量傳遞。當 data 是檔案名稱清單時,split 必須為 True。預設為 True
  • split_on_cjk:bool。如果為 True,則輸入將會根據中日韓越文字元進行分割,例如中文、日文、韓文和越南文字元(https://zh.wikipedia.org/wiki/CJK_統一漢字_(Unicode區段))。請注意,這僅適用於 splitTrue 的情況。預設為 True
  • suffix_indicator:字串。預先附加到 WordPiece 的字元,表示它是另一個子詞的後綴。例如 "##ing"。預設為 "##"
  • reserved_tokens:字串清單。必須包含在詞彙表中的詞彙清單。

回傳值

回傳詞彙表條目清單。

範例

基本用法(從資料集)。

>>> inputs = tf.data.Dataset.from_tensor_slices(["bat sat pat mat rat"])
>>> vocab = compute_word_piece_vocabulary(inputs, 13)
>>> vocab
['[PAD]', '[CLS]', '[SEP]', '[UNK]', '[MASK]', 'a', 'b', 'm', 'p', 'r', 's', 't', '##at']
>>> tokenizer = keras_nlp.tokenizers.WordPieceTokenizer(vocabulary=vocab, oov_token="[UNK]")
>>> outputs = inputs.map(tokenizer.tokenize)
>>> for x in outputs:
...     print(x)
tf.Tensor([ 6 12 10 12  8 12  7 12  9 12], shape=(10,), dtype=int32)

基本用法(從檔名)。

with open("test.txt", "w+") as f:
    f.write("bat sat pat mat rat\n")
inputs = ["test.txt"]
vocab = keras_nlp.tokenizers.compute_word_piece_vocabulary(inputs, 13)

自訂分割用法(從資料集)。

>>> def normalize_and_split(x):
...     "Strip punctuation and split on whitespace."
...     x = tf.strings.regex_replace(x, r"\p{P}", "")
...     return tf.strings.split(x)
>>> inputs = tf.data.Dataset.from_tensor_slices(["bat sat: pat mat rat.\n"])
>>> split_inputs = inputs.map(normalize_and_split)
>>> vocab = compute_word_piece_vocabulary(
...     split_inputs, 13, split=False,
... )
>>> vocab
['[PAD]', '[CLS]', '[SEP]', '[UNK]', '[MASK]', 'a', 'b', 'm', 'p', 'r', 's', 't', '##at']
>>> tokenizer = keras_nlp.tokenizers.WordPieceTokenizer(vocabulary=vocab)
>>> inputs.map(tokenizer.tokenize)

自訂分割用法(從檔名)。

def normalize_and_split(x):
    "Strip punctuation and split on whitespace."
    x = tf.strings.regex_replace(x, r"\p{P}", "")
    return tf.strings.split(x)
with open("test.txt", "w+") as f:
    f.write("bat sat: pat mat rat.\n")
inputs = tf.data.TextLineDataset(["test.txt"])
split_inputs = inputs.map(normalize_and_split)
vocab = keras_nlp.tokenizers.compute_word_piece_vocabulary(
    split_inputs, 13, split=False
)
tokenizer = keras_nlp.tokenizers.WordPieceTokenizer(vocabulary=vocab)
inputs.map(tokenizer.tokenize)