GPT2CausalLMPreprocessor
類別keras_nlp.models.GPT2CausalLMPreprocessor(
tokenizer, sequence_length=1024, add_start_token=True, add_end_token=True, **kwargs
)
GPT2 因果語言模型預處理器。
此預處理層旨在與 keras_nlp.models.GPT2CausalLM
搭配使用。預設情況下,它會接收批次的字串,並以 (x, y, sample_weight)
格式返回輸出,其中 y
標籤是 x
序列中的下一個詞彙 ID。
為了用於生成,該層還公開了兩個方法 generate_preprocess()
和 generate_postprocess()
。當此預處理器附加到 keras_nlp.models.GPT2CausalLM
實例時,這些方法將在 generate()
中隱式調用。它們也可以獨立調用(例如,在單獨的進程中預先計算用於生成的預處理輸入)。
參數
keras_nlp.models.GPT2Tokenizer
實例。True
,預處理器會在每個輸入序列的開頭添加分詞器的起始詞彙。True
,預處理器會在每個輸入序列的結尾添加分詞器的結束詞彙。呼叫參數
tf.Tensor
或 Python 字串列表。None
,因為該層會生成標籤。None
,因為該層會生成標籤權重。sequence_length
。範例
# Load the preprocessor from a preset.
preprocessor = keras_nlp.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, **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",
)
預設設定檔名稱 | 參數 | 說明 |
---|---|---|
gpt2_base_en | 124.44M | 12 層 GPT-2 模型,保留大小寫。在 WebText 上訓練。 |
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 上訓練。 |
gpt2_base_en_cnn_dailymail | 124.44M | 12 層 GPT-2 模型,保留大小寫。在 CNN/DailyMail 摘要數據集上微調。 |
generate_preprocess
方法GPT2CausalLMPreprocessor.generate_preprocess(x, sequence_length=None)
將字串轉換為用於生成的整數標記輸入。
與呼叫用於訓練的層類似,此方法接收字串或張量字串,對輸入進行標記化和打包,並計算一個填充遮罩,遮罩所有未填充填充值的輸入。
與呼叫用於訓練的層不同,此方法不計算標籤,並且永遠不會在序列末尾附加 tokenizer.end_token_id
(因為預計生成會在輸入提示的末尾繼續)。
generate_postprocess
方法GPT2CausalLMPreprocessor.generate_postprocess(x)
將整數標記輸出轉換為用於生成的字串。
此方法會反轉 generate_preprocess()
,方法是先移除所有填充和開始/結束標記,然後將整數序列轉換回字串。
tokenizer
屬性keras_nlp.models.GPT2CausalLMPreprocessor.tokenizer
用於對字串進行標記化的標記器。