Keras 3 API 文件 / KerasHub / 模型 API / Seq2SeqLMPreprocessor

Seq2SeqLMPreprocessor

[來源]

Seq2SeqLMPreprocessor 類別

keras_hub.models.Seq2SeqLMPreprocessor(
    tokenizer, encoder_sequence_length=1024, decoder_sequence_length=1024, **kwargs
)

用於 seq2seq 語言模型預處理層的基底類別。

Seq2SeqLMPreprocessor 任務會包裝一個 keras_hub.tokenizer.Tokenizer 來建立用於 seq2seq 語言模型任務的預處理層。它旨在與 keras.models.Seq2SeqLM 任務配對使用。

所有 Seq2SeqLMPreprocessor 層都接受一個字典輸入,其中包含鍵 "encoder_text""decoder_text"

此層將始終輸出一個 (x, y, sample_weight) 元組,其中 x 是一個包含標記化輸入的字典,y 包含 x 中偏移 1 的標記,而 sample_weight 標記 y 中包含填充值的位置。x 的確切內容將根據所使用的模型而有所不同。

Seq2SeqLMPreprocessor 包含兩個額外的方法,generate_preprocessgenerate_postprocess,用於生成。請參閱下面的示例。

所有 Seq2SeqLMPreprocessor 任務都包含一個 from_preset() 建構函式,可用於載入預先訓練的配置和詞彙表。您可以直接在此基底類別上呼叫 from_preset() 建構函式,在這種情況下,將自動為您的模型實例化正確的類別。

示例。

preprocessor = keras_hub.models.Seq2SeqLMPreprocessor.from_preset(
    "bart_base_en",
    encoder_sequence_length=256,
    decoder_sequence_length=256,
)

# Tokenize, mask and pack a single sentence.
x = {
    "encoder_text": "The fox was sleeping.",
    "decoder_text": "The fox was awake.",
}
x, y, sample_weight = preprocessor(x)

# Tokenize and pad/truncate a batch of labeled sentences.
x = {
    "encoder_text": ["The fox was sleeping."],
    "decoder_text": ["The fox was awake."],
x, y, sample_weight = preprocessor(x)

# With a [`tf.data.Dataset`](https://tensorflow.dev.org.tw/api_docs/python/tf/data/Dataset).
ds = tf.data.Dataset.from_tensor_slices(x)
ds = ds.map(preprocessor, num_parallel_calls=tf.data.AUTOTUNE)

# Generate preprocess and postprocess.
x = preprocessor.generate_preprocess(x)  # Tokenized numeric inputs.
x = preprocessor.generate_postprocess(x)  # Detokenized string outputs.

[來源]

from_preset 方法

Seq2SeqLMPreprocessor.from_preset(preset, config_file="preprocessor.json", **kwargs)

從模型預設設定實例化一個 keras_hub.models.Preprocessor

預設設定是一個包含配置、權重和其他檔案資源的目錄,用於儲存和載入預先訓練的模型。preset 可以作為以下其中之一傳遞:

  1. 一個內建的預設設定識別碼,例如 'bert_base_en'
  2. 一個 Kaggle 模型處理常式,例如 'kaggle://user/bert/keras/bert_base_en'
  3. 一個 Hugging Face 處理常式,例如 'hf://user/bert_base_en'
  4. 一個指向本地預設設定目錄的路徑,例如 './bert_base_en'

對於任何 Preprocessor 子類別,您可以執行 cls.presets.keys() 來列出該類別上可用的所有內建預設設定。

由於給定模型通常有多個預處理類別,因此應該在特定的子類別上呼叫此方法,例如 keras_hub.models.BertTextClassifierPreprocessor.from_preset()

參數

  • preset:字串。內建預設集識別碼、Kaggle 模型控點、Hugging Face 控點或本機目錄的路徑。

範例

# Load a preprocessor for Gemma generation.
preprocessor = keras_hub.models.GemmaCausalLMPreprocessor.from_preset(
    "gemma_2b_en",
)

# Load a preprocessor for Bert classification.
preprocessor = keras_hub.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 骨幹模型。

[來源]

save_to_preset 方法

Seq2SeqLMPreprocessor.save_to_preset(preset_dir)

將預處理器儲存至預設集目錄。

參數

  • preset_dir:本機模型預設集目錄的路徑。

tokenizer 屬性

keras_hub.models.Seq2SeqLMPreprocessor.tokenizer

用於將字串標記化的標記器。