Keras 3 API 文件 / KerasNLP / 預訓練模型 / Bert / BertMaskedLMPreprocessor 層

BertMaskedLMPreprocessor 層

[來源]

BertMaskedLMPreprocessor 類別

keras_nlp.models.BertMaskedLMPreprocessor(
    tokenizer,
    sequence_length=512,
    truncate="round_robin",
    mask_selection_rate=0.15,
    mask_selection_length=96,
    mask_token_rate=0.8,
    random_token_rate=0.1,
    **kwargs
)

用於遮罩語言建模任務的 BERT 預處理。

此預處理層將為遮罩語言建模任務準備輸入。它主要用於 keras_nlp.models.BertMaskedLM 任務模型。預處理將分多個步驟進行。

  1. 使用 tokenizer 對任意數量的輸入片段進行標記化。
  2. 將輸入與適當的 "[CLS]""[SEP]""[PAD]" 標記一起打包。
  3. 隨機選擇非特殊標記進行遮罩,由 mask_selection_rate 控制。
  4. 建構適合使用 keras_nlp.models.BertMaskedLM 任務模型進行訓練的 (x, y, sample_weight) 元組。

參數

  • tokenizer:一個 keras_nlp.models.BertTokenizer 實例。
  • sequence_length:整數。打包輸入的長度。
  • truncate:字串。用於將一批片段列表截斷以適應 sequence_length 的算法。值可以是 round_robinwaterfall
    • "round_robin":可用空間以循環方式一次分配一個標記給仍然需要的輸入,直到達到限制。
    • "waterfall":預算的分配使用「瀑布」算法,以從左到右的方式分配配額,並填滿桶,直到預算用盡。它支持任意數量的片段。
  • mask_selection_rate:浮點數。輸入標記被動態遮罩的概率。
  • mask_selection_length:整數。給定樣本中遮罩標記的最大數量。
  • mask_token_rate:浮點數。所選標記將被替換為遮罩標記的概率。
  • random_token_rate:浮點數。選定詞彙被隨機詞彙替換的機率。選定詞彙維持原樣的機率為 1 - mask_token_rate - random_token_rate

呼叫引數

  • x:單一字串序列的張量,或多個要打包在一起的張量序列的元組。輸入可以是批次或非批次的。對於單一序列,原始 Python 輸入將轉換為張量。對於多個序列,請直接傳遞張量。
  • y:標籤資料。應始終為 None,因為此層會產生標籤。
  • sample_weight:標籤權重。應始終為 None,因為此層會產生標籤權重。

範例

直接在資料上呼叫此層。

preprocessor = keras_nlp.models.BertMaskedLMPreprocessor.from_preset(
    "bert_base_en_uncased"
)

# Tokenize and mask a single sentence.
preprocessor("The quick brown fox jumped.")

# Tokenize and mask a batch of single sentences.
preprocessor(["The quick brown fox jumped.", "Call me Ishmael."])

# Tokenize and mask sentence pairs.
# In this case, always convert input to tensors before calling the layer.
first = tf.constant(["The quick brown fox jumped.", "Call me Ishmael."])
second = tf.constant(["The fox tripped.", "Oh look, a whale."])
preprocessor((first, second))

使用 tf.data.Dataset 進行映射。

preprocessor = keras_nlp.models.BertMaskedLMPreprocessor.from_preset(
    "bert_base_en_uncased"
)

first = tf.constant(["The quick brown fox jumped.", "Call me Ishmael."])
second = tf.constant(["The fox tripped.", "Oh look, a whale."])

# Map single sentences.
ds = tf.data.Dataset.from_tensor_slices(first)
ds = ds.map(preprocessor, num_parallel_calls=tf.data.AUTOTUNE)

# Map sentence pairs.
ds = tf.data.Dataset.from_tensor_slices((first, second))
# Watch out for tf.data's default unpacking of tuples here!
# Best to invoke the `preprocessor` directly in this case.
ds = ds.map(
    lambda first, second: preprocessor(x=(first, second)),
    num_parallel_calls=tf.data.AUTOTUNE,
)

[來源]

from_preset 方法

BertMaskedLMPreprocessor.from_preset(preset, **kwargs)

從模型預設設定建立 keras_nlp.models.Preprocessor 的執行個體。

預設設定是包含用於儲存和載入預先訓練模型的設定、權重和其他檔案資源的目錄。preset 可以傳遞為以下其中一項:

  1. 內建預設設定識別碼,例如 'bert_base_en'
  2. Kaggle 模型控點,例如 'kaggle://user/bert/keras/bert_base_en'
  3. Hugging Face 控點,例如 'hf://user/bert_base_en'
  4. 本機預設設定目錄的路徑,例如 './bert_base_en'

對於任何 Preprocessor 子類別,您可以執行 cls.presets.keys() 來列出類別上所有可用的內建預設設定。

由於給定模型通常有多個預處理類別,因此應在特定子類別上呼叫此方法,例如 keras_nlp.models.BertTextClassifierPreprocessor.from_preset()

參數

  • preset:字串。內建預設設定識別碼、Kaggle 模型控點、Hugging Face 控點或本機目錄的路徑。

範例

# Load a preprocessor for Gemma generation.
preprocessor = keras_nlp.models.GemmaCausalLMPreprocessor.from_preset(
    "gemma_2b_en",
)

# Load a preprocessor for Bert classification.
preprocessor = keras_nlp.models.BertTextClassifierPreprocessor.from_preset(
    "bert_base_en",
)
預設設定名稱 參數 說明
bert_tiny_en_uncased 4.39M 2 層 BERT 模型,所有輸入均為小寫。以英文維基百科 + BooksCorpus 訓練。
bert_small_en_uncased 28.76M 4 層 BERT 模型,所有輸入均為小寫。以英文維基百科 + BooksCorpus 訓練。
bert_medium_en_uncased 41.37M 8 層 BERT 模型,所有輸入均為小寫。以英文維基百科 + BooksCorpus 訓練。
bert_base_en_uncased 109.48M 12 層 BERT 模型,所有輸入均為小寫。以英文維基百科 + BooksCorpus 訓練。
bert_base_en 108.31M 12 層 BERT 模型,會保留大小寫。以英文維基百科 + BooksCorpus 訓練。
bert_base_zh 102.27M 12 層 BERT 模型。以中文維基百科訓練。
bert_base_multi 177.85M 12 層 BERT 模型,會保留大小寫。以 104 種語言的維基百科訓練。
bert_large_en_uncased 335.14M 24 層 BERT 模型,所有輸入均為小寫。以英文維基百科 + BooksCorpus 訓練。
bert_large_en 333.58M 24 層 BERT 模型,會保留大小寫。以英文維基百科 + BooksCorpus 訓練。
bert_tiny_en_uncased_sst2 4.39M 在 SST-2 情緒分析資料集上微調的 bert_tiny_en_uncased 骨幹模型。

tokenizer 屬性

keras_nlp.models.BertMaskedLMPreprocessor.tokenizer

用於將字串標記化的標記器。