Keras 3 API 文件 / KerasNLP / 預訓練模型 / Mistral / MistralCausalLMPreprocessor 層

MistralCausalLMPreprocessor 層

[來源]

MistralCausalLMPreprocessor 類別

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

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

這個預處理層旨在與 keras_nlp.models.MistralCausalLM 一起使用。預設情況下,它會接收一批字串,並以 (x, y, sample_weight) 格式返回輸出,其中 y 標籤是 x 序列中的下一個標記 ID。

為了用於生成,該層還公開了兩個方法 generate_preprocess()generate_postprocess()。當這個預處理器附加到 keras_nlp.models.MistralCausalLM 實例時,這些方法將在 generate() 中被隱式呼叫。它們也可以獨立呼叫(例如,在單獨的程序中預先計算生成所需的預處理輸入)。

參數

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

呼叫參數

  • x:一個字串、tf.Tensor 或 Python 字串列表。
  • y:標籤資料。應始終為 None,因為該層會生成標籤。
  • sample_weight:標籤權重。應始終為 None,因為該層會生成標籤權重。
  • sequence_length:傳遞以覆蓋該層配置的 sequence_length

範例

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

MistralCausalLMPreprocessor.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",
)
預設名稱 參數 描述
mistral_7b_en 7.24B Mistral 7B 基礎模型
mistral_instruct_7b_en 7.24B Mistral 7B 指令模型
mistral_0.2_instruct_7b_en 7.24B Mistral 7B 指令版本 0.2 模型

tokenizer 屬性

keras_nlp.models.MistralCausalLMPreprocessor.tokenizer

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