DistilBertMaskedLMPreprocessor
類別keras_hub.models.DistilBertMaskedLMPreprocessor(
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
)
用於遮罩語言模型任務的 DistilBERT 預處理。
此預處理層將為遮罩語言模型任務準備輸入。它主要用於 keras_hub.models.DistilBertMaskedLM
任務模型。預處理將分多個步驟進行。
tokenizer
對任意數量的輸入片段進行分詞。keras_hub.layers.MultiSegmentPacker
將輸入封裝在一起,並使用適當的 "[CLS]"
、"[SEP]"
和 "[PAD]"
符號。mask_selection_rate
控制。keras_hub.models.DistilBertMaskedLM
任務模型進行訓練的 (x, y, sample_weight)
元組。參數
keras_hub.models.DistilBertTokenizer
實例。sequence_length
的演算法。值可以是 round_robin
或 waterfall
:- "round_robin"
:可用空間以循環分配的方式一次分配一個符號給仍然需要空間的輸入,直到達到限制。 - "waterfall"
:預算的分配使用「瀑布式」演算法,該演算法以從左到右的方式分配配額並填滿儲存桶,直到預算用完。它支援任意數量的片段。1 - mask_token_rate - random_token_rate
的機率保持不變。呼叫參數
None
,因為該層會產生標籤。None
,因為該層會產生標籤權重。範例
直接在資料上呼叫層。
preprocessor = keras_hub.models.DistilBertMaskedLMPreprocessor.from_preset(
"distil_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_hub.models.DistilBertMaskedLMPreprocessor.from_preset(
"distil_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
方法DistilBertMaskedLMPreprocessor.from_preset(
preset, config_file="preprocessor.json", **kwargs
)
從模型預設配置實例化 keras_hub.models.Preprocessor
。
預設配置是一個包含配置、權重和其他檔案資產的目錄,用於儲存和載入預訓練模型。 preset
可以作為以下其中之一傳遞
'bert_base_en'
'kaggle://user/bert/keras/bert_base_en'
'hf://user/bert_base_en'
'./bert_base_en'
對於任何 Preprocessor
子類別,您可以執行 cls.presets.keys()
以列出該類別上所有可用的內建預設配置。
由於給定模型通常有多個預處理類別,因此應在特定的子類別(例如 keras_hub.models.BertTextClassifierPreprocessor.from_preset()
)上呼叫此方法。
參數
範例
# 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",
)
預設配置 | 參數 | 描述 |
---|---|---|
distil_bert_base_en | 65.19M | 6 層 DistilBERT 模型,其中保留大小寫。使用 BERT 作為教師模型在 English Wikipedia + BooksCorpus 上訓練。 |
distil_bert_base_en_uncased | 66.36M | 6 層 DistilBERT 模型,其中所有輸入均為小寫。使用 BERT 作為教師模型在 English Wikipedia + BooksCorpus 上訓練。 |
distil_bert_base_multi | 134.73M | 6 層 DistilBERT 模型,其中保留大小寫。在 104 種語言的 Wikipedia 上訓練 |
tokenizer
屬性keras_hub.models.DistilBertMaskedLMPreprocessor.tokenizer
用於對字串進行分詞的分詞器。