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

DebertaV3TextClassifierPreprocessor 層

[來源]

DebertaV3TextClassifierPreprocessor 類別

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

一個 DeBERTa 預處理層,用於分詞和封裝輸入。

此預處理層將執行三件事

  • 使用 tokenizer 分詞任意數量的輸入段落。
  • 使用 keras_hub.layers.MultiSegmentPacker 將輸入封裝在一起,並帶有適當的 "[CLS]""[SEP]""[PAD]" 符記。
  • 建構一個字典,其中包含鍵 "token_ids""padding_mask",可以直接傳遞到 DeBERTa 模型。

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

此層的呼叫方法接受三個參數:xysample_weightx 可以是代表單個段落的 Python 字串或張量、代表一批單個段落的 Python 字串列表,或是代表要封裝在一起的多個段落的張量列表。ysample_weight 都是可選的,可以具有任何格式,並且將不經修改地傳遞。

當使用 tf.data 對未標記的字串段落元組進行映射時,應特別小心。tf.data.Dataset.map 將直接將此元組解包到此層的呼叫參數中,而不是將所有參數轉發到 x。為了處理這種情況,建議明確呼叫該層,例如 ds.map(lambda seg1, seg2: preprocessor(x=(seg1, seg2)))

引數

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

範例

直接在資料上呼叫該層。

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

# Tokenize and pack a single sentence.
preprocessor("The quick brown fox jumped.")

# Tokenize a batch of single sentences.
preprocessor(["The quick brown fox jumped.", "Call me Ishmael."])

# Preprocess a batch of sentence pairs.
# When handling multiple sequences, always convert to tensors first!
first = tf.constant(["The quick brown fox jumped.", "Call me Ishmael."])
second = tf.constant(["The fox tripped.", "Oh look, a whale."])
preprocessor((first, second))

# Custom vocabulary.
bytes_io = io.BytesIO()
ds = tf.data.Dataset.from_tensor_slices(["The quick brown fox jumped."])
sentencepiece.SentencePieceTrainer.train(
    sentence_iterator=ds.as_numpy_iterator(),
    model_writer=bytes_io,
    vocab_size=9,
    model_type="WORD",
    pad_id=0,
    bos_id=1,
    eos_id=2,
    unk_id=3,
    pad_piece="[PAD]",
    bos_piece="[CLS]",
    eos_piece="[SEP]",
    unk_piece="[UNK]",
)
tokenizer = keras_hub.models.DebertaV3Tokenizer(
    proto=bytes_io.getvalue(),
)
preprocessor = keras_hub.models.DebertaV3TextClassifierPreprocessor(
    tokenizer
)
preprocessor("The quick brown fox jumped.")

使用 tf.data.Dataset 進行映射。

preprocessor = keras_hub.models.TextClassifierPreprocessor.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."])
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 方法

DebertaV3TextClassifierPreprocessor.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.DebertaV3TextClassifierPreprocessor.tokenizer

用於分詞字串的分詞器。