-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheval.py
More file actions
60 lines (47 loc) · 1.94 KB
/
eval.py
File metadata and controls
60 lines (47 loc) · 1.94 KB
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
50
51
52
53
54
55
56
57
58
59
60
import argparse
import yaml
import torch
import math
from tqdm import tqdm
from src.models.hope import HOPE
from src.utils.data_loader import get_data_loader
def evaluate(model, dataloader, device):
model.eval()
total_loss = 0
total_steps = 0
with torch.no_grad():
for batch in tqdm(dataloader, desc="Evaluating"):
batch = batch.to(device)
logits = model(batch)
shift_logits = logits[..., :-1, :].contiguous()
shift_labels = batch[..., 1:].contiguous()
loss_fct = torch.nn.CrossEntropyLoss()
loss = loss_fct(shift_logits.view(-1, shift_logits.size(-1)), shift_labels.view(-1))
total_loss += loss.item()
total_steps += 1
avg_loss = total_loss / total_steps
perplexity = math.exp(avg_loss)
return avg_loss, perplexity
def main():
parser = argparse.ArgumentParser(description="Evaluate HOPE model")
parser.add_argument("--config", type=str, required=True, help="Path to config file")
parser.add_argument("--model_path", type=str, default="hope_model.pth", help="Path to saved model")
parser.add_argument("--dataset", type=str, default="wikitext-2", help="Dataset name")
args = parser.parse_args()
with open(args.config, 'r') as f:
config = yaml.safe_load(f)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = HOPE(config)
model.load_state_dict(torch.load(args.model_path, map_location=device))
model.to(device)
dataloader = get_data_loader(
args.dataset,
config['training']['batch_size'],
config['model']['max_seq_len'],
split='test' # or validation
)
print("Starting evaluation...")
loss, ppl = evaluate(model, dataloader, device)
print(f"Evaluation Results - Loss: {loss:.4f}, Perplexity: {ppl:.4f}")
if __name__ == "__main__":
main()