-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot.py
More file actions
62 lines (56 loc) · 1.82 KB
/
plot.py
File metadata and controls
62 lines (56 loc) · 1.82 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
import matplotlib.pyplot as plt
from sfd40 import Stanford40HyperParameters
import yaml
def plot(
accuracy: "float",
train_losses: "list[float]",
val_losses: "list[float]",
save_not_show: "bool" = False,
) -> None:
"""
plot is responsible for all the plots generated
after the execution of the models. In this version
it only includes the evolution of training and
validation errors.
"""
_num_of_epochs = range(1, len(train_losses) + 1)
plt.figure(figsize=(12, 8))
plt.plot(_num_of_epochs, train_losses, label="Training Error", marker="o")
plt.plot(_num_of_epochs, val_losses, label="Validation Error", marker="x")
# Add labels, title, legend, and grid
plt.xlabel("Epochs")
plt.ylabel("Error")
plt.title(f"Loss Evolution - Accuracy: {accuracy:.2f} %")
plt.legend()
if save_not_show:
plt.savefig("fig.png")
else:
plt.grid(True)
plt.show()
def save_as_yaml(
accuracy: "float",
model_name: "str",
training_losses: "list[float]",
validation_losses: "list[float]",
hparams: "Stanford40HyperParameters",
):
data = {
"eid": 0,
"nn": model_name,
"hyperparameters": {
"in_channels": hparams.in_channels,
"learning_rate": hparams.learning_rate,
"resize": hparams.resize,
"train_batch_size": hparams.train_batch_size,
"test_batch_size": hparams.test_batch_size,
"val_batch_size": hparams.val_batch_size,
"num_epochs": hparams.num_epochs,
},
"results": {
"accuracy": accuracy,
"training_losses": training_losses,
"validation_losses": validation_losses,
},
}
with open("_example.yml", "w") as outfile:
yaml.dump(data, outfile, default_flow_style=False)