KerasHub:预训练模型 / API 文档 / 模型架构 / Bert / BertTextClassifierPreprocessor 层

BertTextClassifierPreprocessor 层

[来源]

BertTextClassifierPreprocessor

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

一个 BERT 预处理层,用于分词和打包输入。

此预处理层将执行三项操作

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

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

参数

  • tokenizer: 一个 keras_hub.models.BertTokenizer 实例。
  • sequence_length: 打包输入的长度。
  • truncate: 字符串。用于截断一批段列表以适应 sequence_length 的算法。值可以是 round_robinwaterfall
    • "round_robin": 可用空间一次分配一个 token,以轮询方式分配给仍需要的一些输入,直到达到限制。
    • "waterfall": 预算的分配使用“瀑布”算法完成,该算法以从左到右的方式分配配额并填充桶,直到我们用完预算。它支持任意数量的段。

调用参数

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

示例

直接在数据上调用层。

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

# 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.
vocab = ["[UNK]", "[CLS]", "[SEP]", "[PAD]", "[MASK]"]
vocab += ["The", "quick", "brown", "fox", "jumped", "."]
tokenizer = keras_hub.models.BertTokenizer(vocabulary=vocab)
preprocessor = keras_hub.models.BertTextClassifierPreprocessor(tokenizer)
preprocessor("The quick brown fox jumped.")

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

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

BertTextClassifierPreprocessor.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",
)
预设 参数 描述
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 上训练。

tokenizer 属性

keras_hub.models.BertTextClassifierPreprocessor.tokenizer

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