Keras 3 API 文件 / KerasHub / 模型 API / MaskedLMPreprocessor

MaskedLMPreprocessor

[來源]

MaskedLMPreprocessor 類別

keras_hub.models.MaskedLMPreprocessor(
    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
)

遮罩語言模型預處理層的基底類別。

MaskedLMPreprocessor 任務會包裝一個 keras_hub.tokenizer.Tokenizer,以建立用於遮罩語言模型任務的預處理層。它旨在與 keras.models.MaskedLM 任務配對使用。

所有 MaskedLMPreprocessor 都會接收單一輸入。這可以是單一字串、一批字串,或應該組合成單一序列的一批字串片段的元組。請參閱以下範例。這些輸入將會被標記化、組合,並沿著序列隨機遮罩。

此層將始終輸出一個 (x, y, sample_weight) 元組,其中 x 是一個包含遮罩、標記化輸入的字典,y 包含在 x 中被遮罩的標記,而 sample_weight 則標記 y 中包含填充值的位置。x 的確切內容將會根據所使用的模型而有所不同。

所有 MaskedLMPreprocessor 任務都包含一個 from_preset() 建構函數,可用於載入預先訓練的配置和詞彙表。您可以直接在此基底類別上呼叫 from_preset() 建構函數,在這種情況下,會自動為您的模型建立正確的類別。

範例。

preprocessor = keras_hub.models.MaskedLMPreprocessor.from_preset(
    "bert_base_en_uncased",
    sequence_length=256, # Optional.
)

# Tokenize, mask and pack a single sentence.
x = "The quick brown fox jumped."
x, y, sample_weight = preprocessor(x)

# Preprocess a batch of labeled sentence pairs.
first = ["The quick brown fox jumped.", "Call me Ishmael."]
second = ["The fox tripped.", "Oh look, a whale."]
x, y, sample_weight = preprocessor((first, second))

# With a [`tf.data.Dataset`](https://tensorflow.dev.org.tw/api_docs/python/tf/data/Dataset).
ds = tf.data.Dataset.from_tensor_slices((first, second))
ds = ds.map(preprocessor, num_parallel_calls=tf.data.AUTOTUNE)

[來源]

from_preset 方法

MaskedLMPreprocessor.from_preset(preset, config_file="preprocessor.json", **kwargs)

從模型預設集建立 keras_hub.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_hub.models.BertTextClassifierPreprocessor.from_preset()

參數

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

範例

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

# Load a preprocessor for Bert classification.
preprocessor = keras_hub.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 主幹模型。
xlm_roberta_base_multi 277.45M 12 層 XLM-RoBERTa 模型,保留大小寫。在 100 種語言的 CommonCrawl 上訓練。
xlm_roberta_large_multi 558.84M 24 層 XLM-RoBERTa 模型,保留大小寫。在 100 種語言的 CommonCrawl 上訓練。
f_net_base_en 82.86M 12 層 FNet 模型,保留大小寫。在 C4 資料集上訓練。
f_net_large_en 236.95M 24 層 FNet 模型,保留大小寫。在 C4 資料集上訓練。
deberta_v3_extra_small_en 70.68M 12 層 DeBERTaV3 模型,保留大小寫。在英文維基百科、BookCorpus 和 OpenWebText 上訓練。
deberta_v3_small_en 141.30M 6 層 DeBERTaV3 模型,保留大小寫。在英文維基百科、BookCorpus 和 OpenWebText 上訓練。
deberta_v3_base_en 183.83M 12 層 DeBERTaV3 模型,保留大小寫。在英文維基百科、BookCorpus 和 OpenWebText 上訓練。
deberta_v3_large_en 434.01M 24 層 DeBERTaV3 模型,保留大小寫。在英文維基百科、BookCorpus 和 OpenWebText 上訓練。
deberta_v3_base_multi 278.22M 12 層 DeBERTaV3 模型,保留大小寫。在 2.5TB 多語言 CC100 資料集上訓練。
roberta_base_en 124.05M 12 層 RoBERTa 模型,保留大小寫。在英文維基百科、BooksCorpus、CommonCraw 和 OpenWebText 上訓練。
roberta_large_en 354.31M 24 層 RoBERTa 模型,保留大小寫。在英文維基百科、BooksCorpus、CommonCraw 和 OpenWebText 上訓練。
distil_bert_base_en_uncased 66.36M 6 層 DistilBERT 模型,所有輸入均為小寫。使用 BERT 作為教師模型,在英文維基百科 + BooksCorpus 上訓練。
distil_bert_base_en 65.19M 6 層 DistilBERT 模型,保留大小寫。使用 BERT 作為教師模型,在英文維基百科 + BooksCorpus 上訓練。
distil_bert_base_multi 134.73M 6 層 DistilBERT 模型,保留大小寫。在 104 種語言的維基百科上訓練
albert_base_en_uncased 11.68M 12 層 ALBERT 模型,所有輸入均為小寫。在英文維基百科 + BooksCorpus 上訓練。
albert_large_en_uncased 17.68M 24 層 ALBERT 模型,所有輸入均為小寫。在英文維基百科 + BooksCorpus 上訓練。
albert_extra_large_en_uncased 58.72M 24 層 ALBERT 模型,所有輸入均為小寫。在英文維基百科 + BooksCorpus 上訓練。
albert_extra_extra_large_en_uncased 222.60M 12 層 ALBERT 模型,所有輸入均為小寫。在英文維基百科 + BooksCorpus 上訓練。

[來源]

save_to_preset 方法

MaskedLMPreprocessor.save_to_preset(preset_dir)

將預處理器儲存到預設集目錄。

參數

  • preset_dir:本機模型預設集目錄的路徑。

tokenizer 屬性

keras_hub.models.MaskedLMPreprocessor.tokenizer

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