-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot_loss.py
More file actions
55 lines (45 loc) · 1.69 KB
/
Copy pathplot_loss.py
File metadata and controls
55 lines (45 loc) · 1.69 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
import matplotlib.pyplot as plt
import csv
import sys
import os
def plot_loss(filename="loss_log.txt"):
if not os.path.exists(filename):
print(f"错误: 找不到文件 {filename}。请先运行 C++ 程序进行训练。")
return
epochs = []
losses = []
print(f"正在读取 {filename} ...")
try:
with open(filename, 'r') as f:
reader = csv.reader(f)
next(reader) # 跳过第一行表头 (epoch,loss)
for row in reader:
if len(row) >= 2:
epochs.append(int(row[0]))
losses.append(float(row[1]))
except Exception as e:
print(f"读取文件出错: {e}")
return
# 开始画图
plt.figure(figsize=(10, 6))
plt.plot(epochs, losses, label='Training Loss', color='blue', linewidth=2)
# 添加标题和标签
plt.title('Neural Network Training Loss Curve', fontsize=16)
plt.xlabel('Epoch', fontsize=12)
plt.ylabel('Average MSE Loss', fontsize=12)
plt.grid(True, which='both', linestyle='--', alpha=0.7)
plt.legend()
# 标记最低点
if losses:
min_loss = min(losses)
min_epoch = epochs[losses.index(min_loss)]
plt.annotate(f'Min Loss: {min_loss:.6f}',
xy=(min_epoch, min_loss),
xytext=(min_epoch, min_loss + 0.05),
arrowprops=dict(facecolor='red', shrink=0.05))
print("正在显示图表...")
plt.show()
if __name__ == "__main__":
# 如果命令行传了参数,就用参数指定的文件名,否则默认 loss_log.txt
log_file = sys.argv[1] if len(sys.argv) > 1 else "loss_log.txt"
plot_loss(log_file)