-
Notifications
You must be signed in to change notification settings - Fork 1
/
plotting.py
373 lines (260 loc) · 9.36 KB
/
plotting.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
import numpy as np
import numpy.linalg as la
import scipy.io as io
import torch
import json
import query_results as qr
import subprocess
import matplotlib.pyplot as plt
from matplotlib.colors import LinearSegmentedColormap
# Custom colormap
cdict = {'red': ((0.0, 0.0, 0.0),
(0.5, 0.0, 0.0),
(1.0, 1.0, 1.0)),
'green': ((0.0, 0.0, 0.0),
(1.0, 0.0, 0.0)),
'blue': ((0.0, 1.0, 1.0),
(0.5, 0.0, 0.0),
(1.0, 0.0, 0.0))}
mymap = LinearSegmentedColormap('MyMap', cdict)
plt.register_cmap(cmap=mymap)
def plot_scalar(dir: str, name: str):
"""
:param dir: directory of the file storage system whose results we are looking at
:param name: name of the scalar metric we want to visualize
"""
path = 'results/' + dir + '/'
handle = open(path + 'metrics.json')
content = handle.read()
handle.close()
json_dict = json.loads(content)
values = json_dict[name]['values']
plt.plot(values)
plt.title(name + ' ' + dir)
plt.show()
def naive_sort_w_matrix(array):
"""
:param array: array to be sorted
:return: a sorted version of the array, greatest to least, with the appropriate permutation matrix
"""
size = len(array)
def make_transposition(i, j):
mat = np.identity(size)
mat[i, i] = 0
mat[j, j] = 0
mat[i, j] = 1
mat[j, i] = 1
return mat
sort_array = np.zeros(size)
permutation = np.identity(size)
for i in range(size):
big = -float("inf")
ix = i
for j in range(i, size):
if array[j] > big:
big = array[j]
ix = j
sort_array[i] = big
permutation = make_transposition(i, ix) @ permutation
return sort_array, permutation
def order_by_eig_entries(matrix):
vals, vecs = la.eig(matrix)
maxva, val, vec = 0.0, vals[0], vecs[0]
for i in range(len(vals)):
if np.absolute(vals[i]) > maxva:
maxva = np.absolute(vals[i])
val = vals[i]
vec = vecs[i]
svec, permutation = naive_sort_w_matrix(vec)
return permutation
def plot_hidden_weights(dir: str,
dict_name: str,
param_name: str,
vmin: float,
vmax: float,
transform=None,
token="hid"):
"""
:param dir: directory of the file storage system whose results we are looking at
:param dict_name: name of the .pt file whose .weight_hh_l0.weight we will visualize
:param param_name: name of the key in the dictionary we are interested in.
:param vmin: expected minimum weight
:param vmax: expected maximum weight
:param transform: function which gets permutation matrix to reorder the rows or columns of the weights
:param token: 'in', 'hid' or 'out' determines how precisely to transform the matrix
"""
path = 'results/' + dir + '/'
sd = torch.load(path + dict_name, map_location='cpu')
hidden_weights = sd[param_name].detach().numpy()
if len(hidden_weights.shape) < 2:
hidden_weights = hidden_weights.reshape(-1, 1)
#print(hidden_weights.shape)
matrix = None
if not transform == None:
matrix = transform(hidden_weights)
transmat = np.transpose(matrix)
if token == 'hid':
hidden_weights = transmat @ hidden_weights @ matrix
elif token == 'in':
hidden_weights = transmat @ hidden_weights
elif token == 'out':
hidden_weights = hidden_weights @ matrix
#plt.title(name + ' weights ' + dir)
fig = plt.figure(figsize=(8, 8), dpi=200)
ax = fig.add_axes([0.1,0.1,0.8,0.8])
#fig, ax = plt.subplots()
ax.pcolor(hidden_weights, vmin=vmin, vmax=vmax, cmap='MyMap', antialiased=False)
ax.set_aspect('equal')
#fig.set_size(5, 5)
fig.show()
plt.gca().invert_yaxis()
return matrix
def plot_eigs(dir: str, name: str, lim: float):
"""
:param dir: directory of the file storage system whose results we are looking at
:param name: name of the .pt file whose .weight_hh_l0.weight eigenvalues we will visualize
:param lim: how large is the square defining the plot
"""
path = 'results/' + dir + '/'
sd = torch.load(path + name, map_location='cpu')
hidden_weights = sd['rnn.weight_hh_l0.weight'].detach().numpy()
vals, vecs = la.eig(hidden_weights)
fig, ax = plt.subplots()
ax.set_xlim([-lim, lim])
ax.set_ylim([-lim, lim])
ax.scatter(np.real(vals), np.imag(vals), s=6)
ax.set_aspect('equal')
fig.show()
def get_metrics(dirs: str, metric: str):
"""
:param dirs: list of directories for which we will look for the final metric
:param metric: name of the metric we are going to plot
:return: list of metrics after training
"""
result = []
for name in dirs:
handle = open('results/' + name + '/metrics.json')
my_dict = json.loads(handle.read())
handle.close()
result.append(my_dict[metric]['values'][-1])
return result
def get_all_metrics(list_of_configs):
train_loss = []
test_loss = []
valid_loss = []
train_acc = []
test_acc = []
valid_acc = []
for config_dict in list_of_configs:
dirs = qr.find_results(config_dict)
trainLoss = np.mean(get_metrics(dirs, "trainLoss"))
testLoss = np.mean(get_metrics(dirs, "testLoss"))
validLoss = np.mean(get_metrics(dirs, "validLoss"))
train_loss.append(trainLoss)
test_loss.append(testLoss)
valid_loss.append(validLoss)
trainAcc = np.mean(get_metrics(dirs, "trainAccuracy"))
testAcc = np.mean(get_metrics(dirs, "testAccuracy"))
validAcc = np.mean(get_metrics(dirs, "validAccuracy"))
train_acc.append(trainAcc)
test_acc.append(testAcc)
valid_acc.append(validAcc)
return train_loss, test_loss, valid_loss, train_acc, test_acc, valid_acc
def make_bars(labels, title, metrics):
train, test, validate = metrics[0], metrics[1], metrics[2]
x = 2.0*np.arange(len(labels))
width = 0.35
fig, ax = plt.subplots()
rects1 = ax.bar(x - width, train, width, label='Train')
rects2 = ax.bar(x, test, width, label='Test')
rects3 = ax.bar(x + width, validate, width, label='Validation')
ax.tick_params(axis='x', which='major', labelsize=6)
ax.set_xticks(x)
ax.set_xticklabels(labels)
ax.legend()
ax.grid()
plt.title(title)
plt.show()
def make_bar(labels, title, data):
x = np.arange(len(labels))
width = 0.35
fig, ax = plt.subplots()
rects1 = ax.bar(x, data, width)
ax.tick_params(axis='x', which='major', labelsize=6)
ax.set_xticks(x)
ax.set_xticklabels(labels)
ax.legend()
ax.grid()
plt.title(title)
plt.show()
def duration_histogram(name: str, set: str):
songs = io.loadmat('data/' + name)[set][0]
record = np.zeros(32)
for song in songs:
for note in range(88):
on = False
count = 0
for t in range(song.shape[0]):
if song[t, note] == 1:
count += 1
on = True
elif song[t, note] == 0:
if on:
record[count - 1] += 1
count = 0
on = False
else:
raise ValueError("Piano roll should be binary.")
durations = []
labels = []
for i, r in enumerate(record):
if r > 0:
durations.append(r)
labels.append(str(i + 1))
durations = durations[4:]
labels = labels[4:]
x = np.arange(len(labels))
width = 0.35
fig, ax = plt.subplots()
rects1 = ax.bar(x, durations, width)
ax.tick_params(axis='x', which='major', labelsize=6)
ax.set_xticks(x)
ax.set_xticklabels(labels)
#ax.legend()
plt.title("Note duration distribution: " + name + " " + set)
plt.show()
def plot_sklearn_weights(dir: str, vmin, vmax, bias=False):
weights = None
if bias:
weights = np.load('results/' + dir + '/intercepts.npy')
else:
weights = np.load('results/' + dir + '/coefs.npy')
if len(weights.shape) < 2:
weights = weights.reshape(-1, 1)
#plt.title(name + ' weights ' + dir)
fig = plt.figure(figsize=(8, 8), dpi=200)
ax = fig.add_axes([0.1,0.1,0.8,0.8])
#fig, ax = plt.subplots()
ax.pcolor(weights, vmin=vmin, vmax=vmax, cmap='MyMap', antialiased=False)
ax.set_aspect('equal')
#fig.set_size(5, 5)
fig.show()
plt.gca().invert_yaxis()
def training_curve(dir: str, title: str):
metric_dict = json.loads(open('results/' + dir + '/metrics.json').read())
train = metric_dict['trainLoss']['values']
test = metric_dict['testLoss']['values']
val = metric_dict['validLoss']['values']
num_epochs = len(test) - 1
steps_per_epoch = (len(train) - 1)//num_epochs
train_vals = [train[0]]
for i in range(num_epochs):
train_vals.append(np.mean(train[steps_per_epoch*i : steps_per_epoch*(i + 1)]))
plt.plot(range(num_epochs + 1), train_vals, label='Train')
plt.plot(range(num_epochs + 1), test, label='Test')
plt.plot(range(num_epochs + 1), val, label='Validation')
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.title(title)
plt.legend()
plt.show()