-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstore_plot_metrics.py
208 lines (149 loc) · 5.16 KB
/
store_plot_metrics.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# Storing and plotting the metrics
class StoreMetrics:
'''
A class for storing numerous metrics; loss, accuracy, and F1 score.
Attributes:
-----------
num_epochs: int
The total number of epochs in the training and validation loops
Methods:
--------
store_loss(t_loss, v_loss, loss_out_path):
Method to store the loss data in a dataframe.
store_acc(t_acc, v_acc, acc_out_path):
Method to store the accuracy data in a dataframe.
store_f1(t_f1, v_f1, f1_out_path):
Method to store the F1 score data in a dataframe.
'''
def __init__(self, num_epochs):
'''
Initializes the StoreMetrics to store metrics such as loss, accuracy,
and F1 score, both for training and validation sets.
Parameters
----------
num_epochs: int
The total number of epochs in the training and validation loops
Returns
-------
None.
'''
self.num_epochs = num_epochs
def store_loss(self, t_loss, v_loss, loss_out_path):
'''
Method to store the loss data in a dataframe.
It saves the dataframe into a csv file in the specified path.
Parameters
----------
t_loss: list
List of training loss values for each epoch.
v_loss: list
List of validation loss values for each epoch.
loss_out_path: str
Output path to which the csv file will be saved.
Returns
-------
None.
'''
loss_data = pd.DataFrame({
'Epoch': list(range(1, num_epochs + 1)),
'Training Loss': t_loss,
'Validation Loss': v_loss
})
loss_data.to_csv(loss_out_path, index=False)
def store_acc(self, t_acc, v_acc, acc_out_path):
'''
Method to store the accuracy data in a dataframe.
It saves the dataframe into a csv file in the specified path.
Parameters
----------
t_acc: list
List of training accuracy values for each epoch.
v_acc: list
List of validation accuracy values for each epoch.
acc_out_path: str
Output path to which the csv file will be saved.
Returns
-------
None.
'''
acc_data = pd.DataFrame({
'Epoch': list(range(1, num_epochs + 1)),
'Training Accuracy': t_acc,
'Validation Accuracy': v_acc
})
acc_data.to_csv(acc_out_path, index=False)
def store_f1(self, t_f1, v_f1, f1_out_path):
'''
Method to store the F1 score data in a dataframe.
It saves the dataframe into a csv file in the specified path.
Parameters
----------
t_f1: list
List of training F1 scores for each epoch.
v_f1: list
List of validation F1 scores for each epoch.
f1_out_path: str
Output path to which the csv file will be saved.
Returns
-------
None.
'''
f1_data = pd.DataFrame({
'Epoch': list(range(1, num_epochs + 1)),
'Training Accuracy': t_f1,
'Validation Accuracy': v_f1
})
f1_data.to_csv(f1_out_path, index=False)
# Plot loss and accuracy with respect to epoch number
class MetricPlotter:
'''
A class for plotting numerous loss and accuracy for training and validation sets.
Methods:
--------
plot_loss(t_loss, v_loss):
Plots the loss function for training and validation.
plot_acc(t_acc, v_acc):
Plots the accuracy function for training and validation.
'''
@staticmethod
def plot_loss(self, t_loss, v_loss):
'''
Plots the loss function for training and validation.
Parameters
----------
t_loss: list
List of training loss values for each epoch.
v_loss: list
List of validation loss values for each epoch.
Returns
-------
None.
'''
plt.plot(t_loss, color='green')
plt.plot(v_loss, color='blue')
plt.title("Loss Calculation")
plt.xlabel('Epoch Number')
plt.ylabel('Loss')
plt.legend(['Training loss', 'Validation loss'])
plt.show()
@staticmethod
def plot_acc(self, t_acc, v_acc):
'''
Plots the accuracy function for training and validation.
Parameters
----------
t_acc: list
List of training accuracy values for each epoch.
v_acc: list
List of validation accuracy values for each epoch.
Returns
-------
None.
'''
plt.plot(t_acc, color='green')
plt.plot(v_acc, color='blue')
plt.title("Accuracy Calculation")
plt.xlabel('Epoch Number')
plt.ylabel('Accuracy')
plt.legend(['Training accuracy', 'Validation accuracy'])
plt.show()