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()
中被隱式呼叫。它們也可以獨立呼叫(例如,在單獨的程序中預先計算生成所需的預處理輸入)。
參數
keras_nlp.models.MistralTokenizer
實例。True
,則預處理器會在每個輸入序列的開頭添加標記器起始標記。預設值為 True
。True
,則預處理器會在每個輸入序列的結尾添加標記器結束標記。預設值為 False
。呼叫參數
tf.Tensor
或 Python 字串列表。None
,因為該層會生成標籤。None
,因為該層會生成標籤權重。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
可以傳遞為以下其中之一
'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",
)
預設名稱 | 參數 | 描述 |
---|---|---|
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
用於將字串標記化的標記器。