-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_validation.py
49 lines (34 loc) · 1.15 KB
/
run_validation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import torch
from pres_gpt2 import PresGPT2, GPTConfig
from Dataset import PresidentDataset
import pickle
import tiktoken
from torch.utils.data import DataLoader
from const import block_size
with open("./tokenizer/pres_tokenizer.pkl", "rb") as f:
pres_enc: tiktoken.Encoding = pickle.load(f)
with open("./data/validation.pkl", "rb") as f:
validation = pickle.load(f)
validation_dataset = PresidentDataset(validation)
config: GPTConfig = GPTConfig(
block_size,
len(pres_enc._mergeable_ranks) + len(pres_enc._special_tokens),
12,
12,
768,
0.0
)
model: PresGPT2 = PresGPT2(config)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
checkpoint = torch.load('./model/checkpoint.pt', map_location=device) # Adjust device if needed
model.load_state_dict(checkpoint)
model.to(device)
validation_dataloader = DataLoader(validation_dataset, batch_size=16, shuffle=True)
model.eval()
val_loss = 0
with torch.no_grad():
for X, Y in validation_dataloader:
X, Y = X.to(device), Y.to(device)
_, loss = model(X, Y)
val_loss += loss.item()
print(val_loss / len(validation_dataloader))