-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplotter.py
213 lines (171 loc) · 7 KB
/
plotter.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
import torch
import pandas as pd
import numpy as np
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
plt.style.use('bmh')
from collections import OrderedDict
from matplotlib.ticker import FormatStrFormatter
from sklearn.decomposition import PCA
from plotting_util import *
import pdb
def pca_directions(weights_accross_training):
pca = PCA(n_components=2)
pca.fit(weights_accross_training)
dirs = pca.components_
return dirs
def plot_loss_landscape(directions,
test_dataset,
support_dataset,
ml,
loss,
weights_over_time,
shapes,
state_dict_template,
plot_dir,
hparams):
gridsize = hparams.plot_gridsize
# not doing that!
# rescaling
# dirs_norms = [get_rescaling_factors(d, shapes) for d in directions]
# last_weights_norm = get_rescaling_factors(weights_over_time[-1], shapes)
# for i in range(len(directions)):
# m = list(np.array(last_weights_norm) / np.array(dirs_norms[i]))
# directions[i] = multiply_filterwise(directions[i], shapes, m)
# print("Rescaled directions!")
offset = np.mean(weights_over_time, axis=0)
w_coeffs = np.dot(weights_over_time - offset, directions.T)
projected_ws = np.dot(w_coeffs, directions) + offset
for i, weights in enumerate(weights_over_time):
err = np.linalg.norm(projected_ws[i] - weights)
print(f"For weight vector {i} in trajectory, we have a projection error of {err:.2f}, which is {err / np.linalg.norm(weights):.2f}%")
# trajectory.append(projected_weights_coeffs)
trajectory = projected_ws
# constructs the test dataset
X, Y = test_dataset
X_s, Y_s = support_dataset
# X_s = X_s[0]
# Y_s = Y_s[0]
print(f"Shape of test data is {X.shape}, and of test labels is {Y.shape}.")
# X = X[0] # TODO update to an average over all test tasks
# Y = Y[0]
# trajectory = []
fig, ax = plt.subplots()
x_traj = [elt[0] for elt in trajectory]
y_traj = [elt[1] for elt in trajectory]
min_x, max_x = min(x_traj), max(x_traj)
range_x = max_x - min_x
margin_x = range_x * 0.1
min_y, max_y = min(y_traj), max(y_traj)
range_y = max_y - min_y
margin_y = range_y * 0.1
print("ranges for plotting", min_x, max_x, min_y, max_y)
grid_x = np.linspace(min_x - margin_x, max_x + margin_x, gridsize)
grid_y = np.linspace(min_y - margin_y, max_y + margin_y, gridsize)
slow_w_loss_grid = np.empty((gridsize, gridsize))
ft_loss_grid = np.empty((gridsize, gridsize))
magn_grid = np.empty((gridsize, gridsize))
vectors_grid_x = np.empty((gridsize, gridsize))
vectors_grid_y = np.empty((gridsize, gridsize))
accuracy = np.empty((gridsize, gridsize))
for i in range(gridsize):
for j in range(gridsize):
tup = loss_eval(grid_x[j],
grid_y[i],
offset,
loss,
directions,
X_s, Y_s,
X, Y,
ml,
shapes,
state_dict_template,
hparams)
slow_w_loss_grid[i, j], ft_loss_grid[i, j], magn_grid[i, j], v, accuracy[i, j] = tup
vectors_grid_x[i, j], vectors_grid_y[i, j] = v[0], v[1]
print(f"At {i}, {j}, w\ coords {grid_x[j]}, {grid_y[i]}\
the loss from slow weights is {slow_w_loss_grid[i, j]},\
the loss after fine-tuning is {ft_loss_grid[i, j]},\
projected directions are {vectors_grid_x[i, j]} \
and {vectors_grid_x[i, j], vectors_grid_y[i, j]}")
# ft_loss_grid = np.clip(ft_loss_grid, 0, 20)
vectors_grid_x /= range_x
vectors_grid_y /= range_y
# print("END LOSS IS:")
# print(loss_eval(0, 0, offset, loss, directions, X, Y, ml, k_query, shapes, state_dict_template))
# print("SLIGHT PERTUBATION OF IT IS:")
# print(loss_eval(0.01, 0.01, offset, loss, directions, X, Y, ml, k_query, shapes, state_dict_template))
# print("SWING OF IT IS:")
# print(loss_eval(-0.09, 0.0025, offset, loss, directions, X, Y, ml, k_query, shapes, state_dict_template))
gx, gy = np.meshgrid(grid_x, grid_y)
C = ax.contourf(gx, gy, ft_loss_grid,
levels=gridsize,
cmap=plt.cm.coolwarm)
cbar = fig.colorbar(C)
# cbar.ax.set_yticklabels(["{:4.3f}".format(i) for i in cbar.ax.get_yticks()])
# cbar.ax.set_major_formatter(FormatStrFormatter('%.2f'))
print("Got contour plot!")
print("LOSS GRID IS:")
print(ft_loss_grid)
# plots the trajectory
plt.plot(x_traj, y_traj, c='black', lw='3')
plt.scatter(x_traj[-1], y_traj[-1], marker='X', c='gold', s=300)
plt.quiver(gx, gy, vectors_grid_x, vectors_grid_y)
title = '_'.join([hparams.meta_learner, \
hparams.dataset, \
str(hparams.lr_finetune), \
str(hparams.n_inner_iter), \
str(hparams.index)])
# ax.set_title(title)
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
if hparams.fix_extractor:
addon = "fe_"
elif hparams.fix_head:
addon = "fh_"
else:
addon = ""
plt.savefig(f"{plot_dir}/{addon}{title}.png", bbox_inches='tight')
plt.close()
fig, ax = plt.subplots()
C = ax.contourf(gx, gy, accuracy,
levels=np.linspace(0,1.0,10),
cmap=plt.cm.Greens)
print("Got contour plot!")
print("ACC GRID IS:")
print(accuracy)
cbar = fig.colorbar(C)
# plots the trajectory
plt.plot(x_traj, y_traj, c='black', lw='3')
plt.scatter(x_traj[-1], y_traj[-1], marker='X', c='gold', s=300)
plt.quiver(gx, gy, vectors_grid_x, vectors_grid_y)
# ax.set_title(title)
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
if hparams.fix_extractor:
addon = "fe_"
elif hparams.fix_head:
addon = "fh_"
else:
addon = ""
plt.savefig(f"{plot_dir}/{addon}acc_{title}.png", bbox_inches='tight')
return title
def plot_progress(log, hparams):
df = pd.DataFrame(log)
fig, ax = plt.subplots(figsize=(6, 4))
train_df = df[df['mode'] == 'train']
test_df = df[df['mode'] == 'test']
ax.plot(train_df['epoch'], train_df['acc'], label='Train')
ax.plot(test_df['epoch'], test_df['acc'], label='Test')
ax.set_xlabel('Epoch')
ax.set_ylabel('Accuracy')
# ax.set_ylim(70, 100)
fig.legend(ncol=2, loc='lower right')
fig.tight_layout()
title = '_'.join([hparams.meta_learner, \
hparams.dataset])
ax.set_title(title)
print(f'--- Plotting accuracy to {title}')
fig.savefig(f"plots/train/{title}.png", bbox_inches='tight')
plt.close(fig)