-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_spectral_convolutional_variable_ab.py
190 lines (133 loc) · 4.64 KB
/
test_spectral_convolutional_variable_ab.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
import numpy as np
import matplotlib.pyplot as plt
from scipy.linalg import circulant
"""
In this scripts we estimate the spectral norm of a concatenation of convolutional or locally connected matrices. We see that the mean is approximately the same
while the variance is greater for the convolutional matrices. We can compute the results for different numbers of input and output channels.
"""
def gen_mat_uncor(N,k,sigma):
'''
Get uncorrelated matrix.
'''
mask = np.hstack( (np.ones((1,k) ), np.zeros( (1,N-k) )) )
mask = circulant(mask)
mat = np.random.normal(0,sigma,(N,N))
mat_uncor = mat*mask
return mat_uncor
def gen_mat_cor(N,k,sigma):
'''
Get correlated matrix.
'''
filters = np.hstack((np.random.normal(0,sigma,(1,k)),np.zeros((1,N-k))))
mat_cor = circulant(filters)
return mat_cor
def create_cor_tot(a,b,N,k,sigma):
'''
Create the complete correlated matrix.
'''
mat1 = np.zeros((N*b,N*a))
for i in range(0,b):
st = gen_mat_cor(N,k,sigma)
tmp = st
for j in range(0,a-1):
app = gen_mat_cor(N,k,sigma)
tmp = np.hstack((tmp, app))
mat1[i*N:(i+1)*N,:] = tmp
return mat1
def create_uncor_tot(a,b,N,k,sigma):
'''
Create the complete uncorrelated matrix.
'''
mat2 = np.zeros((N * b, N * a))
for i in range(0, b):
st = gen_mat_cor(N, k, sigma)
tmp = st
for j in range(0, a - 1):
app = gen_mat_uncor(N, k, sigma)
tmp = np.hstack((tmp, app))
mat2[i * N:(i + 1) * N, :] = tmp
return mat2
def measure_total_matrix(a,b,N,k,sigma,num_trials):
'''
Estimate the metrics for the matrix of interest.
'''
sum_l2_mat1 = 0
sum_l2_mat2 = 0
norm_mat1 = np.zeros((num_trials,1))
norm_mat2 = np.zeros((num_trials,1))
max_mat1 = 0
max_mat2 = 0
min_mat1 = 0
min_mat2 = 0
for i in range(0, num_trials):
if i%100 == 0:
print('Inner Iteration: ', i)
mat1 = create_cor_tot(a, b, N, k, sigma)
mat2 = create_uncor_tot(a, b, N, k, sigma)
norm_mat1[i] = np.linalg.norm(mat1, 2)
norm_mat2[i] = np.linalg.norm(mat2, 2)
avg_l2_cor = np.average(norm_mat1)
avg_l2_uncor = np.average(norm_mat2)
max_l2_cor = np.max(norm_mat1)
max_l2_uncor = np.max(norm_mat2)
min_l2_cor = np.min(norm_mat1)
min_l2_uncor = np.min(norm_mat2)
return avg_l2_cor,max_l2_cor,min_l2_cor,avg_l2_uncor,max_l2_uncor,min_l2_uncor
def theory_uncor(a,b,k,epsilon,N):
'''
Estimate the spectral norm for an uncorrelated matrix.
'''
uncor = (1+epsilon)*(np.sqrt(k*a)+np.sqrt(k*b)+5*np.sqrt(np.log(np.maximum(N*a,N*b))/np.log(1+epsilon)))
return uncor
def theory_cor(a,b,k,N):
'''
Estimate the spectral norm for a correlated matrix.
'''
#cor = (1.4*np.sqrt(k))*(np.sqrt(a)+np.sqrt(b)+np.sqrt(2*np.log(4*N)))
cor = 1.4*(np.sqrt(k))*(np.sqrt(a)+np.sqrt(b))
return cor
'''
Set hyperparameters
'''
N = 100
k = 9
sigma = 1
num_trials = 100
ab_max = 5
epsilon = 0.2
avg_l2_cor = np.zeros((ab_max,1))
avg_l2_uncor = np.zeros((ab_max,1))
max_l2_cor = np.zeros((ab_max,1))
max_l2_uncor = np.zeros((ab_max,1))
min_l2_cor = np.zeros((ab_max,1))
min_l2_uncor = np.zeros((ab_max,1))
theory_l2_cor = np.zeros((ab_max,1))
theory_l2_uncor = np.zeros((ab_max,1))
for i in range(0,ab_max):
print('Outer Iteration: ', i)
a = i+2
b = i+2
avg_l2_cor[i],max_l2_cor[i],min_l2_cor[i],avg_l2_uncor[i],max_l2_uncor[i],min_l2_uncor[i] = measure_total_matrix(a, b, N, k, sigma, num_trials)
theory_l2_cor[i] = theory_cor(a,b,k,N)
theory_l2_uncor[i] = theory_uncor(a,b,k,epsilon,N)
'''
Plot results
'''
fig, ax = plt.subplots()
ax.grid(linestyle='--',linewidth=1.5,alpha=0.5,zorder=0)
x_points = np.arange(0,ab_max)+2
ax.plot(x_points,avg_l2_cor,label = "Empirical l2 Dep",linestyle = '-',color = 'xkcd:brick red',linewidth=4.0)
ax.plot(x_points,avg_l2_uncor,label = "Empirical l2 Indep",linestyle = '-',color = 'xkcd:royal blue',linewidth=4.0)
ax.plot(x_points,theory_l2_cor,label = "Theoretical l2 Dep",linestyle = '--',color = 'xkcd:red',linewidth=4.0)
ax.plot(x_points,theory_l2_uncor,label = "Theoretical l2 Indep",linestyle = '--',color = 'xkcd:bright blue',linewidth=4.0)
a = x_points[:]
b = np.reshape(max_l2_cor,(-1))
c = np.reshape(min_l2_cor,(-1))
ax.fill_between(a,b,c,interpolate=True,facecolor='xkcd:brick red',alpha=0.3)
a = x_points[:]
b = np.reshape(max_l2_uncor,(-1))
c = np.reshape(min_l2_uncor,(-1))
ax.fill_between(a,b,c,interpolate=True,facecolor='xkcd:royal blue',alpha=0.3)
plt.ylabel('E[ ||.||_2 ]')
plt.xlabel('a,b size')
plt.legend(loc = 3)