Keras 3 API 文件 / KerasNLP / 預訓練模型 / OPT / OPTCausalLMPreprocessor 層

OPTCausalLMPreprocessor 層

[原始碼]

OPTCausalLMPreprocessor 類別

keras_nlp.models.OPTCausalLMPreprocessor(
    tokenizer, sequence_length=1024, add_start_token=True, add_end_token=True, **kwargs
)

OPT 因果語言模型預處理器。

這個預處理層主要用於 keras_nlp.models.OPTCausalLM。默認情況下,它將接收一批字串,並以 (x, y, sample_weight) 的格式返回輸出,其中 y 標籤是 x 序列中的下一個標記 ID。若要與生成一起使用,請傳遞 return_labels=False,在這種情況下,輸出將只是編碼的字串特徵。

參數

  • tokenizer:一個 keras_nlp.models.OPTTokenizer 實例。
  • sequence_length:打包輸入的長度。
  • add_start_token:如果為 True,則預處理器會將標記器的起始標記附加到每個輸入序列的開頭。
  • add_end_token:如果為 True,則預處理器會將標記器的結束標記附加到每個輸入序列的結尾。

呼叫參數

  • x:一個字串、tf.Tensor 或 Python 字串的列表。
  • y:標籤數據。應始終為 None,因為該層會生成標籤。
  • sample_weight:標籤權重。應始終為 None,因為該層會生成標籤權重。
  • sequence_length:傳遞以覆寫層的已配置 sequence_length
  • add_start_token:傳遞以覆寫層上 add_start_token 的已配置值。
  • add_end_token:傳遞以覆寫層上 add_end_token 的已配置值。
  • return_labels:如果為 True,則輸出 "token_ids" 將偏移一個位置並作為標籤返回。如果為 False,則只會返回特徵。

示例

# Load the preprocessor from a preset.
preprocessor = keras_nlp.models.OPTCausalLMPreprocessor.from_preset(
    "opt_125m_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 方法

OPTCausalLMPreprocessor.from_preset(preset, **kwargs)

從模型預設設定實例化一個 keras_nlp.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_nlp.models.BertTextClassifierPreprocessor.from_preset()

參數

  • preset:字串。內建預設值識別碼、Kaggle 模型處理方式、Hugging Face 處理方式或本機目錄的路徑。

示例

# 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",
)
預設值名稱 參數 說明
opt_125m_en 125.24M 12 層 OPT 模型,保留大小寫。根據 BookCorpus、CommonCrawl、Pile 和 PushShift.io 語料庫進行訓練。
opt_1.3b_en 1.32B 24 層 OPT 模型,保留大小寫。根據 BookCorpus、CommonCrawl、Pile 和 PushShift.io 語料庫進行訓練。
opt_2.7b_en 2.70B 32 層 OPT 模型,保留大小寫。根據 BookCorpus、CommonCrawl、Pile 和 PushShift.io 語料庫進行訓練。
opt_6.7b_en 6.70B 32 層 OPT 模型,保留大小寫。根據 BookCorpus、CommonCrawl、Pile 和 PushShift.io 語料庫進行訓練。

tokenizer 屬性

keras_nlp.models.OPTCausalLMPreprocessor.tokenizer

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