XLMRobertaTextClassifierPreprocessor
类keras_hub.models.XLMRobertaTextClassifierPreprocessor(
tokenizer, sequence_length=512, truncate="round_robin", **kwargs
)
一个 XLM-RoBERTa 预处理层,用于对输入进行分词和打包。
此预处理层将执行三件事:
tokenizer
对任意数量的输入段进行分词。keras_hub.layers.MultiSegmentPacker
打包输入,并附带适当的 "<s>"
、"</s>"
和 "<pad>"
标记,即在整个序列的开头添加一个 "<s>"
,在每个分段的末尾(除了最后一个)添加 "</s></s>"
,并在整个序列的末尾添加一个 "</s>"
。"token_ids"
和 "padding_mask"
的字典,可以直接传递给 XLM-RoBERTa 模型。此层可直接与 tf.data.Dataset.map
结合使用,以预处理 keras.Model.fit
使用的 (x, y, sample_weight)
格式的字符串数据。
参数
keras_hub.tokenizers.XLMRobertaTokenizer
实例。sequence_length
的算法。该值可以是 round_robin
或 waterfall
: - "round_robin"
:可用空间以轮询方式一次分配一个标记给仍然需要标记的输入,直到达到限制。 - "waterfall"
:预算分配使用“瀑布”算法完成,该算法从左到右分配配额并填充桶,直到预算用尽。它支持任意数量的分段。调用参数
示例
直接在数据上调用该层。
preprocessor = keras_hub.models.TextClassifierPreprocessor.from_preset(
"xlm_roberta_base_multi"
)
# Tokenize and pack a single sentence.
preprocessor("The quick brown fox jumped.")
# Tokenize a batch of single sentences.
preprocessor(["The quick brown fox jumped.", "اسمي اسماعيل"])
# Preprocess a batch of sentence pairs.
# When handling multiple sequences, always convert to tensors first!
first = tf.constant(["The quick brown fox jumped.", "اسمي اسماعيل"])
second = tf.constant(["The fox tripped.", "الأسد ملك الغابة"])
preprocessor((first, second))
# Custom vocabulary.
def train_sentencepiece(ds, vocab_size):
bytes_io = io.BytesIO()
sentencepiece.SentencePieceTrainer.train(
sentence_iterator=ds.as_numpy_iterator(),
model_writer=bytes_io,
vocab_size=vocab_size,
model_type="WORD",
unk_id=0,
bos_id=1,
eos_id=2,
)
return bytes_io.getvalue()
ds = tf.data.Dataset.from_tensor_slices(
["the quick brown fox", "the earth is round"]
)
proto = train_sentencepiece(ds, vocab_size=10)
tokenizer = keras_hub.models.XLMRobertaTokenizer(proto=proto)
preprocessor = keras_hub.models.XLMRobertaTextClassifierPreprocessor(
tokenizer
)
preprocessor("The quick brown fox jumped.")
使用 tf.data.Dataset
进行映射。
preprocessor = keras_hub.models.TextClassifierPreprocessor.from_preset(
"xlm_roberta_base_multi"
)
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
方法XLMRobertaTextClassifierPreprocessor.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",
)
预设 | 参数 | 描述 |
---|---|---|
xlm_roberta_base_multi | 2.7745亿 | 12 层 XLM-RoBERTa 模型,大小写保持不变。在 100 种语言的 CommonCrawl 上训练。 |
xlm_roberta_large_multi | 5.5884亿 | 24 层 XLM-RoBERTa 模型,大小写保持不变。在 100 种语言的 CommonCrawl 上训练。 |
tokenizer
属性keras_hub.models.XLMRobertaTextClassifierPreprocessor.tokenizer
用于对字符串进行分词的分词器。