Views
No views yet
| Training Loss | Step | Validation Loss |
|---|---|---|
| 4.1548 | 0 | 4.1548 |
| 1.8700 | 400 | 1.9857 |
| 1.5111 | 800 | 1.7069 |
| 1.3853 | 1200 | 1.6218 |
| 1.3089 | 1600 | 1.5623 |
| 1.2522 | 1999 | 1.5198 |
model.py file to define the architecture and load the weights.1import torch
2import pickle
3from model import LanguageModel # Imports the class from the uploaded model.py
4
5# 1. Load Tokenizer Mappings
6with open('tokenizer_meta.pkl', 'rb') as f:
7 meta = pickle.load(f)
8stoi, itos = meta['stoi'], meta['itos']
9encode = lambda s: [stoi[c] for c in s]
10decode = lambda l: ''.join([itos[i] for i in l])
11
12# 2. Initialize Model
13device = 'cuda' if torch.cuda.is_available() else 'cpu'
14
15# Note: Using the exact architecture from training
16model = LanguageModel(
17 vocab_size=len(meta['vocab_size']), # derived from meta
18 n_embd=384,
19 block_size=256,
20 n_head=4,
21 n_layer=4
22)
23
24# 3. Load Weights
25model.load_state_dict(torch.load('pytorch_model.pth', map_location=device))
26model.to(device)
27model.eval()
28
29# 4. Generate Text
30context = torch.zeros((1, 1), dtype=torch.long, device=device)
31generated_ids = model.generate(context, max_new_tokens=500)[0].tolist()
32print(decode(generated_ids))