Instructions to use kerasformers/gpt2_medium with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- KerasFormers
How to use kerasformers/gpt2_medium 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 kerasformers/gpt2_medium with Keras:
# Available backend options are: "jax", "torch", "tensorflow". import os os.environ["KERAS_BACKEND"] = "jax" import keras model = keras.saving.load_model("hf://kerasformers/gpt2_medium") - Notebooks
- Google Colab
- Kaggle
Run GPT-2 with Keras 3: JAX, PyTorch, or TensorFlow
kerasformers/gpt2_medium
Paper: Language Models are Unsupervised Multitask Learners (Radford et al., 2019)
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
355M variant, a base completion model (no chat template).
For more details, see the upstream model card.
Pure-Keras 3 conversion of openai-community/gpt2-medium for
kerasformers. One implementation runs unmodified on
TensorFlow / Torch / JAX.
Quick start
import os
os.environ["KERAS_BACKEND"] = "torch" # or "jax" / "tensorflow"
from kerasformers.models.gpt2 import GPT2Generate, GPT2Tokenizer
model = GPT2Generate.from_weights("kerasformers/gpt2_medium")
tokenizer = GPT2Tokenizer.from_weights("kerasformers/gpt2_medium")
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 |
124M |
gpt2_medium |
kerasformers/gpt2_medium |
355M |
gpt2_large |
kerasformers/gpt2_large |
774M |
gpt2_xl |
kerasformers/gpt2_xl |
1.5B |
Tips
- Set
KERAS_BACKENDbefore importing Keras / kerasformers. - This is a base completion model: it continues a prompt and is not instruction-tuned.
- See the GPT-2 docs and Loading Weights.
- Upstream safetensors still work via the
hf:prefix, e.g.GPT2Generate.from_weights("hf:openai-community/gpt2-medium").
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.
- Downloads last month
- 24
Model tree for kerasformers/gpt2_medium
Base model
openai-community/gpt2-medium