BartSeq2SeqLM
類別keras_nlp.models.BartSeq2SeqLM(backbone, preprocessor=None, **kwargs)
用於序列到序列語言建模的端到端 BART 模型。
序列到序列語言模型 (LM) 是一種編碼器-解碼器模型,用於條件式文字生成。編碼器會接收一個「上下文」文字(輸入到編碼器),而解碼器會根據編碼器輸入和先前的詞彙預測下一個詞彙。您可以微調 BartSeq2SeqLM
以生成任何序列到序列任務(例如翻譯或摘要)的文字。
此模型有一個 generate()
方法,可以根據編碼器輸入和解碼器的選用提示生成文字。使用的生成策略由傳遞給 compile()
的額外 sampler
參數控制。您可以使用不同的 keras_nlp.samplers
物件重新編譯模型以控制生成。預設情況下,將使用 "top_k"
採樣。
此模型可以選擇性地配置一個 preprocessor
層,在這種情況下,它將在 fit()
、predict()
、evaluate()
和 generate()
期間自動對字串輸入應用預處理。使用 from_preset()
建立模型時,預設會執行此操作。
免責聲明:預訓練模型按「原樣」提供,不提供任何形式的保證或條件。基礎模型由第三方提供,並受制於單獨的授權,請參閱 此處。
參數
keras_nlp.models.BartBackbone
實例。keras_nlp.models.BartSeq2SeqLMPreprocessor
或 None
。如果為 None
,則此模型將不會應用預處理,並且應在呼叫模型之前對輸入進行預處理。範例
使用 generate()
函數,給定輸入上下文來生成文字。
bart_lm = keras_nlp.models.BartSeq2SeqLM.from_preset("bart_base_en")
bart_lm.generate("The quick brown fox", max_length=30)
# Generate with batched inputs.
bart_lm.generate(["The quick brown fox", "The whale"], max_length=30)
使用自訂取樣器編譯 generate()
函數。
bart_lm = keras_nlp.models.BartSeq2SeqLM.from_preset("bart_base_en")
bart_lm.compile(sampler="greedy")
bart_lm.generate("The quick brown fox", max_length=30)
將 generate()
與編碼器輸入和不完整的解碼器輸入(提示)一起使用。
bart_lm = keras_nlp.models.BartSeq2SeqLM.from_preset("bart_base_en")
bart_lm.generate(
{
"encoder_text": "The quick brown fox",
"decoder_text": "The fast"
}
)
在沒有預處理的情況下使用 generate()
。
# Preprocessed inputs, with encoder inputs corresponding to
# "The quick brown fox", and the decoder inputs to "The fast". Use
# `"padding_mask"` to indicate values that should not be overridden.
prompt = {
"encoder_token_ids": np.array([[0, 133, 2119, 6219, 23602, 2, 1, 1]]),
"encoder_padding_mask": np.array(
[[True, True, True, True, True, True, False, False]]
),
"decoder_token_ids": np.array([[2, 0, 133, 1769, 2, 1, 1]]),
"decoder_padding_mask": np.array([[True, True, True, True, False, False]])
}
bart_lm = keras_nlp.models.BartSeq2SeqLM.from_preset(
"bart_base_en",
preprocessor=None,
)
bart_lm.generate(prompt)
在單一批次上呼叫 fit()
。
features = {
"encoder_text": ["The quick brown fox jumped.", "I forgot my homework."],
"decoder_text": ["The fast hazel fox leapt.", "I forgot my assignment."]
}
bart_lm = keras_nlp.models.BartSeq2SeqLM.from_preset("bart_base_en")
bart_lm.fit(x=features, batch_size=2)
在沒有預處理的情況下呼叫 fit()
。
x = {
"encoder_token_ids": np.array([[0, 133, 2119, 2, 1]] * 2),
"encoder_padding_mask": np.array([[1, 1, 1, 1, 0]] * 2),
"decoder_token_ids": np.array([[2, 0, 133, 1769, 2]] * 2),
"decoder_padding_mask": np.array([[1, 1, 1, 1, 1]] * 2),
}
y = np.array([[0, 133, 1769, 2, 1]] * 2)
sw = np.array([[1, 1, 1, 1, 0]] * 2)
bart_lm = keras_nlp.models.BartSeq2SeqLM.from_preset(
"bart_base_en",
preprocessor=None,
)
bart_lm.fit(x=x, y=y, sample_weight=sw, batch_size=2)
自訂骨幹和詞彙。
features = {
"encoder_text": [" afternoon sun"],
"decoder_text": ["noon sun"],
}
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.BartSeq2SeqLMPreprocessor(
tokenizer=tokenizer,
encoder_sequence_length=128,
decoder_sequence_length=128,
)
backbone = keras_nlp.models.BartBackbone(
vocabulary_size=50265,
num_layers=6,
num_heads=12,
hidden_dim=768,
intermediate_dim=3072,
max_sequence_length=128,
)
bart_lm = keras_nlp.models.BartSeq2SeqLM(
backbone=backbone,
preprocessor=preprocessor,
)
bart_lm.fit(x=features, batch_size=2)
from_preset
方法BartSeq2SeqLM.from_preset(preset, load_weights=True, **kwargs)
從模型預設集實例化 keras_nlp.models.Task
。
預設集是一個包含配置、權重和其他檔案資源的目錄,用於儲存和載入預先訓練的模型。preset
可以作為以下其中之一傳遞
'bert_base_en'
'kaggle://user/bert/keras/bert_base_en'
'hf://user/bert_base_en'
'./bert_base_en'
對於任何 Task
子類別,您可以執行 cls.presets.keys()
來列出類別上可用的所有內建預設集。
這個建構函數可以透過兩種方式之一呼叫。可以從特定任務的基類呼叫,例如 keras_nlp.models.CausalLM.from_preset()
,也可以從模型類別呼叫,例如 keras_nlp.models.BertTextClassifier.from_preset()
。如果從基類呼叫,則返回物件的子類別將從預設集目錄中的配置推斷。
參數
True
,則儲存的權重將會載入到模型架構中。如果為 False
,則所有權重都將隨機初始化。範例
# Load a Gemma generative task.
causal_lm = keras_nlp.models.CausalLM.from_preset(
"gemma_2b_en",
)
# Load a Bert classification task.
model = keras_nlp.models.TextClassifier.from_preset(
"bert_base_en",
num_classes=2,
)
預設集名稱 | 參數 | 說明 |
---|---|---|
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
方法BartSeq2SeqLM.generate(inputs, max_length=None, stop_token_ids="auto")
根據提示 inputs
生成文字。
此方法根據給定的 inputs
生成文字。用於生成的取樣方法可以透過 compile()
方法設定。
如果 inputs
是 tf.data.Dataset
,則輸出將「逐批」生成並串聯起來。否則,所有輸入都將作為單一批次處理。
如果 preprocessor
附加到模型,則 inputs
將在 generate()
函數內部進行預處理,並且應與 preprocessor
層預期的結構相符(通常是原始字串)。如果沒有附加 preprocessor
,則輸入應與 backbone
預期的結構相符。請參閱上面的範例用法以了解每個範例的示範。
參數
tf.data.Dataset
。如果 preprocessor
附加到模型,則 inputs
應與 preprocessor
層預期的結構相符。如果沒有附加 preprocessor
,則輸入應與 backbone
模型預期的結構相符。preprocessor
配置的最大 sequence_length
。如果 preprocessor
為 None
,則 inputs
應填充到所需的最大長度,並且將忽略此參數。None
、「auto」或由多個權杖 ID 組成的元組。預設值為「auto」,會使用 preprocessor.tokenizer.end_token_id
。未指定處理器將會產生錯誤。將 None
設為在產生 max_length
個權杖後停止產生。您也可以指定模型應停止處理的權杖 ID 清單。請注意,每個權杖序列都會被解讀為停止權杖,不支援多重權杖停止序列。backbone
屬性keras_nlp.models.BartSeq2SeqLM.backbone
一個 keras_nlp.models.Backbone
模型,具有核心架構。
preprocessor
屬性keras_nlp.models.BartSeq2SeqLM.preprocessor
用於預先處理輸入的 keras_nlp.models.Preprocessor
層。