Views
No views yet
pip install -U -q keras-hub
pip install -U -q keras| Preset name | Parameters | Description |
|---|---|---|
mistral_7b_en | 7.24B | 7B base model |
mistral_instruct_7b_en | 7.24B | 7B instruction-tuned model |
mistral_0.2_instruct_7b_en | 7.24B | 7B instruction-tuned model version 0.2 |
1prompt = """[INST] Hello! [/INST] Hello! How are you? [INST] I'm great. Could you help me with a task? [/INST]
2"""1import keras
2import keras_hub
3import numpy as npgenerate() to do text generation.1mistral_lm = keras_hub.models.MistralCausalLM.from_preset("mistral_instruct_7b_en")
2mistral_lm.generate("[INST] What is Keras? [/INST]", max_length=500)
3
4# Generate with batched prompts.
5mistral_lm.generate(["[INST] What is Keras? [/INST]", "[INST] Give me your best brownie recipe. [/INST]"], max_length=500)generate() function with a custom sampler.1mistral_lm = keras_hub.models.MistralCausalLM.from_preset("mistral_instruct_7b_en")
2mistral_lm.compile(sampler="greedy")
3mistral_lm.generate("I want to say", max_length=30)
4
5mistral_lm.compile(sampler=keras_hub.samplers.BeamSampler(num_beams=2))
6mistral_lm.generate("I want to say", max_length=30)generate() without preprocessing.1prompt = {
2 # `1` maps to the start token followed by "I want to say".
3 "token_ids": np.array([[1, 315, 947, 298, 1315, 0, 0, 0, 0, 0]] * 2),
4 # Use `"padding_mask"` to indicate values that should not be overridden.
5 "padding_mask": np.array([[1, 1, 1, 1, 1, 0, 0, 0, 0, 0]] * 2),
6}
7
8mistral_lm = keras_hub.models.MistralCausalLM.from_preset(
9 "mistral_instruct_7b_en",
10 preprocessor=None,
11 dtype="bfloat16"
12)
13mistral_lm.generate(prompt)fit() on a single batch.1features = ["The quick brown fox jumped.", "I forgot my homework."]
2mistral_lm = keras_hub.models.MistralCausalLM.from_preset("mistral_instruct_7b_en")
3mistral_lm.fit(x=features, batch_size=2)fit() without preprocessing.1x = {
2 "token_ids": np.array([[1, 315, 947, 298, 1315, 369, 315, 837, 0, 0]] * 2),
3 "padding_mask": np.array([[1, 1, 1, 1, 1, 1, 1, 1, 0, 0]] * 2),
4}
5y = np.array([[315, 947, 298, 1315, 369, 315, 837, 0, 0, 0]] * 2)
6sw = np.array([[1, 1, 1, 1, 1, 1, 1, 0, 0, 0]] * 2)
7
8mistral_lm = keras_hub.models.MistralCausalLM.from_preset(
9 "mistral_instruct_7b_en",
10 preprocessor=None,
11 dtype="bfloat16"
12)
13mistral_lm.fit(x=x, y=y, sample_weight=sw, batch_size=2)1import keras
2import keras_hub
3import numpy as npgenerate() to do text generation.1mistral_lm = keras_hub.models.MistralCausalLM.from_preset("hf://keras/mistral_instruct_7b_en")
2mistral_lm.generate("[INST] What is Keras? [/INST]", max_length=500)
3
4# Generate with batched prompts.
5mistral_lm.generate(["[INST] What is Keras? [/INST]", "[INST] Give me your best brownie recipe. [/INST]"], max_length=500)generate() function with a custom sampler.1mistral_lm = keras_hub.models.MistralCausalLM.from_preset("hf://keras/mistral_instruct_7b_en")
2mistral_lm.compile(sampler="greedy")
3mistral_lm.generate("I want to say", max_length=30)
4
5mistral_lm.compile(sampler=keras_hub.samplers.BeamSampler(num_beams=2))
6mistral_lm.generate("I want to say", max_length=30)generate() without preprocessing.1prompt = {
2 # `1` maps to the start token followed by "I want to say".
3 "token_ids": np.array([[1, 315, 947, 298, 1315, 0, 0, 0, 0, 0]] * 2),
4 # Use `"padding_mask"` to indicate values that should not be overridden.
5 "padding_mask": np.array([[1, 1, 1, 1, 1, 0, 0, 0, 0, 0]] * 2),
6}
7
8mistral_lm = keras_hub.models.MistralCausalLM.from_preset(
9 "hf://keras/mistral_instruct_7b_en",
10 preprocessor=None,
11 dtype="bfloat16"
12)
13mistral_lm.generate(prompt)fit() on a single batch.1features = ["The quick brown fox jumped.", "I forgot my homework."]
2mistral_lm = keras_hub.models.MistralCausalLM.from_preset("hf://keras/mistral_instruct_7b_en")
3mistral_lm.fit(x=features, batch_size=2)fit() without preprocessing.1x = {
2 "token_ids": np.array([[1, 315, 947, 298, 1315, 369, 315, 837, 0, 0]] * 2),
3 "padding_mask": np.array([[1, 1, 1, 1, 1, 1, 1, 1, 0, 0]] * 2),
4}
5y = np.array([[315, 947, 298, 1315, 369, 315, 837, 0, 0, 0]] * 2)
6sw = np.array([[1, 1, 1, 1, 1, 1, 1, 0, 0, 0]] * 2)
7
8mistral_lm = keras_hub.models.MistralCausalLM.from_preset(
9 "hf://keras/mistral_instruct_7b_en",
10 preprocessor=None,
11 dtype="bfloat16"
12)
13mistral_lm.fit(x=x, y=y, sample_weight=sw, batch_size=2)