-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot_2D.py
367 lines (310 loc) · 13.9 KB
/
plot_2D.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
"""
2D plotting funtions
"""
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import pyplot as plt
from matplotlib import cm
import h5py
import argparse
import numpy as np
from os.path import exists
import seaborn as sns
import copy
def plot_2d_contour(surf_file, surf_name='train_loss', vmin=0.1, vmax=10, vlevel=0.5, show=False):
"""Plot 2D contour map and 3D surface."""
f = h5py.File(surf_file, 'r')
x = np.array(f['xcoordinates'][:])
y = np.array(f['ycoordinates'][:])
X, Y = np.meshgrid(x, y)
if surf_name in f.keys():
Z = np.array(f[surf_name][:])
elif surf_name == 'train_err' or surf_name == 'test_err' :
Z = 100 - np.array(f[surf_name][:])
else:
print ('%s is not found in %s' % (surf_name, surf_file))
print('------------------------------------------------------------------')
print('plot_2d_contour')
print('------------------------------------------------------------------')
print("loading surface file: " + surf_file)
print('len(xcoordinates): %d len(ycoordinates): %d' % (len(x), len(y)))
print('max(%s) = %f \t min(%s) = %f' % (surf_name, np.max(Z), surf_name, np.min(Z)))
#print(Z)
if (len(x) <= 1 or len(y) <= 1):
print('The length of coordinates is not enough for plotting contours')
return
# --------------------------------------------------------------------
# Plot 2D contours
# --------------------------------------------------------------------
fig = plt.figure()
CS = plt.contour(X, Y, Z, cmap='summer', levels=np.arange(vmin, vmax, vlevel))
plt.clabel(CS, inline=1, fontsize=8)
fig.savefig(surf_file + '_' + surf_name + '_2dcontour' + '.pdf', dpi=300,
bbox_inches='tight', format='pdf')
fig = plt.figure()
print(surf_file + '_' + surf_name + '_2dcontourf' + '.pdf')
CS = plt.contourf(X, Y, Z, cmap='summer', levels=np.arange(vmin, vmax, vlevel))
fig.savefig(surf_file + '_' + surf_name + '_2dcontourf' + '.pdf', dpi=300,
bbox_inches='tight', format='pdf')
# --------------------------------------------------------------------
# Plot 2D heatmaps
# --------------------------------------------------------------------
fig = plt.figure()
sns_plot = sns.heatmap(Z, cmap='viridis', cbar=True, vmin=vmin, vmax=vmax,
xticklabels=False, yticklabels=False)
sns_plot.invert_yaxis()
sns_plot.get_figure().savefig(surf_file + '_' + surf_name + '_2dheat.pdf',
dpi=300, bbox_inches='tight', format='pdf')
# --------------------------------------------------------------------
# Plot 3D surface
# --------------------------------------------------------------------
plt.rcParams['grid.color'] = "blue"
# params = {"ytick.color" : "blue",
# "grid.color" : "blue",
# "xtick.color" : "blue",
# "axes.labelcolor" : "blue",
# "axes.edgecolor" : "black"}
fig = plt.figure()
#ax = Axes3D(fig)
ax = fig.gca(projection='3d')
#ax.azim = 10
ax.elev = 40
ax.w_xaxis.set_pane_color((0.0, 0.0, 0.0, 1))
ax.w_yaxis.set_pane_color((0.0, 0.0, 0.0, 1))
ax.w_zaxis.set_pane_color((0.0, 0.0, 0.0, 1))
#ax.set_facecolor('black')
#fig.set_facecolor('black')
# ax.w_xaxis.pane.fill = False
# ax.w_yaxis.pane.fill = False
# ax.w_zaxis.pane.fill = False
# hacky outlier trimming
#Z = np.clip(Z, 0, 22)
Z = np.log(Z)
radius = X.shape[0] / 2
# loop thru every index of X.shape
# keep elements whose indices euclidian distance is less than the radius
ax.set_zlim(0, np.max(Z))
for i in range(X.shape[0]):
for j in range(X.shape[0]):
dist_center = ((i-radius)**2 + (j-radius)**2)**0.5
if dist_center > radius:
Z[i][j] = np.nan
Z = np.ma.array(Z, mask=np.isnan(Z))
#print(np.max(Z))
# default coolwarm, tried plasma, Greens_r
cmap = copy.copy(cm.get_cmap("coolwarm"))
cmap.set_bad(alpha=0)
surf = ax.plot_surface(X, Y, Z, cmap=cmap, alpha=0.9, rstride=1, cstride=1, linewidth=0, antialiased=True, vmin=0, vmax=np.max(Z))
fig.colorbar(surf, shrink=0.5, aspect=5)
fig.savefig(surf_file + '_' + surf_name + '_3dsurface.pdf', dpi=600,
bbox_inches='tight', format='pdf')
f.close()
if show: plt.show()
def plot_trajectory(proj_file, dir_file, show=False):
""" Plot optimization trajectory on the plane spanned by given directions."""
assert exists(proj_file), 'Projection file does not exist.'
f = h5py.File(proj_file, 'r')
fig = plt.figure()
plt.plot(f['proj_xcoord'], f['proj_ycoord'], marker='.')
plt.tick_params('y', labelsize='x-large')
plt.tick_params('x', labelsize='x-large')
f.close()
if exists(dir_file):
f2 = h5py.File(dir_file,'r')
if 'explained_variance_ratio_' in f2.keys():
ratio_x = f2['explained_variance_ratio_'][0]
ratio_y = f2['explained_variance_ratio_'][1]
plt.xlabel('1st PC: %.2f %%' % (ratio_x*100), fontsize='xx-large')
plt.ylabel('2nd PC: %.2f %%' % (ratio_y*100), fontsize='xx-large')
f2.close()
fig.savefig(proj_file + '.pdf', dpi=300, bbox_inches='tight', format='pdf')
if show: plt.show()
def plot_contour_trajectory(surf_file, dir_file, proj_file, surf_name='loss_vals',
vmin=0.1, vmax=10, vlevel=0.5, show=False):
"""2D contour + trajectory"""
assert exists(surf_file) and exists(proj_file) and exists(dir_file)
# plot contours
f = h5py.File(surf_file,'r')
x = np.array(f['xcoordinates'][:])
y = np.array(f['ycoordinates'][:])
X, Y = np.meshgrid(x, y)
if surf_name in f.keys():
Z = np.array(f[surf_name][:])
fig = plt.figure()
CS1 = plt.contour(X, Y, Z, levels=np.arange(vmin, vmax, vlevel))
CS2 = plt.contour(X, Y, Z, levels=np.logspace(1, 8, num=8))
# plot trajectories
pf = h5py.File(proj_file, 'r')
plt.plot(pf['proj_xcoord'], pf['proj_ycoord'], marker='.')
# plot red points when learning rate decays
# for e in [150, 225, 275]:
# plt.plot([pf['proj_xcoord'][e]], [pf['proj_ycoord'][e]], marker='.', color='r')
# add PCA notes
df = h5py.File(dir_file,'r')
ratio_x = df['explained_variance_ratio_'][0]
ratio_y = df['explained_variance_ratio_'][1]
plt.xlabel('1st PC: %.2f %%' % (ratio_x*100), fontsize='xx-large')
plt.ylabel('2nd PC: %.2f %%' % (ratio_y*100), fontsize='xx-large')
df.close()
plt.clabel(CS1, inline=1, fontsize=6)
plt.clabel(CS2, inline=1, fontsize=6)
fig.savefig(proj_file + '_' + surf_name + '_2dcontour_proj.pdf', dpi=300,
bbox_inches='tight', format='pdf')
pf.close()
if show: plt.show()
def plot_2d_eig_ratio(surf_file, val_1='min_eig', val_2='max_eig', show=False):
""" Plot the heatmap of eigenvalue ratios, i.e., |min_eig/max_eig| of hessian """
print('------------------------------------------------------------------')
print('plot_2d_eig_ratio')
print('------------------------------------------------------------------')
print("loading surface file: " + surf_file)
f = h5py.File(surf_file,'r')
x = np.array(f['xcoordinates'][:])
y = np.array(f['ycoordinates'][:])
X, Y = np.meshgrid(x, y)
Z1 = np.array(f[val_1][:])
Z2 = np.array(f[val_2][:])
# Plot 2D heatmaps with color bar using seaborn
abs_ratio = np.absolute(np.divide(Z1, Z2))
print(abs_ratio)
fig = plt.figure()
sns_plot = sns.heatmap(abs_ratio, cmap='viridis', vmin=0, vmax=.5, cbar=True,
xticklabels=False, yticklabels=False)
sns_plot.invert_yaxis()
sns_plot.get_figure().savefig(surf_file + '_' + val_1 + '_' + val_2 + '_abs_ratio_heat_sns.pdf',
dpi=300, bbox_inches='tight', format='pdf')
# Plot 2D heatmaps with color bar using seaborn
ratio = np.divide(Z1, Z2)
print(ratio)
fig = plt.figure()
sns_plot = sns.heatmap(ratio, cmap='viridis', cbar=True, xticklabels=False, yticklabels=False)
sns_plot.invert_yaxis()
sns_plot.get_figure().savefig(surf_file + '_' + val_1 + '_' + val_2 + '_ratio_heat_sns.pdf',
dpi=300, bbox_inches='tight', format='pdf')
f.close()
if show: plt.show()
def plot_3d_scatter(surf_file, surf_name='test_loss', show=False):
"""Plot 3D scatterplot with color and size mapping."""
f = h5py.File(surf_file, 'r')
x = np.array(f['xcoordinates'][:])
y = np.array(f['ycoordinates'][:])
z = np.array(f['zcoordinates'][:])
X, Y, Z = np.meshgrid(x, y, z)
if surf_name in f.keys():
loss = np.array(f[surf_name][:])
elif surf_name == 'train_err' or surf_name == 'test_err':
print("UMMMM....")
loss = 100 - np.array(f[surf_name][:])
else:
print ('%s is not found in %s' % (surf_name, surf_file))
#loss = np.log(loss+1)
print("loading surface file: " + surf_file)
print('max(%s) = %f \t min(%s) = %f' % (surf_name, np.max(loss), surf_name, np.min(loss)))
print("the above could be after logging!")
if (len(x) <= 1 or len(y) <= 1):
print('The length of coordinates is not enough for plotting contours')
return
# --------------------------------------------------------------------
# Plot 3d scatter plot
# --------------------------------------------------------------------
fig = plt.figure()
#plt.rcParams['grid.color'] = "blue"
from mpl_toolkits.mplot3d import Axes3D
ax = fig.add_subplot(111, projection='3d')
#print(loss.shape, loss)
s = np.copy(loss)
# shift to 0
s = s - np.amin(s)
# mirror the values
s = np.amax(s) - s
# softmax scaling
s = np.exp(s - np.max(s))
s /= s.sum()
clr = np.copy(s)
# multiply by a constant
c = 7500
s *= c
#print(np.amax(s), np.amin(s))
#cm = plt.get_cmap("copper_r")
#ax.elev = 40
# ax.w_xaxis.set_pane_color((0.0, 0.0, 0.0, 1))
# ax.w_yaxis.set_pane_color((0.0, 0.0, 0.0, 1))
# ax.w_zaxis.set_pane_color((0.0, 0.0, 0.0, 1))
p = ax.scatter(X, Y, Z, c=clr, s=s, depthshade=False, alpha=0.7)
fig.colorbar(p, fraction=0.013, pad=0.05)
#plt.clabel(CS, inline=1, fontsize=8)
fig.savefig(surf_file + '_' + surf_name + '_3dscatter' + '.png', dpi=1200,
bbox_inches='tight', format='png')
if show: plt.show()
def plot_4d_path(surf_file, surf_name='test_loss', show=False):
"""Plot 3D scatterplot over time using color and size mapping."""
f = h5py.File(surf_file, 'r')
x = np.array(f['xcoordinates'][:])
y = np.array(f['ycoordinates'][:])
z = np.array(f['zcoordinates'][:])
t = np.array(f['tcoordinates'][:])
X, Y, Z, T = np.meshgrid(x, y, z, t)
if surf_name in f.keys():
loss = np.array(f[surf_name][:])
elif surf_name == 'train_err' or surf_name == 'test_err':
print("UMMMM....")
loss = 100 - np.array(f[surf_name][:])
else:
print ('%s is not found in %s' % (surf_name, surf_file))
#loss = np.log(loss+1)
print("loading surface file: " + surf_file)
print('max(%s) = %f \t min(%s) = %f' % (surf_name, np.max(loss), surf_name, np.min(loss)))
print("the above could be after logging!")
if (len(x) <= 1 or len(y) <= 1):
print('The length of coordinates is not enough for plotting contours')
return
# --------------------------------------------------------------------
# Plot 3d scatter plot
# --------------------------------------------------------------------
fig = plt.figure()
#plt.rcParams['grid.color'] = "blue"
from mpl_toolkits.mplot3d import Axes3D
ax = fig.add_subplot(111, projection='3d')
#print(loss.shape, loss)
s = np.copy(loss)
# shift to 0
s = s - np.amin(s)
# mirror the values
s = np.amax(s) - s
# softmax scaling
s = np.exp(s - np.max(s))
s /= s.sum()
clr = np.copy(s)
# multiply by a constant
c = 7500
s *= c
#print(np.amax(s), np.amin(s))
#cm = plt.get_cmap("copper_r")
#ax.elev = 40
# ax.w_xaxis.set_pane_color((0.0, 0.0, 0.0, 1))
# ax.w_yaxis.set_pane_color((0.0, 0.0, 0.0, 1))
# ax.w_zaxis.set_pane_color((0.0, 0.0, 0.0, 1))
p = ax.scatter(X, Y, Z, c=clr, s=s, depthshade=False, alpha=0.7)
fig.colorbar(p, fraction=0.013, pad=0.05)
#plt.clabel(CS, inline=1, fontsize=8)
fig.savefig(surf_file + '_' + surf_name + '_3dscatter' + '.png', dpi=1200,
bbox_inches='tight', format='png')
if show: plt.show()
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Plot 2D loss surface')
parser.add_argument('--surf_file', '-f', default='', help='The h5 file that contains surface values')
parser.add_argument('--dir_file', default='', help='The h5 file that contains directions')
parser.add_argument('--proj_file', default='', help='The h5 file that contains the projected trajectories')
parser.add_argument('--surf_name', default='train_loss', help='The type of surface to plot')
parser.add_argument('--vmax', default=10, type=float, help='Maximum value to map')
parser.add_argument('--vmin', default=0.1, type=float, help='Miminum value to map')
parser.add_argument('--vlevel', default=0.5, type=float, help='plot contours every vlevel')
parser.add_argument('--zlim', default=10, type=float, help='Maximum loss value to show')
parser.add_argument('--show', action='store_true', default=False, help='show plots')
args = parser.parse_args()
if exists(args.surf_file) and exists(args.proj_file) and exists(args.dir_file):
plot_contour_trajectory(args.surf_file, args.dir_file, args.proj_file,
args.surf_name, args.vmin, args.vmax, args.vlevel, args.show)
elif exists(args.proj_file) and exists(args.dir_file):
plot_trajectory(args.proj_file, args.dir_file, args.show)
elif exists(args.surf_file):
plot_2d_contour(args.surf_file, args.surf_name, args.vmin, args.vmax, args.vlevel, args.show)