Instructions to use zeromodels/gpt2 with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- KerasFormers
How to use zeromodels/gpt2 with KerasFormers:
# No code snippets available yet for this library. # To use this model, check the repository files and the library's documentation. # Want to help? PRs adding snippets are welcome at: # https://github.com/huggingface/huggingface.js
- Keras
How to use zeromodels/gpt2 with Keras:
# Available backend options are: "jax", "torch", "tensorflow". import os os.environ["KERAS_BACKEND"] = "jax" import keras model = keras.saving.load_model("hf://zeromodels/gpt2") - Notebooks
- Google Colab
- Kaggle
File size: 2,880 Bytes
da6ae62 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | ---
pipeline_tag: text-generation
license: mit
base_model: openai-community/gpt2
library_name: kerasformers
language:
- en
tags:
- keras
- kerasformers
- gpt2
- gpt-2
- text-generation
- pytorch
- jax
- tf
---
# Run GPT-2 with Keras 3: JAX, PyTorch, or TensorFlow
[](https://github.com/IMvision12/KerasFormers) [](https://imvision12.github.io/KerasFormers/gpt2/)
# kerasformers/gpt2
Paper: [Language Models are Unsupervised Multitask Learners (Radford et al., 2019)](https://cdn.openai.com/better-language-models/language_models_are_unsupervised_multitask_learners.pdf)
GPT-2 is OpenAI's decoder-only transformer language model trained on WebText:
learned absolute position embeddings, pre-LayerNorm blocks, `gelu_new`
activations, a tied output head, and a byte-level BPE tokenizer. This is the
**124M** variant, a base completion model (no chat template).
For more details, see the upstream [model card](https://huggingface.co/openai-community/gpt2).
Pure-**Keras 3** conversion of [`openai-community/gpt2`](https://huggingface.co/openai-community/gpt2) for
[kerasformers](https://github.com/IMvision12/KerasFormers). One implementation runs unmodified on
**TensorFlow / Torch / JAX**.
## Quick start
```python
import os
os.environ["KERAS_BACKEND"] = "torch" # or "jax" / "tensorflow"
from kerasformers.models.gpt2 import GPT2TextGenerate, GPT2Tokenizer
model = GPT2TextGenerate.from_weights("kerasformers/gpt2")
tokenizer = GPT2Tokenizer.from_weights("kerasformers/gpt2")
inputs = tokenizer("The meaning of life is")
outputs = model.generate(**inputs, max_new_tokens=40)
print(tokenizer.decode(outputs[0]))
```
All GPT-2 sizes load the same way with `from_weights("kerasformers/<variant>")`:
| Variant | Hub | Params |
|---|---|---|
| `gpt2` | [`kerasformers/gpt2`](https://huggingface.co/kerasformers/gpt2) | 124M |
| `gpt2_medium` | [`kerasformers/gpt2_medium`](https://huggingface.co/kerasformers/gpt2_medium) | 355M |
| `gpt2_large` | [`kerasformers/gpt2_large`](https://huggingface.co/kerasformers/gpt2_large) | 774M |
| `gpt2_xl` | [`kerasformers/gpt2_xl`](https://huggingface.co/kerasformers/gpt2_xl) | 1.5B |
## Tips
- Set `KERAS_BACKEND` **before** importing Keras / kerasformers.
- This is a base completion model: it continues a prompt and is not
instruction-tuned.
- See the [GPT-2 docs](https://imvision12.github.io/KerasFormers/gpt2/) and [Loading Weights](https://imvision12.github.io/KerasFormers/loading_weights/).
- Upstream safetensors still work via the `hf:` prefix, e.g.
`GPT2TextGenerate.from_weights("hf:openai-community/gpt2")`.
## Special Thanks
A huge thank you to the OpenAI GPT-2 authors for creating and releasing these models.
License: MIT, inherited from the upstream OpenAI GPT-2 release.
|