Instructions to use keras/modern_bert_large_en with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- KerasHub
How to use keras/modern_bert_large_en with KerasHub:
import keras_hub # Load TextClassifier model text_classifier = keras_hub.models.TextClassifier.from_preset( "hf://keras/modern_bert_large_en", num_classes=2, ) # Fine-tune text_classifier.fit(x=["Thilling adventure!", "Total snoozefest."], y=[1, 0]) # Classify text text_classifier.predict(["Not my cup of tea."])import keras_hub # Create a MaskedLM model task = keras_hub.models.MaskedLM.from_preset("hf://keras/modern_bert_large_en")import keras_hub # Create a Backbone model unspecialized for any task backbone = keras_hub.models.Backbone.from_preset("hf://keras/modern_bert_large_en") - Keras
How to use keras/modern_bert_large_en with Keras:
# !pip install -U keras tensorflow huggingface_hub # Keras needs TensorFlow installed to read "hf://" paths, so the tensorflow backend is selected here; # "jax" and "torch" also work for computation once TensorFlow is installed. import os os.environ["KERAS_BACKEND"] = "tensorflow" import keras model = keras.saving.load_model("hf://keras/modern_bert_large_en") - Notebooks
- Google Colab
- Kaggle
Update README.md with new model card content
Browse files
README.md
CHANGED
|
@@ -1,22 +1,191 @@
|
|
| 1 |
---
|
| 2 |
library_name: keras-hub
|
| 3 |
---
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
*
|
| 13 |
-
*
|
| 14 |
-
*
|
| 15 |
-
*
|
| 16 |
-
*
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
library_name: keras-hub
|
| 3 |
---
|
| 4 |
+
### Model Overview
|
| 5 |
+
### Model Overview
|
| 6 |
+
ModernBERT is a family of encoder-only Transformer models published by [Answer.AI](https://www.answer.ai/) and [LightOn](https://www.lighton.ai/). It is a modernized drop-in replacement for BERT: pre-trained on 2 trillion tokens of English and code with a native 8,192-token context window, it uses Rotary Position Embeddings (RoPE), alternating local and global attention, and GeGLU feed-forward layers. ModernBERT is intended for classification, retrieval, and embedding of text, not for text-generation. See the model card below for benchmarks, data sources, and intended use cases.
|
| 7 |
+
|
| 8 |
+
Weights are released under the [Apache 2 License](https://github.com/AnswerDotAI/ModernBERT/blob/main/LICENSE). Keras model code is released under the [Apache 2 License](https://github.com/keras-team/keras-hub/blob/master/LICENSE).
|
| 9 |
+
|
| 10 |
+
## Links
|
| 11 |
+
|
| 12 |
+
* [ModernBERT Quickstart Notebook](https://www.kaggle.com/code/ruchithaxwf/modernbert-quickstart)
|
| 13 |
+
* [ModernBERT Model Card](https://huggingface.co/answerdotai/ModernBERT-base)
|
| 14 |
+
* [ModernBERT Model Paper](https://arxiv.org/abs/2412.13663)
|
| 15 |
+
* [KerasHub Beginner Guide](https://keras.io/guides/keras_hub/getting_started/)
|
| 16 |
+
* [KerasHub Model Publishing Guide](https://keras.io/guides/keras_hub/upload/)
|
| 17 |
+
|
| 18 |
+
## Installation
|
| 19 |
+
|
| 20 |
+
Keras and KerasHub can be installed with:
|
| 21 |
+
|
| 22 |
+
```
|
| 23 |
+
pip install -U -q keras-hub
|
| 24 |
+
pip install -U -q keras
|
| 25 |
+
```
|
| 26 |
+
|
| 27 |
+
Jax, TensorFlow, and Torch come preinstalled in Kaggle Notebooks. For instructions on installing them in another environment see the [Keras Getting Started](https://keras.io/getting_started/) page.
|
| 28 |
+
|
| 29 |
+
## Presets
|
| 30 |
+
|
| 31 |
+
The following model checkpoints are provided by the Keras team. Full code examples for each are available below.
|
| 32 |
+
|
| 33 |
+
| Preset name | Parameters | Description |
|
| 34 |
+
|------------------------|------------|--------------------------------------------------------------------------------------------------------------------------------------------------------|
|
| 35 |
+
| `modern_bert_base_en` | 149.01M | 22-layer ModernBERT Base encoder model pretrained on English for masked language modeling. Uses RoPE, alternating local and global attention, and GeGLU feedforward layers. |
|
| 36 |
+
| `modern_bert_large_en` | 394.78M | 28-layer ModernBERT Large encoder model pretrained on English for masked language modeling. Uses RoPE, alternating local and global attention, and GeGLU feedforward layers. |
|
| 37 |
+
|
| 38 |
+
## Example Usage
|
| 39 |
+
```python
|
| 40 |
+
import keras
|
| 41 |
+
import keras_hub
|
| 42 |
+
import numpy as np
|
| 43 |
+
```
|
| 44 |
+
|
| 45 |
+
Raw string data.
|
| 46 |
+
|
| 47 |
+
```python
|
| 48 |
+
features = ["The quick brown fox jumped.", "I forgot my homework."]
|
| 49 |
+
labels = [0, 3]
|
| 50 |
+
|
| 51 |
+
# Pretrained classifier.
|
| 52 |
+
classifier = keras_hub.models.ModernBertTextClassifier.from_preset(
|
| 53 |
+
"modern_bert_large_en",
|
| 54 |
+
num_classes=4,
|
| 55 |
+
)
|
| 56 |
+
classifier.fit(x=features, y=labels, batch_size=2)
|
| 57 |
+
classifier.predict(x=features, batch_size=2)
|
| 58 |
+
|
| 59 |
+
# Re-compile (e.g., with a new learning rate).
|
| 60 |
+
classifier.compile(
|
| 61 |
+
loss=keras.losses.SparseCategoricalCrossentropy(from_logits=True),
|
| 62 |
+
optimizer=keras.optimizers.Adam(5e-5),
|
| 63 |
+
jit_compile=True,
|
| 64 |
+
)
|
| 65 |
+
# Access backbone programmatically (e.g., to change `trainable`).
|
| 66 |
+
classifier.backbone.trainable = False
|
| 67 |
+
# Fit again.
|
| 68 |
+
classifier.fit(x=features, y=labels, batch_size=2)
|
| 69 |
+
```
|
| 70 |
+
|
| 71 |
+
Preprocessed integer data.
|
| 72 |
+
|
| 73 |
+
```python
|
| 74 |
+
features = {
|
| 75 |
+
"token_ids": np.ones(shape=(2, 12), dtype="int32"),
|
| 76 |
+
"padding_mask": np.array([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0]] * 2),
|
| 77 |
+
}
|
| 78 |
+
labels = [0, 3]
|
| 79 |
+
|
| 80 |
+
# Pretrained classifier without preprocessing.
|
| 81 |
+
classifier = keras_hub.models.ModernBertTextClassifier.from_preset(
|
| 82 |
+
"modernbert_base_en",
|
| 83 |
+
num_classes=4,
|
| 84 |
+
preprocessor=None,
|
| 85 |
+
)
|
| 86 |
+
classifier.fit(x=features, y=labels, batch_size=2)
|
| 87 |
+
```
|
| 88 |
+
|
| 89 |
+
Masked language modeling.
|
| 90 |
+
|
| 91 |
+
```python
|
| 92 |
+
masked_lm = keras_hub.models.ModernBertMaskedLM.from_preset("modern_bert_large_en")
|
| 93 |
+
tokenizer = masked_lm.preprocessor.tokenizer
|
| 94 |
+
|
| 95 |
+
text = "The capital of France is [MASK]."
|
| 96 |
+
token_ids = (
|
| 97 |
+
[tokenizer.cls_token_id]
|
| 98 |
+
+ list(np.asarray(tokenizer(text)))
|
| 99 |
+
+ [tokenizer.sep_token_id]
|
| 100 |
+
)
|
| 101 |
+
mask_positions = [
|
| 102 |
+
i for i, t in enumerate(token_ids) if t == tokenizer.mask_token_id
|
| 103 |
+
]
|
| 104 |
+
logits = masked_lm(
|
| 105 |
+
{
|
| 106 |
+
"token_ids": np.array([token_ids], dtype="int32"),
|
| 107 |
+
"padding_mask": np.ones((1, len(token_ids)), dtype="bool"),
|
| 108 |
+
"mask_positions": np.array([mask_positions], dtype="int32"),
|
| 109 |
+
}
|
| 110 |
+
)
|
| 111 |
+
top5 = np.argsort(-np.asarray(logits)[0, 0])[:5]
|
| 112 |
+
print([tokenizer.detokenize([int(t)]).strip() for t in top5])
|
| 113 |
+
```
|
| 114 |
+
|
| 115 |
+
## Example Usage with Hugging Face URI
|
| 116 |
+
|
| 117 |
+
```python
|
| 118 |
+
import keras
|
| 119 |
+
import keras_hub
|
| 120 |
+
import numpy as np
|
| 121 |
+
```
|
| 122 |
+
|
| 123 |
+
Raw string data.
|
| 124 |
+
|
| 125 |
+
```python
|
| 126 |
+
features = ["The quick brown fox jumped.", "I forgot my homework."]
|
| 127 |
+
labels = [0, 3]
|
| 128 |
+
|
| 129 |
+
# Pretrained classifier.
|
| 130 |
+
classifier = keras_hub.models.ModernBertTextClassifier.from_preset(
|
| 131 |
+
"hf://keras/modern_bert_large_en",
|
| 132 |
+
num_classes=4,
|
| 133 |
+
)
|
| 134 |
+
classifier.fit(x=features, y=labels, batch_size=2)
|
| 135 |
+
classifier.predict(x=features, batch_size=2)
|
| 136 |
+
|
| 137 |
+
# Re-compile (e.g., with a new learning rate).
|
| 138 |
+
classifier.compile(
|
| 139 |
+
loss=keras.losses.SparseCategoricalCrossentropy(from_logits=True),
|
| 140 |
+
optimizer=keras.optimizers.Adam(5e-5),
|
| 141 |
+
jit_compile=True,
|
| 142 |
+
)
|
| 143 |
+
# Access backbone programmatically (e.g., to change `trainable`).
|
| 144 |
+
classifier.backbone.trainable = False
|
| 145 |
+
# Fit again.
|
| 146 |
+
classifier.fit(x=features, y=labels, batch_size=2)
|
| 147 |
+
```
|
| 148 |
+
|
| 149 |
+
Preprocessed integer data.
|
| 150 |
+
|
| 151 |
+
```python
|
| 152 |
+
features = {
|
| 153 |
+
"token_ids": np.ones(shape=(2, 12), dtype="int32"),
|
| 154 |
+
"padding_mask": np.array([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0]] * 2),
|
| 155 |
+
}
|
| 156 |
+
labels = [0, 3]
|
| 157 |
+
|
| 158 |
+
# Pretrained classifier without preprocessing.
|
| 159 |
+
classifier = keras_hub.models.ModernBertTextClassifier.from_preset(
|
| 160 |
+
"modernbert_base_en",
|
| 161 |
+
num_classes=4,
|
| 162 |
+
preprocessor=None,
|
| 163 |
+
)
|
| 164 |
+
classifier.fit(x=features, y=labels, batch_size=2)
|
| 165 |
+
```
|
| 166 |
+
|
| 167 |
+
Masked language modeling.
|
| 168 |
+
|
| 169 |
+
```python
|
| 170 |
+
masked_lm = keras_hub.models.ModernBertMaskedLM.from_preset("hf://keras/modern_bert_large_en")
|
| 171 |
+
tokenizer = masked_lm.preprocessor.tokenizer
|
| 172 |
+
|
| 173 |
+
text = "The capital of France is [MASK]."
|
| 174 |
+
token_ids = (
|
| 175 |
+
[tokenizer.cls_token_id]
|
| 176 |
+
+ list(np.asarray(tokenizer(text)))
|
| 177 |
+
+ [tokenizer.sep_token_id]
|
| 178 |
+
)
|
| 179 |
+
mask_positions = [
|
| 180 |
+
i for i, t in enumerate(token_ids) if t == tokenizer.mask_token_id
|
| 181 |
+
]
|
| 182 |
+
logits = masked_lm(
|
| 183 |
+
{
|
| 184 |
+
"token_ids": np.array([token_ids], dtype="int32"),
|
| 185 |
+
"padding_mask": np.ones((1, len(token_ids)), dtype="bool"),
|
| 186 |
+
"mask_positions": np.array([mask_positions], dtype="int32"),
|
| 187 |
+
}
|
| 188 |
+
)
|
| 189 |
+
top5 = np.argsort(-np.asarray(logits)[0, 0])[:5]
|
| 190 |
+
print([tokenizer.detokenize([int(t)]).strip() for t in top5])
|
| 191 |
+
```
|