BertTextClassifierPreprocessor
类keras_hub.models.BertTextClassifierPreprocessor(
tokenizer, sequence_length=512, truncate="round_robin", **kwargs
)
一个用于分词和打包输入的 BERT 预处理层。
这个预处理层将执行以下三个操作:
tokenizer
对任意数量的输入段进行分词。keras_hub.layers.MultiSegmentPacker
以及相应的 "[CLS]"
、"[SEP]"
和 "[PAD]"
token 将输入打包在一起。"token_ids"
、"segment_ids"
、"padding_mask"
的字典,可以直接传递给 BERT 模型。这个层可以直接与 tf.data.Dataset.map
一起使用,以预处理 keras.Model.fit
使用的 (x, y, sample_weight)
格式的字符串数据。
参数
keras_hub.models.BertTokenizer
实例。sequence_length
的算法。值可以是 round_robin
或 waterfall
"round_robin"
:可用空间以轮询方式一次一个 token 分配给仍然需要的输入,直到达到限制。"waterfall"
:预算分配采用“瀑布”算法,以从左到右的方式分配配额,并填充桶直到预算用尽。它支持任意数量的段。调用参数
示例
直接在数据上调用层。
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
可以是以下之一:
'bert_base_en'
'kaggle://user/bert/keras/bert_base_en'
'hf://user/bert_base_en'
'./bert_base_en'
对于任何 Preprocessor
子类,您可以运行 cls.presets.keys()
来列出该类所有可用的内置预设。
由于一个给定的模型通常有多个预处理类,因此应在特定的子类上调用此方法,例如 keras_hub.models.BertTextClassifierPreprocessor.from_preset()
。
参数
示例
# 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
用于分词字符串的分词器。