Views
No views yet
pip install -U -q keras-hub
pip install -U -q keras| Preset name | Parameters | Description |
|---|---|---|
| qwen2.5_0.5b_en | 0.5B | 24-layer Qwen model with 0.5 billion parameters. |
| qwen2.5_3b_en | 3.1B | 36-layer Qwen model with 3.1 billion parameters. |
| qwen2.5_7b_en | 7B | 48-layer Qwen model with 7 billion parameters. |
| qwen2.5_instruct_0.5b_en | 0.5B | Instruction fine-tuned 24-layer Qwen model with 0.5 billion parameters. |
| qwen2.5_instruct_32b_en | 32B | Instruction fine-tuned 64-layer Qwen model with 32 billion parameters. |
| qwen2.5_instruct_72b_en | 72B | Instruction fine-tuned 80-layer Qwen model with 72 billion parameters. |
1
2import keras
3import keras_hub
4import numpy as np
5
6# Use generate() to do text generation.
7qwen_lm = keras_hub.models.Qwen2CausalLM.from_preset("qwen2.5_instruct_32b_en")
8qwen_lm.generate("I want to say", max_length=30)
9
10# Generate with batched prompts.
11qwen_lm.generate(["This is a", "Where are you"], max_length=30)
12
13# Compile the generate() function with a custom sampler.
14qwen_lm = keras_hub.models.Qwen2CausalLM.from_preset("qwen2.5_instruct_32b_en")
15qwen_lm.compile(sampler="greedy")
16qwen_lm.generate("I want to say", max_length=30)
17qwen_lm.compile(sampler=keras_hub.samplers.BeamSampler(num_beams=2))
18qwen_lm.generate("I want to say", max_length=30)
19
20# Use generate() without preprocessing.
21# Prompt the model with `15191, 374` (the token ids for `"Who is"`).
22# Use `"padding_mask"` to indicate values that should not be overridden.
23prompt = {
24 "token_ids": np.array([[15191, 374, 0, 0, 0]] * 2),
25 "padding_mask": np.array([[1, 1, 0, 0, 0]] * 2),
26}
27
28qwen_lm = keras_hub.models.Qwen2CausalLM.from_preset(
29 "qwen2.5_instruct_32b_en",
30 preprocessor=None,
31)
32qwen_lm.generate(prompt)
33
34# Call fit() on a single batch.
35features = ["The quick brown fox jumped.", "I forgot my homework."]
36qwen_lm = keras_hub.models.Qwen2CausalLM.from_preset("qwen2.5_instruct_32b_en")
37qwen_lm.fit(x=features, batch_size=2)
38
39# Call fit() without preprocessing.
40x = {
41 "token_ids": np.array([[1, 2, 3, 4, 5]] * 2),
42 "padding_mask": np.array([[1, 1, 1, 1, 1]] * 2),
43}
44y = np.array([[2, 3, 4, 5, 0]] * 2)
45sw = np.array([[1, 1, 1, 1, 1]] * 2)
46
47qwen_lm = keras_hub.models.Qwen2CausalLM.from_preset(
48 "qwen2.5_instruct_32b_en",
49 preprocessor=None,
50)
51qwen_lm.fit(x=x, y=y, sample_weight=sw, batch_size=2)
521
2import keras
3import keras_hub
4import numpy as np
5
6# Use generate() to do text generation.
7qwen_lm = keras_hub.models.Qwen2CausalLM.from_preset("hf://keras/qwen2.5_instruct_32b_en")
8qwen_lm.generate("I want to say", max_length=30)
9
10# Generate with batched prompts.
11qwen_lm.generate(["This is a", "Where are you"], max_length=30)
12
13# Compile the generate() function with a custom sampler.
14qwen_lm = keras_hub.models.Qwen2CausalLM.from_preset("hf://keras/qwen2.5_instruct_32b_en")
15qwen_lm.compile(sampler="greedy")
16qwen_lm.generate("I want to say", max_length=30)
17qwen_lm.compile(sampler=keras_hub.samplers.BeamSampler(num_beams=2))
18qwen_lm.generate("I want to say", max_length=30)
19
20# Use generate() without preprocessing.
21# Prompt the model with `15191, 374` (the token ids for `"Who is"`).
22# Use `"padding_mask"` to indicate values that should not be overridden.
23prompt = {
24 "token_ids": np.array([[15191, 374, 0, 0, 0]] * 2),
25 "padding_mask": np.array([[1, 1, 0, 0, 0]] * 2),
26}
27
28qwen_lm = keras_hub.models.Qwen2CausalLM.from_preset(
29 "hf://keras/qwen2.5_instruct_32b_en",
30 preprocessor=None,
31)
32qwen_lm.generate(prompt)
33
34# Call fit() on a single batch.
35features = ["The quick brown fox jumped.", "I forgot my homework."]
36qwen_lm = keras_hub.models.Qwen2CausalLM.from_preset("hf://keras/qwen2.5_instruct_32b_en")
37qwen_lm.fit(x=features, batch_size=2)
38
39# Call fit() without preprocessing.
40x = {
41 "token_ids": np.array([[1, 2, 3, 4, 5]] * 2),
42 "padding_mask": np.array([[1, 1, 1, 1, 1]] * 2),
43}
44y = np.array([[2, 3, 4, 5, 0]] * 2)
45sw = np.array([[1, 1, 1, 1, 1]] * 2)
46
47qwen_lm = keras_hub.models.Qwen2CausalLM.from_preset(
48 "hf://keras/qwen2.5_instruct_32b_en",
49 preprocessor=None,
50)
51qwen_lm.fit(x=x, y=y, sample_weight=sw, batch_size=2)
52