KerasHub:預訓練模型 / API 文件 / 模型架構 / DistilBert / DistilBertTextClassifierPreprocessor 層

DistilBertTextClassifierPreprocessor 層

[source]

DistilBertTextClassifierPreprocessor 類別

keras_hub.models.DistilBertTextClassifierPreprocessor(
    tokenizer, sequence_length=512, truncate="round_robin", **kwargs
)

一個 DistilBERT 預處理層,用於將輸入 Token 化並打包。

此預處理層將執行三件事

  1. 使用 tokenizer 將任意數量的輸入片段 Token 化。
  2. 使用 keras_hub.layers.MultiSegmentPacker 將輸入打包在一起,並帶有適當的 "[CLS]""[SEP]""[PAD]" Token。
  3. 建構一個字典,其中包含鍵 "token_ids""padding_mask",可以直接傳遞給 DistilBERT 模型。

此層可以直接與 tf.data.Dataset.map 一起使用,以預處理 (x, y, sample_weight) 格式的字串資料,該格式由 keras.Model.fit 使用。

引數

  • tokenizerkeras_hub.models.DistilBertTokenizer 實例。
  • sequence_length:打包輸入的長度。
  • truncate:字串。將批次片段列表截斷以符合 sequence_length 的演算法。值可以是 round_robinwaterfall:- "round_robin":可用空間以循環方式一次分配一個 Token 給仍然需要的一些輸入,直到達到限制。- "waterfall":預算的分配是使用「瀑布式」演算法完成的,該演算法以從左到右的方式分配配額,並填充儲存桶,直到預算用完。它支援任意數量的片段。

呼叫引數

  • x:單個字串序列的張量,或要打包在一起的多個張量序列的元組。輸入可以是批次的或非批次的。對於單個序列,原始 Python 輸入將轉換為張量。對於多個序列,直接傳遞張量。
  • y:任何標籤資料。將不經修改地傳遞。
  • sample_weight:任何標籤權重資料。將不經修改地傳遞。

範例

直接在資料上呼叫層。

preprocessor = keras_hub.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_hub.models.DistilBertTokenizer(vocabulary=vocab)
preprocessor = keras_hub.models.DistilBertTextClassifierPreprocessor(
    tokenizer
)
preprocessor("The quick brown fox jumped.")

使用 tf.data.Dataset 進行 Mapping。

preprocessor = keras_hub.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,
)

[source]

from_preset 方法

DistilBertTextClassifierPreprocessor.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",
)
預設 參數 描述
distil_bert_base_en 65.19M 6 層 DistilBERT 模型,其中保留大小寫。使用 BERT 作為教師模型,在英文維基百科 + BooksCorpus 上訓練。
distil_bert_base_en_uncased 66.36M 6 層 DistilBERT 模型,其中所有輸入均為小寫。使用 BERT 作為教師模型,在英文維基百科 + BooksCorpus 上訓練。
distil_bert_base_multi 134.73M 6 層 DistilBERT 模型,其中保留大小寫。在 104 種語言的維基百科上訓練

tokenizer 屬性

keras_hub.models.DistilBertTextClassifierPreprocessor.tokenizer

用於 Token 化字串的 Tokenizer。