-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot_results.py
More file actions
65 lines (56 loc) · 1.84 KB
/
Copy pathplot_results.py
File metadata and controls
65 lines (56 loc) · 1.84 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
61
62
63
64
65
# %%
import re
from pathlib import Path
from typing import List
import matplotlib.pyplot as plt
# %%
def extract_field(log_path: str, field: str) -> List[str]:
"""
Extract values like: field=value from each line in the log.
Works for fields such as:
best_mrr, best_epoch, model, epochs, batch_size, lr, criterion, R@1, R@5, R@10, checkpoint, etc.
Returns values as strings (convert to float/int if you want).
"""
text = Path(log_path).read_text(encoding="utf-8", errors="ignore").splitlines()
# Escape field for regex (important for things like "R@1")
field_escaped = re.escape(field)
# Capture until next separator: space OR " |" OR end of line
# Handles "field=0.123", "field=mixed", "field=output/file.pt"
pattern = re.compile(rf"\b{field_escaped}=(?P<val>.*?)(?=\s\|\s|\s+$|$)")
out = []
for line in text:
m = pattern.search(line)
if m:
out.append(float(m.group("val").strip()))
return out
# %%
log_file = "output/log_train.log"
field = "best_mrr" # change to whatever you want
values = extract_field(log_file, field)
values
# %%
x = list(range(len(values)))
vals = sorted(values)
plt.scatter(x, vals)
plt.scatter([x[0]], [vals[0]], color="red", zorder=5, label="reference")
plt.plot(sorted(values), linestyle="--", label="MRR improvements")
plt.xlabel("Training Sorted")
plt.ylabel("MRR results")
plt.legend()
plt.show()
# %%
for i in [1, 5, 10]:
field = f"R@{i}" # change to whatever you want
values = extract_field(log_file, field)
vals = sorted(values)
plt.scatter(x, vals)
plt.scatter(
[x[0]], [vals[0]], color="red", zorder=5, label="reference" if i == 1 else None
)
plt.plot(sorted(values), linestyle="--", label=field)
plt.xlabel("Training Sorted")
plt.ylabel("Recall rate")
plt.axhline(y=1.0, label="y=1")
plt.legend()
plt.show()
# %%