forked from dingo-actual/dropgrad
-
Notifications
You must be signed in to change notification settings - Fork 0
/
visualize.py
46 lines (39 loc) · 1.48 KB
/
visualize.py
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 os
import torch
import matplotlib.pyplot as plt
def main():
scenarios = [
{"name": "Baseline", "dropout_rate": 0.0, "dropgrad_rate": 0.0},
{"name": "Dropout", "dropout_rate": 0.1, "dropgrad_rate": 0.0},
{"name": "DropGrad", "dropout_rate": 0.0, "dropgrad_rate": 0.1},
{"name": "Dropout+DropGrad", "dropout_rate": 0.1, "dropgrad_rate": 0.1},
]
optimizers = [
"Adam",
"AdamW",
"SGD",
"Adagrad",
"Adadelta",
]
for optimizer_name in optimizers:
plt.figure(figsize=(10, 5))
for scenario in scenarios:
file_path = os.path.join(".", f"losses_{scenario['name']}_{optimizer_name}.pth")
if os.path.exists(file_path):
losses = torch.load(file_path)
train_losses = losses["train_losses"]
test_losses = losses["test_losses"]
plt.plot(train_losses, label=f"{scenario['name']} - Train Loss")
plt.plot(test_losses, label=f"{scenario['name']} - Test Loss")
plt.xlabel("Epoch")
plt.ylabel("Loss")
plt.legend()
plt.title(f"Train and Test Losses - {optimizer_name}")
plt.tight_layout()
output_dir = os.path.join(".", "output")
os.makedirs(output_dir, exist_ok=True)
output_path = os.path.join(output_dir, f"loss_plot_{optimizer_name}.png")
plt.savefig(output_path)
plt.close()
if __name__ == "__main__":
main()