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)))

参数

  • tokenizer:一个 keras_hub.models.DebertaV3Tokenizer 实例。
  • sequence_length:打包输入的长度。
  • truncate:string。用于将一批片段列表截断以符合 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:string。内置预设标识符、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

用于标记化字符串的分词器。