DistilBertTextClassifierPreprocessor
類別keras_nlp.models.DistilBertTextClassifierPreprocessor(
tokenizer, sequence_length=512, truncate="round_robin", **kwargs
)
一個用於對輸入進行標記化和打包的 DistilBERT 預處理圖層。
此預處理圖層將執行三項操作
tokenizer
對任意數量的輸入片段進行標記化。keras_nlp.layers.MultiSegmentPacker
將輸入打包在一起,並使用適當的 "[CLS]"
、"[SEP]"
和 "[PAD]"
標記。"token_ids"
和 "padding_mask"
的字典,可以直接傳遞給 DistilBERT 模型。此圖層可以直接與 tf.data.Dataset.map
一起使用,以預處理 keras.Model.fit
使用的 (x, y, sample_weight)
格式的字串資料。
參數
keras_nlp.models.DistilBertTokenizer
實例。sequence_length
的演算法。值可以是 round_robin
或 waterfall
: - "round_robin"
:可用空間以循環方式一次分配一個標記給仍然需要的輸入,直到達到限制。 - "waterfall"
:使用「瀑布」演算法進行預算分配,該演算法以從左到右的方式分配配額,並填滿桶,直到預算用盡。它支援任意數量的片段。呼叫參數
範例
直接呼叫資料上的層。
preprocessor = keras_nlp.models.TextClassifierPreprocessor.from_preset(
"distil_bert_base_en_uncased"
)
preprocessor(["The quick brown fox jumped.", "Call me Ishmael."])
# Custom vocabulary.
vocab = ["[UNK]", "[CLS]", "[SEP]", "[PAD]", "[MASK]"]
vocab += ["The", "quick", "brown", "fox", "jumped", "."]
tokenizer = keras_nlp.models.DistilBertTokenizer(vocabulary=vocab)
preprocessor = keras_nlp.models.DistilBertTextClassifierPreprocessor(
tokenizer
)
preprocessor("The quick brown fox jumped.")
使用 tf.data.Dataset
進行映射。
preprocessor = keras_nlp.models.TextClassifierPreprocessor.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."])
label = tf.constant([1, 1])
# Map labeled single sentences.
ds = tf.data.Dataset.from_tensor_slices((first, label))
ds = ds.map(preprocessor, num_parallel_calls=tf.data.AUTOTUNE)
# Map unlabeled single sentences.
ds = tf.data.Dataset.from_tensor_slices(first)
ds = ds.map(preprocessor, num_parallel_calls=tf.data.AUTOTUNE)
# Map labeled sentence pairs.
ds = tf.data.Dataset.from_tensor_slices(((first, second), label))
ds = ds.map(preprocessor, num_parallel_calls=tf.data.AUTOTUNE)
# Map unlabeled 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
方法DistilBertTextClassifierPreprocessor.from_preset(preset, **kwargs)
從模型預設集實例化 keras_nlp.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_nlp.models.BertTextClassifierPreprocessor.from_preset()
。
參數
範例
# 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",
)
預設集名稱 | 參數 | 說明 |
---|---|---|
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 種語言的維基百科上進行訓練 |
tokenizer
屬性keras_nlp.models.DistilBertTextClassifierPreprocessor.tokenizer
用於將字串標記化的標記器。