Skip to main content
The sampling script provides several parameters to control how text is generated. These parameters affect the randomness, diversity, and length of the generated outputs.

Core parameters

int
default:"10"
Number of independent samples to generate. Each sample starts from the same prompt but produces different output due to randomness.
int
default:"500"
Maximum number of tokens to generate in each sample. This controls the length of the generated text.
string
default:"\\n"
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

float
default:"0.8"
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)
Temperature affects the probability distribution over tokens. Lower temperatures make the model more confident and repetitive, while higher temperatures increase diversity but may reduce coherence.

Top-k sampling

int
default:"200"
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
  • None or 0 - No top-k filtering (use full vocabulary)
Top-k sampling prevents the model from selecting very unlikely tokens, which can improve output quality.

Reproducibility

int
default:"1337"
Random seed for reproducible generation. Using the same seed with the same parameters will produce identical outputs.
Set the seed for reproducible results:
The seed affects both CPU and CUDA random number generators:

Complete example

Here’s how the generation loop works in sample.py:

Configuration file

You can override parameters using the configurator system:
  1. Create a config file (e.g., my_sample_config.py):
my_sample_config.py
  1. Run sampling with the config:
Command-line arguments will override config file values:

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 a meta.pkl file:

GPT-2 models

For pre-trained GPT-2 models or when no meta.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.