AlbertTextClassifierPreprocessor 类keras_nlp.models.AlbertTextClassifierPreprocessor(
tokenizer, sequence_length=512, truncate="round_robin", **kwargs
)
一个 ALBERT 预处理层,用于分词和打包输入。
此预处理层将执行三件事
tokenizer 对任意数量的输入段进行分词。keras_nlp.layers.MultiSegmentPacker 将输入打包在一起,并使用适当的 "[CLS]"、"[SEP]" 和 "<pad>" 符号。"token_ids"、"segment_ids" 和 "padding_mask",可以将其直接传递给 keras_nlp.models.AlbertBackbone。此层可以直接与 tf.data.Dataset.map 一起使用,以预处理 keras.Model.fit 使用的 (x, y, sample_weight) 格式的字符串数据。
此层的调用方法接受三个参数:x、y 和 sample_weight。x 可以是表示单个段的 Python 字符串或张量,可以是表示一批单个段的 Python 字符串列表,也可以是表示要打包在一起的多个段的张量列表。y 和 sample_weight 都是可选的,可以具有任何格式,并且将直接传递,不会被修改。
在使用 tf.data 对未标记的字符串段元组进行映射时,应特别注意。 tf.data.Dataset.map 将直接将此元组解包到此层的调用参数中,而不是将所有参数转发到 x。为了处理这种情况,建议明确调用此层,例如 ds.map(lambda seg1, seg2: preprocessor(x=(seg1, seg2)))。
参数
keras_nlp.models.AlbertTokenizer 实例。sequence_length 的算法。值可以是 round_robin 或 waterfall"round_robin": 可用空间将一次分配一个符号,以循环方式分配给仍然需要一些空间的输入,直到达到限制为止。"waterfall": 使用“瀑布”算法分配预算,该算法以从左到右的方式分配配额,并填满桶,直到预算用完。它支持任意数量的段。示例
直接在数据上调用此层。
preprocessor = keras_nlp.models.TextClassifierPreprocessor.from_preset(
"albert_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.
bytes_io = io.BytesIO()
ds = tf.data.Dataset.from_tensor_slices(["The quick brown fox jumped."])
sentencepiece.SentencePieceTrainer.train(
sentence_iterator=ds.as_numpy_iterator(),
model_writer=bytes_io,
vocab_size=10,
model_type="WORD",
pad_id=0,
unk_id=1,
bos_id=2,
eos_id=3,
pad_piece="<pad>",
unk_piece="<unk>",
bos_piece="[CLS]",
eos_piece="[SEP]",
user_defined_symbols="[MASK]",
)
tokenizer = keras_nlp.models.AlbertTokenizer(
proto=bytes_io.getvalue(),
)
preprocessor = keras_nlp.models.AlbertTextClassifierPreprocessor(tokenizer)
preprocessor("The quick brown fox jumped.")
使用 tf.data.Dataset 进行映射。
preprocessor = keras_nlp.models.TextClassifierPreprocessor.from_preset(
"albert_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 方法AlbertTextClassifierPreprocessor.from_preset(preset, **kwargs)
从模型预设实例化一个 keras_nlp.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_nlp.models.BertTextClassifierPreprocessor.from_preset()。
参数
示例
# 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.BertTextClassifierPreprocessor.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 上训练。 |
tokenizer 属性keras_nlp.models.AlbertTextClassifierPreprocessor.tokenizer
用于对字符串进行分词的分词器。