C.W.K.
Stream
Lesson 02 of 07 · published

Pretrained Models

~9 min · keras-nlp

Level 0Keras Apprentice
0 XP0/97 lessons0/20 achievements
0/120 XP to next level120 XP to go0% complete

The model zoo, organized by what they do

KerasHub ships dozens of pretrained presets, but they sort cleanly into three architectural families. The table below is the cheat sheet — read the Type column first, because architecture decides what a model is good for far more than its brand name does.

ModelTypeUse Case
BERTEncoderClassification, NER, embeddings
GPT-2Causal LMText generation
Gemma (1/2/3/4)Causal LMGoogle's open model family
Llama 2/3Causal LMMeta's open models
MistralCausal LMEfficient open model
T5Encoder-DecoderTranslation, summarization
WhisperAudioSpeech recognition

One loader, every model

Notice what the Code section shows: a classifier and a generator load with the same from_preset() call — only the class name and preset string change. That uniformity is the payoff of the hub. You don't relearn an API per model; you pick a class for your task, hand it a preset name, and KerasHub fetches the weights and the matching tokenizer in one shot. num_classes on the classifier is the only task-specific knob — it reshapes the output head while leaving the pretrained backbone untouched.

Code

Load a classifier and a generator with the same from_preset call·python
import keras_hub

# Load a pretrained classifier
classifier = keras_hub.models.BertClassifier.from_preset(
    "bert_base_en",
    num_classes=4,
)

# Load a text generator
gpt2 = keras_hub.models.GPT2CausalLM.from_preset("gpt2_base_en")

External links

Exercise

Load BertBackbone (small) and GemmaCausalLM (small). Print parameter counts. Run inference on the same text input. Note that BERT outputs embeddings, Gemma outputs token logits.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.