Core parameters
Number of independent samples to generate. Each sample starts from the same prompt but produces different output due to randomness.
Maximum number of tokens to generate in each sample. This controls the length of the generated text.
Starting prompt for generation. Can be:
- Direct text:
"Once upon a time" - Special token:
"<|endoftext|>" - File reference:
"FILE:prompt.txt"(reads prompt from file)
Sampling control
Temperature
Controls randomness in token selection:
1.0- No change to the model’s predictions< 1.0- Less random, more conservative outputs> 1.0- More random, more diverse outputs- Approaching
0.0- Nearly deterministic (always picks most likely token)
Top-k sampling
Limits token selection to the top-k most likely tokens. All other tokens have their probability set to zero.
- Higher values (e.g.,
200,500) - More diversity - Lower values (e.g.,
10,50) - More focused outputs Noneor0- No top-k filtering (use full vocabulary)
Reproducibility
Random seed for reproducible generation. Using the same seed with the same parameters will produce identical outputs.
Complete example
Here’s how the generation loop works insample.py:
Configuration file
You can override parameters using the configurator system:- Create a config file (e.g.,
my_sample_config.py):
my_sample_config.py
- Run sampling with the config:
Parameter recommendations
For coherent, focused text
For creative, diverse text
For long-form generation
The optimal parameters depend on your model, training data, and use case. Experiment with different combinations to find what works best.
Tokenization
The script automatically handles tokenization based on your model:Custom datasets
If you trained on a custom dataset with ameta.pkl file:
GPT-2 models
For pre-trained GPT-2 models or when nometa.pkl exists:
The
<|endoftext|> token is a special token in GPT-2 that indicates the end of a document. You can use it as a starting prompt to generate from scratch.