KerasNLP
raw JSON → 0.28.0 verified Fri May 01 auth: no python
KerasNLP provides pretrained NLP models (e.g., BERT, GPT-2, T5) and preprocessing utilities for Keras. Current version 0.28.0 supports Python >=3.10. Released approximately monthly.
pip install keras-nlp Common errors
error AttributeError: module 'keras_nlp' has no attribute 'BertClassifier' ↓
cause Wrong import path. BertClassifier is in keras_nlp.models.
fix
Use: from keras_nlp.models import BertClassifier
error RuntimeError: You must call `build()` on the tokenizer before using it. ↓
cause Tokenizers from presets need to be built or used via from_preset which builds automatically.
fix
Use: tokenizer = keras_nlp.tokenizers.BertTokenizer.from_preset('bert_tiny_en_uncased')
error ValueError: Unknown preset: 'bert_tiny_uncased' ↓
cause Typo or outdated preset name.
fix
Run: print(keras_nlp.presets.list_presets()) to see valid names.
error ImportError: cannot import name 'bert_base_preprocessor' from 'keras_nlp.models' ↓
cause The preprocessor name may vary by preset; not all presets have this symbol.
fix
Use: from keras_nlp.models import BertPreprocessor (or check preset-specific exports).
Warnings
breaking KerasNLP models require Keras 3 and TensorFlow/JAX/PyTorch backend. Using with Keras 2 will fail. ↓
fix Install Keras 3: pip install 'keras>=3.0' and set backend via KERAS_BACKEND env var.
deprecated Presets like 'bert_tiny_en_uncased' may be removed or renamed in future versions. Always refer to the official preset list. ↓
fix Use keras_nlp.presets.list_presets() to see current presets.
gotcha Tokenizers and preprocessors must be built with a vocabulary before use. Calling .fit() on a raw tokenizer will raise an error. ↓
fix Use a preset tokenizer: tokenizer = keras_nlp.tokenizers.BertTokenizer.from_preset('bert_tiny_en_uncased')
breaking From version 0.13.0, 'import keras_nlp' no longer imports TensorFlow. You must explicitly import the backend. ↓
fix Add import os; os.environ['KERAS_BACKEND'] = 'tensorflow' before importing keras_nlp.
Imports
- KerasNLP
import keras_nlp - BertTokenizer wrong
from keras_nlp.models import BertTokenizercorrectfrom keras_nlp.tokenizers import BertTokenizer - bert_base_preprocessor wrong
import bert_base_preprocessorcorrectfrom keras_nlp.models import bert_base_preprocessor
Quickstart
import keras_nlp
import keras
# Load a pretrained BERT model
model = keras_nlp.models.BertClassifier.from_preset(
"bert_tiny_en_uncased",
num_classes=2,
)
# Encode sample text using the built-in preprocessor
preprocessor = keras_nlp.models.bert_base_preprocessor()
inputs = preprocessor(["Hello, world!"])
# Predict
outputs = model(inputs)
print(outputs)