Views
No views yet
n_embd): 384n_head): 2n_layer): 24train.txt (not included here; assume Shakespeare for reference).pip install torchgpt_model.pth) and vocabulary (vocab.pkl) must be downloaded from this repository.generate.py (or train.py) into your script. Here's an example:1import torch
2import pickle
3# Load hyperparameters (ensure they match training)
4block_size = 256
5n_embd = 384
6n_head = 2 # Corrected to match training
7n_layer = 24 # Corrected to match training
8dropout = 0.2
9device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
10# Load vocab
11with open('vocab.pkl', 'rb') as f:
12 vocab_data = pickle.load(f)
13stoi = vocab_data['stoi']
14itos = vocab_data['itos']
15vocab_size = vocab_data['vocab_size']
16decode = lambda l: ''.join([itos[i] for i in l])
17# Model classes (paste from generate.py or train.py here)
18# ... (Head, MultiHeadAttention, FeedForward, Block, GPTLanguageModel)
19# Load the model
20model = GPTLanguageModel()
21model.load_state_dict(torch.load('gpt_model.pth', map_location=device))
22model = model.to(device)
23model.eval()
24# Generate text
25context = torch.zeros((1, 1), dtype=torch.long, device=device)
26generated = model.generate(context, max_new_tokens=500)
27print(decode(generated[0].tolist()))train.py (for training) and generate.py (for inference) in the repository.train.txt with your text corpus.train.py to train and save the model/vocab.train.txt, assumed to be a concatenation of Shakespeare's works (as in nanoGPT). Total characters: ~1M (typical for Shakespeare). Unique characters: 65.