Keras 3 API 文档 / KerasNLP / 预训练模型 / DistilBert / DistilBertPreprocessor 层

DistilBertPreprocessor 层

[源代码]

DistilBertPreprocessor

keras_nlp.models.DistilBertPreprocessor(
    tokenizer, sequence_length=512, truncate="round_robin", **kwargs
)

用于对输入进行分词和打包的 DistilBERT 预处理层。

该预处理层将执行以下三个操作:

  1. 使用 tokenizer 对任意数量的输入段进行分词。
  2. 使用带有适当的 "[CLS]""[SEP]""[PAD]" 标记的 keras_nlp.layers.MultiSegmentPacker 将输入打包在一起。
  3. 构造一个字典,键为 "token_ids""padding_mask",可以直接传递给 DistilBERT 模型。

该层可以直接与 tf.data.Dataset.map 一起使用,以预处理 keras.Model.fit 使用的 (x, y, sample_weight) 格式的字符串数据。

参数

  • tokenizer:一个 keras_nlp.models.DistilBertTokenizer 实例。
  • sequence_length:打包输入的长度。
  • truncate:字符串。用于将一批段列表截断以适应 sequence_length 的算法。该值可以是 round_robinwaterfall: - "round_robin":可用空间以循环方式一次分配一个标记给仍然需要的输入,直到达到限制。 - "waterfall":使用“瀑布”算法进行预算分配,该算法以从左到右的方式分配配额,并填充存储桶,直到预算用完。它支持任意数量的段。

调用参数

  • x:单个字符串序列的张量,或要打包在一起的多个张量序列的元组。输入可以是批量的或非批量的。对于单个序列,原始 Python 输入将转换为张量。对于多个序列,直接传递张量。
  • y:任何标签数据。将原样传递。
  • sample_weight:任何标签权重数据。将原样传递。

示例

直接调用数据上的层。

preprocessor = keras_nlp.models.DistilBertPreprocessor.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.DistilBertPreprocessor(tokenizer)
preprocessor("The quick brown fox jumped.")

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

preprocessor = keras_nlp.models.DistilBertPreprocessor.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 方法

DistilBertPreprocessor.from_preset(preset, **kwargs)

从模型预设实例化 keras_nlp.models.Preprocessor

预设是用于保存和加载预训练模型的配置、权重和其他文件资产的目录。 preset 可以作为以下之一传递:

  1. 内置预设标识符,例如 'bert_base_en'
  2. Kaggle 模型句柄,例如 'kaggle://user/bert/keras/bert_base_en'
  3. Hugging Face 句柄,例如 'hf://user/bert_base_en'
  4. 本地预设目录的路径,例如 './bert_base_en'

对于任何 Preprocessor 子类,您可以运行 cls.presets.keys() 以列出该类上可用的所有内置预设。

由于给定模型通常有多个预处理类,因此应在特定子类上调用此方法,例如 keras_nlp.models.BertPreprocessor.from_preset()

参数

  • preset:字符串。内置预设标识符、Kaggle 模型句柄、Hugging Face 句柄或本地目录的路径。

示例

# 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.BertPreprocessor.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.DistilBertPreprocessor.tokenizer

用于对字符串进行分词的分词器。