BartSeq2SeqLMPreprocessor
類別keras_nlp.models.BartSeq2SeqLMPreprocessor(
tokenizer, encoder_sequence_length=1024, decoder_sequence_length=1024, **kwargs
)
BART Seq2Seq LM 預處理器。
此層用作使用 BART 模型的 seq2seq 任務的預處理器。此類別繼承自 keras_nlp.models.BartPreprocessor
並保留其大部分功能。它與父類別有兩個不同之處
y
(標籤)和 sample_weights
欄位。這兩個欄位都是在內部推斷的,任何傳入的值都將被忽略。參數
keras_nlp.models.BartTokenizer
的實例。呼叫參數
encoder_text
和 decoder_text
作為其鍵。字典中的每個值都應該是單個字串序列的張量。輸入可以是批次或非批次的。原始 Python 輸入將被轉換為張量。None
,因為該層透過將解碼器輸入序列向左移動一步來生成標籤。None
,因為該層透過將填充遮罩向左移動一步來生成標籤權重。範例
直接在資料上呼叫該層
preprocessor = keras_nlp.models.BartPreprocessor.from_preset("bart_base_en")
# Preprocess unbatched inputs.
inputs = {
"encoder_text": "The fox was sleeping.",
"decoder_text": "The fox was awake."
}
preprocessor(inputs)
# Preprocess batched inputs.
inputs = {
"encoder_text": ["The fox was sleeping.", "The lion was quiet."],
"decoder_text": ["The fox was awake.", "The lion was roaring."]
}
preprocessor(inputs)
# Custom vocabulary.
vocab = {
"<s>": 0,
"<pad>": 1,
"</s>": 2,
"Ġafter": 5,
"noon": 6,
"Ġsun": 7,
}
merges = ["Ġ a", "Ġ s", "Ġ n", "e r", "n o", "o n", "Ġs u", "Ġa f", "no on"]
merges += ["Ġsu n", "Ġaf t", "Ġaft er"]
tokenizer = keras_nlp.models.BartTokenizer(
vocabulary=vocab,
merges=merges,
)
preprocessor = keras_nlp.models.BartPreprocessor(
tokenizer=tokenizer,
encoder_sequence_length=20,
decoder_sequence_length=10,
)
inputs = {
"encoder_text": "The fox was sleeping.",
"decoder_text": "The fox was awake."
}
preprocessor(inputs)
使用 tf.data.Dataset
進行映射。
preprocessor = keras_nlp.models.BartPreprocessor.from_preset("bart_base_en")
# Map single sentences.
features = {
"encoder_text": tf.constant(
["The fox was sleeping.", "The lion was quiet."]
),
"decoder_text": tf.constant(
["The fox was awake.", "The lion was roaring."]
)
}
ds = tf.data.Dataset.from_tensor_slices(features)
ds = ds.map(preprocessor, num_parallel_calls=tf.data.AUTOTUNE)
from_preset
方法BartSeq2SeqLMPreprocessor.from_preset(preset, **kwargs)
從模型預設值實例化 keras_nlp.models.Preprocessor
。
預設集 (preset) 是一個包含設定、權重和其他檔案資源的目錄,用於儲存和載入預先訓練的模型。您可以將 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",
)
預設集名稱 | 參數 | 說明 |
---|---|---|
bart_base_en | 139.42M | 6 層 BART 模型,保留大小寫。在 BookCorpus、英文維基百科和 CommonCrawl 上訓練。 |
bart_large_en | 406.29M | 12 層 BART 模型,保留大小寫。在 BookCorpus、英文維基百科和 CommonCrawl 上訓練。 |
bart_large_en_cnn | 406.29M | 在 CNN+DM 摘要資料集上微調的 bart_large_en 骨幹模型。 |
generate_preprocess
方法BartSeq2SeqLMPreprocessor.generate_preprocess(
x, encoder_sequence_length=None, decoder_sequence_length=None, sequence_length=None
)
將編碼器和解碼器輸入字串轉換為用於生成的整數標記輸入。
與呼叫用於訓練的層類似,此方法接受一個包含 "encoder_text"
和 "decoder_text"
的字典,其中包含用於值的字串或張量字串,對輸入進行標記化和打包,並計算遮罩所有未填入填充值的輸入的填充遮罩。
與呼叫用於訓練的層不同,此方法不計算標籤,並且永遠不會將 tokenizer.end_token_id 附加到解碼器序列的末尾(因為預計生成會在輸入的解碼器提示的末尾繼續)。
generate_postprocess
方法BartSeq2SeqLMPreprocessor.generate_postprocess(x)
將整數標記輸出轉換為用於生成的字串。
此方法會反轉 generate_preprocess()
,方法是先移除所有填充以及開始/結束標記,然後將整數序列轉換回字串。
tokenizer
屬性keras_nlp.models.BartSeq2SeqLMPreprocessor.tokenizer
用於對字串進行標記化的標記器。