KerasHub:预训练模型 / API 文档 / 建模 API / MaskedLMPreprocessor

MaskedLMPreprocessor

[source]

MaskedLMPreprocessor

keras_hub.models.MaskedLMPreprocessor(
    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
)

用于掩码语言建模预处理层的基类。

MaskedLMPreprocessor 任务包装了一个 keras_hub.tokenizer.Tokenizer,以创建一个用于掩码语言建模任务的预处理层。它旨在与 keras.models.MaskedLM 任务配对使用。

所有 MaskedLMPreprocessor 接受单个输入。这可以是单个字符串、一批字符串,或者应组合成单个序列的字符串段批次的元组。请参阅以下示例。这些输入将被分词、组合,并在序列中随机掩码。

此层将始终输出一个 (x, y, sample_weight) 元组,其中 x 是一个字典,包含掩码后的分词输入,y 包含在 x 中被掩码的标记,而 sample_weight 标记 y 中包含填充值的位置。 x 的确切内容将根据所使用的模型而有所不同。

所有 MaskedLMPreprocessor 任务都包含一个 from_preset() 构造函数,可用于加载预训练的配置和词汇表。您可以直接在此基类上调用 from_preset() 构造函数,在这种情况下,将自动实例化适合您模型的正确类。

示例。

preprocessor = keras_hub.models.MaskedLMPreprocessor.from_preset(
    "bert_base_en_uncased",
    sequence_length=256, # Optional.
)

# Tokenize, mask and pack a single sentence.
x = "The quick brown fox jumped."
x, y, sample_weight = preprocessor(x)

# Preprocess a batch of labeled sentence pairs.
first = ["The quick brown fox jumped.", "Call me Ishmael."]
second = ["The fox tripped.", "Oh look, a whale."]
x, y, sample_weight = preprocessor((first, second))

# With a [`tf.data.Dataset`](https://tensorflowcn.cn/api_docs/python/tf/data/Dataset).
ds = tf.data.Dataset.from_tensor_slices((first, second))
ds = ds.map(preprocessor, num_parallel_calls=tf.data.AUTOTUNE)

[source]

from_preset 方法

MaskedLMPreprocessor.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",
)
预设 参数 描述
albert_base_en_uncased 11.68M 12 层 ALBERT 模型,其中所有输入均为小写。在英文维基百科 + BooksCorpus 上训练。
albert_large_en_uncased 17.68M 24 层 ALBERT 模型,其中所有输入均为小写。在英文维基百科 + BooksCorpus 上训练。
albert_extra_large_en_uncased 58.72M 24 层 ALBERT 模型,其中所有输入均为小写。在英文维基百科 + BooksCorpus 上训练。
albert_extra_extra_large_en_uncased 222.60M 12 层 ALBERT 模型,其中所有输入均为小写。在英文维基百科 + BooksCorpus 上训练。
bert_tiny_en_uncased 4.39M 2 层 BERT 模型,其中所有输入均为小写。在英文维基百科 + BooksCorpus 上训练。
bert_tiny_en_uncased_sst2 4.39M 在 SST-2 情感分析数据集上微调的 bert_tiny_en_uncased 主干模型。
bert_small_en_uncased 28.76M 4 层 BERT 模型,其中所有输入均为小写。在英文维基百科 + BooksCorpus 上训练。
bert_medium_en_uncased 41.37M 8 层 BERT 模型,其中所有输入均为小写。在英文维基百科 + BooksCorpus 上训练。
bert_base_zh 102.27M 12 层 BERT 模型。在中文维基百科上训练。
bert_base_en 108.31M 12 层 BERT 模型,其中保留大小写。在英文维基百科 + BooksCorpus 上训练。
bert_base_en_uncased 109.48M 12 层 BERT 模型,其中所有输入均为小写。在英文维基百科 + BooksCorpus 上训练。
bert_base_multi 177.85M 12 层 BERT 模型,其中保留大小写。在 104 种语言的维基百科上训练
bert_large_en 333.58M 24 层 BERT 模型,其中保留大小写。在英文维基百科 + BooksCorpus 上训练。
bert_large_en_uncased 335.14M 24 层 BERT 模型,其中所有输入均为小写。在英文维基百科 + BooksCorpus 上训练。
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 上训练。
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 种语言的维基百科上训练
f_net_base_en 82.86M 12 层 FNet 模型,其中保留大小写。在 C4 数据集上训练。
f_net_large_en 236.95M 24 层 FNet 模型,其中保留大小写。在 C4 数据集上训练。
roberta_base_en 124.05M 12 层 RoBERTa 模型,其中保留大小写。在英文维基百科、BooksCorpus、CommonCraw 和 OpenWebText 上训练。
roberta_large_en 354.31M 24 层 RoBERTa 模型,其中保留大小写。在英文维基百科、BooksCorpus、CommonCraw 和 OpenWebText 上训练。
xlm_roberta_base_multi 277.45M 12 层 XLM-RoBERTa 模型,其中保留大小写。在 100 种语言的 CommonCrawl 上训练。
xlm_roberta_large_multi 558.84M 24 层 XLM-RoBERTa 模型,其中保留大小写。在 100 种语言的 CommonCrawl 上训练。

[source]

save_to_preset 方法

MaskedLMPreprocessor.save_to_preset(preset_dir)

将预处理器保存到预设目录。

参数

  • preset_dir:本地模型预设目录的路径。

tokenizer 属性

keras_hub.models.MaskedLMPreprocessor.tokenizer

用于分词字符串的分词器。