KerasHub: 预训练模型 / API 文档 / 模型架构 / DistilBert / DistilBertMaskedLMPreprocessor 层

DistilBertMaskedLMPreprocessor 层

[源]

DistilBertMaskedLMPreprocessor

keras_hub.models.DistilBertMaskedLMPreprocessor(
    tokenizer,
    sequence_length=512,
    truncate="round_robin",
    mask_selection_rate=0.15,
    mask_selection_length=96,
    mask_token_rate=0.8,
    random_token_rate=0.1,
    **kwargs
)

用于掩码语言建模任务的 DistilBERT 预处理。

此预处理层将为掩码语言建模任务准备输入。它主要用于 keras_hub.models.DistilBertMaskedLM 任务模型。预处理将分多个步骤进行。

  1. 使用 tokenizer 对任意数量的输入片段进行分词。
  2. 使用 keras_hub.layers.MultiSegmentPacker. 并使用适当的 "[CLS]""[SEP]""[PAD]" 标记将输入打包在一起。
  3. 随机选择非特殊标记进行掩码,由 mask_selection_rate 控制。
  4. 构建一个适用于使用 keras_hub.models.DistilBertMaskedLM 任务模型进行训练的 (x, y, sample_weight) 元组。

参数

  • tokenizer: 一个 keras_hub.models.DistilBertTokenizer 实例。
  • sequence_length: int。打包输入的长度。
  • truncate: string。截断一批片段以使其符合 sequence_length 的算法。值可以是 round_robinwaterfall:- "round_robin":可用空间以轮询方式一次为一个标记分配给仍需要空间的输入,直到达到限制。- "waterfall":使用“瀑布”算法进行预算分配,该算法以从左到右的方式分配配额,并填充存储桶直到预算用完。它支持任意数量的片段。
  • mask_selection_rate: float。输入标记将被动态掩码的概率。
  • mask_selection_length: int。给定样本中掩码标记的最大数量。
  • mask_token_rate: float。选定的标记被替换为掩码标记的概率。
  • random_token_rate: float。选定的标记被词汇表中的随机标记替换的概率。选定的标记将以 1 - mask_token_rate - random_token_rate 的概率保持原样。

调用参数

  • x: 单字符串序列的张量,或由多个张量序列组成的元组,这些序列将被打包在一起。输入可以批量或非批量。对于单序列,原始 python 输入将被转换为张量。对于多个序列,直接传递张量。
  • y: 标签数据。应始终为 None,因为该层会生成标签。
  • sample_weight: 标签权重。应始终为 None,因为该层会生成标签权重。

示例

直接在数据上调用该层。

preprocessor = keras_hub.models.DistilBertMaskedLMPreprocessor.from_preset(
    "distil_bert_base_en_uncased"
)

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

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

# Tokenize and mask sentence pairs.
# In this case, always convert input to tensors before calling the layer.
first = tf.constant(["The quick brown fox jumped.", "Call me Ishmael."])
second = tf.constant(["The fox tripped.", "Oh look, a whale."])
preprocessor((first, second))

tf.data.Dataset 映射。

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

# Map single sentences.
ds = tf.data.Dataset.from_tensor_slices(first)
ds = ds.map(preprocessor, num_parallel_calls=tf.data.AUTOTUNE)

# Map 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 方法

DistilBertMaskedLMPreprocessor.from_preset(
    preset, config_file="preprocessor.json", **kwargs
)

从模型预设实例化 keras_hub.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_hub.models.BertTextClassifierPreprocessor.from_preset()

参数

  • preset: string。内置预设标识符、Kaggle 模型句柄、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",
)
Preset 参数 描述
distil_bert_base_en 65.19M 保持大小写的 6 层 DistilBERT 模型。在 English Wikipedia + BooksCorpus 上使用 BERT 作为教师模型进行训练。
distil_bert_base_en_uncased 66.36M 所有输入均为小写字母的 6 层 DistilBERT 模型。在 English Wikipedia + BooksCorpus 上使用 BERT 作为教师模型进行训练。
distil_bert_base_multi 134.73M 保持大小写的 6 层 DistilBERT 模型。在 104 种语言的维基百科上进行训练。

tokenizer 属性

keras_hub.models.DistilBertMaskedLMPreprocessor.tokenizer

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