-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot_metrics.py
More file actions
46 lines (37 loc) · 1.32 KB
/
plot_metrics.py
File metadata and controls
46 lines (37 loc) · 1.32 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
import json
import matplotlib.pyplot as plt
def main():
try:
with open('metrics.json', 'r') as f:
metrics = json.load(f)
except FileNotFoundError:
print("metrics.json not found. Please run train.py first to generate metrics.")
return
epochs = metrics['epochs']
losses = metrics['losses']
margins = metrics['margins']
fig, ax1 = plt.subplots(figsize=(10, 6))
# Plot DPO Loss
color = 'tab:red'
ax1.set_xlabel('Epochs')
ax1.set_ylabel('DPO Loss', color=color)
ax1.plot(epochs, losses, color=color, label='Loss', marker='o')
ax1.tick_params(axis='y', labelcolor=color)
# Plot Margin on the second y-axis
ax2 = ax1.twinx()
color = 'tab:blue'
ax2.set_ylabel('Margin (Chosen - Rejected)', color=color)
ax2.plot(epochs, margins, color=color, label='Margin', marker='s')
ax2.tick_params(axis='y', labelcolor=color)
# Formatting and saving
fig.tight_layout()
plt.title('DPO Training Metrics (Loss & Margin)')
plt.grid(True, alpha=0.3)
# Save the plot
output_path = 'dpo_metrics.png'
plt.savefig(output_path, dpi=300)
print(f"Plot successfully saved as {output_path}")
# Check if we should display it immediately (useful if running in GUI env)
# plt.show()
if __name__ == "__main__":
main()