KerasHub:預訓練模型 / API 文件 / 模型架構 / DebertaV3 / DebertaV3MaskedLMPreprocessor 層

DebertaV3MaskedLMPreprocessor 層

[來源]

DebertaV3MaskedLMPreprocessor 類別

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

用於遮罩語言模型任務的 DeBERTa 預處理。

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

  • 使用 tokenizer 對任意數量的輸入段落進行分詞。
  • 將輸入與適當的 "<s>""</s>""<pad>" 標記打包在一起,即在整個序列的開頭添加單個 "<s>",在每個段落之間添加 "</s></s>",並在整個序列的末尾添加 "</s>"
  • 隨機選擇非特殊標記進行遮罩,由 mask_selection_rate 控制。
  • 建構適用於使用 keras_hub.models.DebertaV3MaskedLM 任務模型進行訓練的 (x, y, sample_weight) 元組。

引數

  • tokenizerkeras_hub.models.DebertaV3Tokenizer 實例。
  • sequence_length:打包輸入的長度。
  • mask_selection_rate:輸入標記將被動態遮罩的機率。
  • mask_selection_length:圖層支援的遮罩標記的最大數量。
  • mask_token_rate:浮點數。mask_token_rate 必須介於 0 和 1 之間,表示 mask_token 取代選定用於遮罩的標記的頻率。預設為 0.8
  • random_token_rate:浮點數。random_token_rate 必須介於 0 和 1 之間,表示隨機標記取代選定用於遮罩的標記的頻率。注意:mask_token_rate + random_token_rate <= 1,對於 (1 - mask_token_rate - random_token_rate),標記將不會變更。預設為 0.1
  • truncate:字串。將批次段落列表截斷以符合 sequence_length 的演算法。值可以是 round_robinwaterfall
    • "round_robin":可用空間以循環方式一次分配一個標記給仍然需要一些標記的輸入,直到達到限制。
    • "waterfall":預算的分配是使用「瀑布」演算法完成的,該演算法以從左到右的方式分配配額並填滿儲存桶,直到我們用完預算。它支援任意數量的段落。

範例

直接在資料上呼叫圖層。

preprocessor = keras_hub.models.DebertaV3MaskedLMPreprocessor.from_preset(
    "deberta_v3_base_en"
)

# 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_hub.models.DebertaV3MaskedLMPreprocessor.from_preset(
    "deberta_v3_base_en"
)

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 方法

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

從模型預設實例化 keras_hub.models.Preprocessor

預設是配置、權重和其他檔案資產的目錄,用於儲存和載入預訓練模型。preset 可以作為以下其中之一傳遞

  1. 內建預設識別碼,例如 'bert_base_en'
  2. Kaggle Models 句柄,例如 '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 Models 句柄、Hugging Face 句柄或本機目錄的路徑。

範例

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

# Load a preprocessor for Bert classification.
preprocessor = keras_hub.models.TextClassifierPreprocessor.from_preset(
    "bert_base_en",
)
預設 參數 描述
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_base_multi 278.22M 12 層 DeBERTaV3 模型,其中保留大小寫。在 2.5TB 多語言 CC100 資料集上訓練。
deberta_v3_large_en 434.01M 24 層 DeBERTaV3 模型,其中保留大小寫。在英文維基百科、BookCorpus 和 OpenWebText 上訓練。

tokenizer 屬性

keras_hub.models.DebertaV3MaskedLMPreprocessor.tokenizer

用於標記字串的分詞器。