Views
No views yet
pip install -U -q keras-hub
pip install -U -q keras
| Preset | Architecture | Total Params | Active Params | Description |
|---|---|---|---|---|
qwen3_5_moe_35b_a3b_base | Qwen3.5 MoE | ~35B | ~3B | Qwen3.5 MoE base model. |
qwen3_5_moe_35b_a3b | Qwen3.5 MoE | ~35B | ~3B | Qwen3.5 MoE instruction-tuned chat model. |
1import keras
2import keras_hub
3import numpy as np1
2# Load pre-trained Qwen3 model
3qwen3_lm = keras_hub.models.Qwen3_5MoeCausalLM.from_preset( "qwen3_5_moe_35b_a3b")
4
5# Generate text from prompt
6response = qwen3_lm.generate("I want to learn about", max_length=50)
7print(response)
8
9# Batch generation with multiple prompts
10prompts = ["The future of AI is", "Machine learning helps us"]
11responses = qwen3_lm.generate(prompts, max_length=30)
12for prompt, response in zip(prompts, responses):
13 print(f"Prompt: {prompt}")
14 print(f"Response: {response}\n")
151
2# Greedy sampling (default)
3qwen3_lm.compile(sampler="greedy")
4response = qwen3_lm.generate("Explain quantum computing", max_length=100)
5
6# Top-k sampling
7qwen3_lm.compile(sampler="top_k")
8response = qwen3_lm.generate("Write a story about", max_length=80)
9
10# Beam search
11qwen3_lm.compile(sampler=keras_hub.samplers.BeamSampler(num_beams=4))
12response = qwen3_lm.generate("The best way to learn programming is", max_length=60)
131
2# Enable LoRA for efficient fine-tuning
3qwen3_lm.backbone.enable_lora(rank=8)
4
5# Prepare training data
6training_texts = [
7 "The quick brown fox jumped over the lazy dog.",
8 "Machine learning is a subset of artificial intelligence.",
9 "Python is a popular programming language for data science.",
10 "Deep learning models require large amounts of training data.",
11 "Natural language processing helps computers understand human language."
12]
13
14# Compile for training
15qwen3_lm.compile(
16 loss=keras.losses.SparseCategoricalCrossentropy(from_logits=True),
17 optimizer=keras.optimizers.Adam(1e-4),
18 metrics=["accuracy"]
19)
20
21# Fine-tune the model
22qwen3_lm.fit(x=training_texts, batch_size=2, epochs=3)
23
24# Generate with fine-tuned model
25response = qwen3_lm.generate("The importance of", max_length=50)
26print(response)
271
2# Create custom Qwen3_5 MoE backbone
3backbone = keras_hub.models.Qwen3_5MoeBackbone(
4 vocabulary_size=151936,
5 num_layers=12, # Smaller model for faster training
6 num_query_heads=16,
7 num_key_value_heads=8,
8 head_dim=128,
9 hidden_dim=1024,
10 intermediate_dim=2048,
11 layer_norm_epsilon=1e-6,
12 dropout=0.1,
13 dtype="float32"
14)
15
16# Create tokenizer first
17tokenizer = keras_hub.models.Qwen3_5MoeTokenizer.from_preset("qwen3_5_moe_35b_a3b")
18
19# Create preprocessor with tokenizer
20preprocessor = keras_hub.models.Qwen3_5MoeCausalLMPreprocessor(
21 tokenizer=tokenizer,
22 sequence_length=512
23)
24
25# Create custom causal LM
26custom_qwen3 = keras_hub.models.Qwen3_5MoeCausalLM(
27 backbone=backbone,
28 preprocessor=preprocessor
29)
30
31# Compile and train
32custom_qwen3.compile(
33 loss=keras.losses.SparseCategoricalCrossentropy(from_logits=True),
34 optimizer=keras.optimizers.Adam(1e-4)
35)
36
37# Training data
38texts = ["Hello world", "How are you", "Machine learning"]
39custom_qwen3.fit(x=texts, batch_size=2, epochs=1)1import keras
2import keras_hub
3import numpy as np1
2# Load pre-trained Qwen3 model
3qwen3_lm = keras_hub.models.Qwen3_5MoeCausalLM.from_preset( "hf://keras/qwen3_5_moe_35b_a3b")
4
5# Generate text from prompt
6response = qwen3_lm.generate("I want to learn about", max_length=50)
7print(response)
8
9# Batch generation with multiple prompts
10prompts = ["The future of AI is", "Machine learning helps us"]
11responses = qwen3_lm.generate(prompts, max_length=30)
12for prompt, response in zip(prompts, responses):
13 print(f"Prompt: {prompt}")
14 print(f"Response: {response}\n")
151
2# Greedy sampling (default)
3qwen3_lm.compile(sampler="greedy")
4response = qwen3_lm.generate("Explain quantum computing", max_length=100)
5
6# Top-k sampling
7qwen3_lm.compile(sampler="top_k")
8response = qwen3_lm.generate("Write a story about", max_length=80)
9
10# Beam search
11qwen3_lm.compile(sampler=keras_hub.samplers.BeamSampler(num_beams=4))
12response = qwen3_lm.generate("The best way to learn programming is", max_length=60)
131
2# Enable LoRA for efficient fine-tuning
3qwen3_lm.backbone.enable_lora(rank=8)
4
5# Prepare training data
6training_texts = [
7 "The quick brown fox jumped over the lazy dog.",
8 "Machine learning is a subset of artificial intelligence.",
9 "Python is a popular programming language for data science.",
10 "Deep learning models require large amounts of training data.",
11 "Natural language processing helps computers understand human language."
12]
13
14# Compile for training
15qwen3_lm.compile(
16 loss=keras.losses.SparseCategoricalCrossentropy(from_logits=True),
17 optimizer=keras.optimizers.Adam(1e-4),
18 metrics=["accuracy"]
19)
20
21# Fine-tune the model
22qwen3_lm.fit(x=training_texts, batch_size=2, epochs=3)
23
24# Generate with fine-tuned model
25response = qwen3_lm.generate("The importance of", max_length=50)
26print(response)
271
2# Create custom Qwen3_5 MoE backbone
3backbone = keras_hub.models.Qwen3_5MoeBackbone(
4 vocabulary_size=151936,
5 num_layers=12, # Smaller model for faster training
6 num_query_heads=16,
7 num_key_value_heads=8,
8 head_dim=128,
9 hidden_dim=1024,
10 intermediate_dim=2048,
11 layer_norm_epsilon=1e-6,
12 dropout=0.1,
13 dtype="float32"
14)
15
16# Create tokenizer first
17tokenizer = keras_hub.models.Qwen3_5MoeTokenizer.from_preset("hf://keras/qwen3_5_moe_35b_a3b")
18
19# Create preprocessor with tokenizer
20preprocessor = keras_hub.models.Qwen3_5MoeCausalLMPreprocessor(
21 tokenizer=tokenizer,
22 sequence_length=512
23)
24
25# Create custom causal LM
26custom_qwen3 = keras_hub.models.Qwen3_5MoeCausalLM(
27 backbone=backbone,
28 preprocessor=preprocessor
29)
30
31# Compile and train
32custom_qwen3.compile(
33 loss=keras.losses.SparseCategoricalCrossentropy(from_logits=True),
34 optimizer=keras.optimizers.Adam(1e-4)
35)
36
37# Training data
38texts = ["Hello world", "How are you", "Machine learning"]
39custom_qwen3.fit(x=texts, batch_size=2, epochs=1)