GPT2CausalLMPreprocessor
类keras_hub.models.GPT2CausalLMPreprocessor(
tokenizer, sequence_length=1024, add_start_token=True, add_end_token=True, **kwargs
)
GPT2 因果语言模型预处理器。
此预处理层旨在与 keras_hub.models.GPT2CausalLM
一起使用。默认情况下,它接收字符串批次,并以 (x, y, sample_weight)
格式返回输出,其中 y
标签是 x
序列中的下一个 token ID。
对于生成任务,该层还公开了两个方法 generate_preprocess()
和 generate_postprocess()
。当此预处理器附加到 keras_hub.models.GPT2CausalLM
实例时,这些方法将在 generate()
中隐式调用。它们也可以单独调用(例如,在单独的进程中预计算用于生成的预处理输入)。
参数
keras_hub.models.GPT2Tokenizer
实例。True
,预处理器会将分词器起始 token 添加到每个输入序列的开头。True
,预处理器会将分词器结束 token 添加到每个输入序列的末尾。调用参数
tf.Tensor
或 Python 字符串列表。None
,因为该层会生成标签。None
,因为该层会生成标签权重。sequence_length
。示例
# Load the preprocessor from a preset.
preprocessor = keras_hub.models.GPT2CausalLMPreprocessor.from_preset(
"gpt2_base_en"
)
# Tokenize and pack a single sentence.
sentence = tf.constant("League of legends")
preprocessor(sentence)
# Same output.
preprocessor("League of legends")
# Tokenize a batch of sentences.
sentences = tf.constant(["Taco tuesday", "Fish taco please!"])
preprocessor(sentences)
# Same output.
preprocessor(["Taco tuesday", "Fish taco please!"])
# Map a dataset to preprocess a single sentence.
features = tf.constant(
[
"Avatar 2 is amazing!",
"Well, I am not sure.",
]
)
labels = tf.constant([1, 0])
ds = tf.data.Dataset.from_tensor_slices((features, labels))
ds = ds.map(preprocessor, num_parallel_calls=tf.data.AUTOTUNE)
# Map a dataset to preprocess unlabled sentences.
ds = tf.data.Dataset.from_tensor_slices(features)
ds = ds.map(preprocessor, num_parallel_calls=tf.data.AUTOTUNE)
from_preset
方法GPT2CausalLMPreprocessor.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",
)
预设 | 参数 | 描述 |
---|---|---|
gpt2_base_en | 124.44M | 保留大小写的 12 层 GPT-2 模型。在 WebText 上训练。 |
gpt2_base_en_cnn_dailymail | 124.44M | 保留大小写的 12 层 GPT-2 模型。在 CNN/DailyMail 摘要数据集上微调。 |
gpt2_medium_en | 354.82M | 保留大小写的 24 层 GPT-2 模型。在 WebText 上训练。 |
gpt2_large_en | 774.03M | 保留大小写的 36 层 GPT-2 模型。在 WebText 上训练。 |
gpt2_extra_large_en | 1.56B | 保留大小写的 48 层 GPT-2 模型。在 WebText 上训练。 |
generate_preprocess
方法GPT2CausalLMPreprocessor.generate_preprocess(x, sequence_length=None)
将字符串转换为用于生成的整数 token 输入。
与调用层进行训练类似,此方法接收字符串或 tensor 字符串,对输入进行分词和打包,并计算填充掩码,以屏蔽所有未用填充值填充的输入。
与调用层进行训练不同,此方法不计算标签,也不会在序列末尾附加 tokenizer.end_token_id
(因为预期生成将在输入提示符的末尾继续)。
generate_postprocess
方法GPT2CausalLMPreprocessor.generate_postprocess(x)
将整数 token 输出转换为字符串,用于生成。
此方法通过先移除所有填充和起始/结束 token,然后将整数序列转换回字符串,来逆转 generate_preprocess()
的过程。
tokenizer
属性keras_hub.models.GPT2CausalLMPreprocessor.tokenizer
用于对字符串进行分词的分词器。