-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy paththeory.py
175 lines (132 loc) · 4.4 KB
/
theory.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
import os
import pickle
import numpy as np
from os.path import join
from training_utils import append
class LinearNetwork (object):
def __init__ (self, Ws, w_star, q=0.5, wd=0., eta=0.001, cov=None, out_dir="."):
self.out_dir = out_dir
os.makedirs(out_dir, exist_ok=True)
self.W0s = Ws.copy() # store initial values
self.Ws = self.W0s.copy()
self.w_star = w_star
# add checks on matrix dimensions here
assert (q > 0) and (q <= 1), "Probability of synaptic connection out of bounds (0,1]."
self.q = q # probability of synaptic connection
self.wd = wd # weight decay
self.eta = eta # learning rate
self.d_input = self.Ws[0].shape[-1]
self.d_output = self.Ws[-1].shape[0]
if cov is None:
cov = np.eye(self.d_input)
self.cov = cov
self._M = self.cov + np.diagflat( np.diagonal(self.cov) )/self.q
def save (self, filename):
with open(join(self.out_dir, filename), "wb") as f:
pickle.dump(self.Ws, f)
def load (self, filename):
with open(join(self.out_dir, filename), "rb") as f:
self.Ws = pickle.load(f)
def step (self, t):
'''
One-step update of the weights
------------------------------
'''
_del_pref = 2 / self.d_output
_reg_pref = _del_pref * (1 - self.q) / self.q
W = self.Ws[0].copy()
a = self.Ws[1].copy()
_w = np.dot( a, W )
_v = np.dot( (self.w_star - _w), self.cov )
# gradient of av loss term for W
_del_W = _del_pref * np.dot( a.T, _v )
# gradient of av loss term for a
_del_a = _del_pref * np.dot( _v, W.T )
# "regularisation" term for W
_reg_W = np.diagonal( np.dot(a.T, a) )
_reg_W = _reg_W[:,None] * np.dot( W, self._M )
_reg_W = _reg_pref * _reg_W
_reg_W = _reg_W + self.wd * W
# "regularisation" term for a
_reg_a = np.dot( W, self._M )
_reg_a = np.dot( _reg_a, W.T )
_reg_a = a * np.diagonal(_reg_a)[None,:]
_reg_a = _reg_pref * _reg_a
_reg_a = _reg_a + self.wd * a
# weight update
self.Ws[0] = W + self.eta * ( _del_W - _reg_W )
self.Ws[1] = a + self.eta * ( _del_a - _reg_a )
def simulate (self, n_steps, W0s=None, n_save=1, saved_steps=None):
if W0s is None:
W0s = self.W0s.copy()
self.Ws = W0s
if saved_steps is None:
n_save = min( n_steps, n_save )
saved_steps = np.linspace(0, n_steps, n_save+1).astype(int)
_weights = [np.array([]) for _ in range(len(self.Ws))]
_loss = []
for n in range(n_steps+1):
if n in saved_steps:
_loss.append(self.loss)
for l, W in enumerate(self.Ws):
_weights[l] = append(_weights[l], W)
np.save( join(self.out_dir, f"weights_{l+1}.npy"), _weights[l] )
self.step(n)
_loss = np.array(_loss)
return saved_steps, _weights, _loss
@property
def loss (self):
W = self.Ws[0].copy()
a = self.Ws[1].copy()
_w = np.dot( a, W )
_v = np.dot( (self.w_star - _w), self.cov )
_v = np.dot( _v, (self.w_star - _w).T )
_loss = np.trace(_v)
_WMW = np.dot( W, self._M )
_WMW = np.dot( _WMW, W.T )
_AA = np.dot( a.T, a )
_loss =_loss + (1 - self.q) / self.q * np.dot( np.diagonal( _WMW ), np.diagonal(_AA) )
# reduction="mean" in pytorch loss function gives the average
# loss function across units -- here, `_loss` is the sum
return _loss / self.d_output
if __name__ == "__main__":
from plot_utils import (plot_alignment_layers, plot_alignment_wstar,
plot_singular_values, plot_loss_accuracy,
plot_weights, plot_hidden_units,
plot_covariance)
N = 128
D = 7
d = 4
W = np.random.randn(N,D)/np.sqrt(D)
# W = np.random.randn(N,D)/1000.
a = np.random.randn(d,N)/np.sqrt(N)
# a = np.random.randn(d,N)/1000.
# w_star = np.random.randn(d,D)
# w_star = w_star / np.sqrt(np.sum(w_star**2, axis=1))[:, None]
w_star = np.array([
[1,1,0,1,0,0,0],
[1,1,0,0,1,0,0],
[1,0,1,0,0,1,0],
[1,0,1,0,0,0,1]
]).astype(float)
ln = LinearNetwork(
[W, a],
w_star,
q = 0.5,
eta=.001 * 10, # learning rate multiplied by the number of batches in the actual network simulation
out_dir="test_theory")
n_steps = 1000
n_save = 100
saved_epochs, weights_list = ln.simulate(n_steps, n_save=n_save)
print("Calculataing SVD of weights...")
Us = []
Ss = []
Vs = []
for l, W in enumerate(weights_list):
print(f"\t\tLayer {l+1}, {W.shape}", end=" ")
U, S, Vh = np.linalg.svd(W)
Us.append(U)
Ss.append(S)
Vs.append(Vh)
print("Done")
plot_singular_values (Ss, epochs=saved_epochs, out_dir=ln.out_dir)