-
Notifications
You must be signed in to change notification settings - Fork 0
/
plots_convolutional.py
160 lines (142 loc) · 4.3 KB
/
plots_convolutional.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
import matplotlib.pyplot as plt
import numpy as np
import torch
from typing import List, Any
from sklearn.linear_model import LinearRegression
from utils import custom_round
import seaborn as sns
sns.set(
font="Helvetica",
rc={
"axes.axisbelow": False,
"axes.edgecolor": "lightgrey",
"axes.facecolor": "None",
"axes.grid": False,
"axes.labelcolor": "dimgrey",
"axes.spines.right": False,
"axes.spines.top": False,
"figure.facecolor": "white",
"lines.solid_capstyle": "round",
"patch.edgecolor": "w",
"patch.force_edgecolor": True,
"text.color": "dimgrey",
"xtick.bottom": False,
"xtick.color": "dimgrey",
"xtick.direction": "out",
"xtick.top": False,
"ytick.color": "dimgrey",
"ytick.direction": "out",
"ytick.left": False,
"ytick.right": False,
},
)
sns.set_context(
"notebook", rc={"font.size": 18, "axes.titlesize": 20, "axes.labelsize": 18}
)
# load
PATH = "./scaling/"
_depths = [8, 11, 12, 14, 16, 20, 24, 28, 33, 42, 50, 65, 80, 100, 121]
depths = [f"{depth:03d}" for depth in _depths]
sv = [
torch.load(PATH + f"dataset-cifar/act-relu/depth_" + d + "/spectral/sv_dict.pth")
for d in depths
]
def plot_max_norms(
tensor_lists: List[List[torch.tensor]], _depths, path, beta=True, offset: int = 0
):
fig = plt.figure()
ax = plt.gca()
ax.scatter(
sum([[x] * len(t_l) for x, t_l in zip(_depths, tensor_lists)], []),
sum([[np.max(x) for x in tensor_list] for tensor_list in tensor_lists], []),
s=1,
c="blue",
)
max_values = [
np.max([np.max(x) for x in tensor_list]) for tensor_list in tensor_lists
]
lr_max = LinearRegression().fit(
np.log(_depths[offset:]).reshape(-1, 1),
np.log(np.array(max_values)[offset:]).reshape(-1, 1),
)
beta_max = custom_round(-float(lr_max.coef_), 1)
label = (
r"Max norm: $\beta={}$".format(beta_max)
if beta
else r"Max norm: $\alpha={}$".format(beta_max)
)
ax.plot(
[x for x in _depths],
max_values,
linewidth=3,
label=label,
c="red",
linestyle="solid",
)
ax.set_yscale("log")
ax.set_xscale("log")
ax.set_ylim([0, 10])
plt.legend(loc="lower left", fontsize=16)
plt.show(path)
#############################################################
# load
PATH = "./scaling/"
_depths = [8, 11, 12, 14, 16, 20, 24, 28, 33, 42, 50, 65, 80, 100, 121]
depths = [f"{depth:03d}" for depth in _depths]
sv = [
torch.load(PATH + f"dataset-cifar/act-relu/depth_" + d + "/spectral/sv_dict.pth")
for d in depths
]
diffs_sv = [
torch.load(PATH + f"dataset-cifar/act-relu/depth_" + d + "/weights/diffs_sv.pth")
for d in depths
]
def plot_rssq_and_increments(
tensor_lists: List[List[torch.tensor]],
tensor_diff_lists: List[List[torch.tensor]],
_depths,
beta,
path,
offset: int = 0,
):
# we take the max before doing any op because we have the 10 largest singular values!
fig = plt.figure()
ax = plt.gca()
rssq_values = [
np.sqrt(np.sum([np.max(x) ** 2 for x in tensor_list]))
for tensor_list in tensor_lists
]
lr_rssq = LinearRegression().fit(
np.log(_depths[offset:]).reshape(-1, 1),
np.log(np.array(rssq_values)[offset:]).reshape(-1, 1),
)
beta_rssq = custom_round(-float(lr_rssq.coef_), 1)
ax.plot(
[x for x in _depths],
rssq_values,
linewidth=3,
label=r"Root SSQ: ${}$".format(beta_rssq),
c="magenta",
linestyle="solid",
)
increment_values = [
(_depths[i] ** beta) * np.max([np.max(x) for x in tensor_list])
for i, tensor_list in enumerate(tensor_diff_lists)
]
lr_increment = LinearRegression().fit(
np.log(_depths[offset:]).reshape(-1, 1),
np.log(np.array(increment_values)[offset:]).reshape(-1, 1),
)
beta_increment = custom_round(-float(lr_increment.coef_), 1)
ax.plot(
[x for x in _depths],
increment_values,
linewidth=3,
label=r"Increment: ${}$".format(beta_increment),
c="orange",
linestyle="solid",
)
ax.set_yscale("log")
ax.set_xscale("log")
plt.legend(loc="right", fontsize=16)
plt.savefig(path)