MistralCausalLMPreprocessor
類別keras_hub.models.MistralCausalLMPreprocessor(
tokenizer, sequence_length=1024, add_start_token=True, add_end_token=True, **kwargs
)
Mistral 因果語言模型預處理器。
此預處理層旨在與 keras_hub.models.MistralCausalLM
一起使用。預設情況下,它將接收字串批次,並以 (x, y, sample_weight)
格式傳回輸出,其中 y
標籤是 x
序列中的下一個 token id。
為了用於生成,此層還公開了兩個方法 generate_preprocess()
和 generate_postprocess()
。當此預處理器附加到 keras_hub.models.MistralCausalLM
實例時,這些方法將在 generate()
中隱式調用。它們也可以獨立調用(例如,在單獨的進程中預先計算生成預處理輸入)。
引數
keras_hub.models.MistralTokenizer
實例。True
,預處理器將把 tokenizer 開始 token 前置到每個輸入序列。預設值為 True
。True
,預處理器將把 tokenizer 結束 token 附加到每個輸入序列。預設值為 False
。呼叫引數
tf.Tensor
或 Python 字串列表。None
,因為此層會生成標籤。None
,因為此層會生成標籤權重。sequence_length
。範例
# Load the preprocessor from a preset.
preprocessor = keras_hub.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, config_file="preprocessor.json", **kwargs
)
從模型預設實例化 keras_hub.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_hub.models.BertTextClassifierPreprocessor.from_preset()
)上呼叫此方法。
引數
範例
# Load a preprocessor for Gemma generation.
preprocessor = keras_hub.models.CausalLMPreprocessor.from_preset(
"gemma_2b_en",
)
# Load a preprocessor for Bert classification.
preprocessor = keras_hub.models.TextClassifierPreprocessor.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_hub.models.MistralCausalLMPreprocessor.tokenizer
用於 token 化字串的 tokenizer。