forked from N8python/mlx-pretrain
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot_curve.py
More file actions
63 lines (57 loc) · 2.08 KB
/
plot_curve.py
File metadata and controls
63 lines (57 loc) · 2.08 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
import re
import pandas as pd
import matplotlib.pyplot as plt
def parse_log(file_path):
train_pattern = re.compile(
r"Step (?P<step>\d+): loss=(?P<loss>\d+\.\d+) \| ppl=(?P<ppl>\d+\.\d+) \| lr=(?P<lr>[\d\.e\-]+).*?tokens_per_sec=(?P<tokens_per_sec>\d+\.\d+)"
)
val_pattern = re.compile(r"val_loss=(?P<val_loss>\d+\.\d+) \| val_ppl=(?P<val_ppl>\d+\.\d+)")
train_data = []
val_data = []
with open(file_path, "r") as f:
for line in f:
t = train_pattern.search(line)
if t:
train_data.append({
"step": int(t.group("step")),
"loss": float(t.group("loss")),
"ppl": float(t.group("ppl")),
"lr": float(t.group("lr")),
"tokens_per_sec": float(t.group("tokens_per_sec")),
})
v = val_pattern.search(line)
if v:
val_data.append({
"val_loss": float(v.group("val_loss")),
"val_ppl": float(v.group("val_ppl")),
})
train_df = pd.DataFrame(train_data)
val_df = pd.DataFrame(val_data)
return train_df, val_df
def plot_curves(train_df, val_df):
plt.figure()
plt.plot(train_df["step"], train_df["loss"])
plt.xlabel("Step")
plt.ylabel("Training Loss")
plt.title("Training Loss Curve")
plt.show()
plt.figure()
plt.plot(train_df["step"], train_df["ppl"])
plt.xlabel("Step")
plt.ylabel("Training Perplexity")
plt.title("Training PPL Curve")
plt.show()
if not val_df.empty:
plt.figure()
plt.plot(range(len(val_df)), val_df["val_loss"])
plt.xlabel("Validation Checkpoint")
plt.ylabel("Validation Loss")
plt.title("Validation Loss Curve")
plt.show()
if __name__ == "__main__":
import sys
log_path = sys.argv[1] if len(sys.argv) > 1 else "training.log"
train_df, val_df = parse_log(log_path)
plot_curves(train_df, val_df)
# Save this script as `plot_training_curves.py` and run:
# python plot_training_curves.py path/to/your/logfile.log