-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathkohonen.py
258 lines (221 loc) · 7.89 KB
/
kohonen.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
import numpy as np
import matplotlib.pyplot as plt
import math
import random
import sys
import cPickle
import gzip
import next_permutation
import munkres
def pretty(m):
for row in m:
print "\t".join(map(str, row))
def halfCircle():
x = 1.0
y = 1.0
while x*x+y*y>1.0:
x = random.uniform( 0.0, +1.0)
y = random.uniform(-1.0, +1.0)
return (x,y)
def wave():
x = random.uniform( -math.pi, +math.pi)
y = math.sin(x)+random.uniform( -0.2, +0.2)
return (x,y)
def triangle():
x = random.uniform(-1.0, +1.0)
y = random.uniform(-1.0, x)
return (x,y)
def sampleFromTarget():
# return wave()
return halfCircle()
# return triangle()
def samplesFromTarget(n):
return np.array([sampleFromTarget() for i in xrange(n)])
def samplesFromInit(n, d, e):
norm = np.random.normal(loc=0.0, scale=1.0, size=(n,e))
z = np.zeros((n,d-e))
data = np.hstack((norm, z))
assert data.shape==(n,d)
return data
# Both are (n x d) arrays.
def sumOfDistances(x,y):
return np.sum(np.linalg.norm(x-y, axis=1))
# Both are (n x d) arrays.
# Scales with O(n!) boo!
# We could bring it down by reducing it to minimum-weight
# matching on a complete bipartite graph.
# If we need really large n, then a sequential
# greedy alg is probably more than good enough.
# Probably we'll have something partially parallel that's even
# faster than the naive sequential greedy alg.
def slowOptimalPairing(x,y):
n,d = x.shape
assert y.shape==(n,d)
bestDist = np.inf
bestP = None
for p in next_permutation.next_permutation(range(n)):
dist = sumOfDistances(x[p],y)
if dist<bestDist:
bestDist = dist
bestP = p
return bestP
def distanceMatrix(x, y):
xL2S = np.sum(x*x,axis=-1)
yL2S = np.sum(y*y,axis=-1)
xL2SM = np.tile(xL2S, (len(y), 1))
yL2SM = np.tile(yL2S, (len(x), 1))
squaredDistances = xL2SM + yL2SM.T - 2.0*y.dot(x.T)
# elementwise. abs is to supress negative values caused by rounding errors.
# TODO Should switch to squared distances everywhere, but be careful about fitAndVis fitAndVisNNBaseline.
distances = np.sqrt(np.abs(squaredDistances))
return distances
def optimalPairing(x, y):
distances = distanceMatrix(x,y)
perm = munkres.Munkres().compute(distances)
p = []
for i,(a,b) in enumerate(perm):
assert i==a
p.append(b)
# assert p==slowOptimalPairing(x,y)
return p
class LocalMapping(object):
KERNEL_SIZE = 0.33 # Ad hoc is an understatement
def __init__(self, source, gradient):
self.source = source
self.gradient = gradient
# TODO Make it work on arrays as well.
def __call__(self, x):
y = x.copy()
dists = np.linalg.norm(self.source-x, axis=1)
for (d, g) in zip(dists, self.gradient):
if d<self.KERNEL_SIZE:
y += g * (self.KERNEL_SIZE-d) / self.KERNEL_SIZE
return y
def testLocalMapping():
d = 2
source = np.array( [[0.0, 0.0], [ 1.0, 1.0]])
gradient = np.array([[0.5, 0.5], [-0.5, 0.5]])
f = LocalMapping(source, gradient)
np.testing.assert_array_almost_equal( f(np.array([1.0, 1.0])), np.array([0.95, 1.05]) )
np.testing.assert_array_almost_equal( f(np.array([0.09, 0.0])), np.array([0.095, 0.005]) )
class GlobalMapping(LocalMapping):
def __init__(self, source, gradient, ancestor):
super(GlobalMapping, self).__init__(source, gradient)
self.ancestor = ancestor
def __call__(self, x):
if self.ancestor is None:
return super(GlobalMapping, self).__call__(x)
else:
intermediate = self.ancestor(x)
return super(GlobalMapping, self).__call__(intermediate)
def testGlobalMapping():
source = np.array( [[0.0, 0.0], [ 1.0, 1.0]])
gradient = np.array([[0.5, 0.5], [-0.5, 0.5]])
f = GlobalMapping(source, gradient, None)
g = GlobalMapping(source, gradient, f)
x = np.array([1.0, 1.0])
np.testing.assert_array_almost_equal( f(f(x)), g(x) )
# testLocalMapping()
# testGlobalMapping()
def drawMapping(ax, f):
n = 30
window = 2
ax.set_xlim((-window, +window))
ax.set_ylim((-window, +window))
for x in np.linspace(-window, +window, num=n):
for y in np.linspace(-window, +window, num=n):
x2, y2 = f(np.array([x, y]))
ax.arrow(x, y, x2-x, y2-y, head_width=0.05, head_length=0.1, fc='k', ec='k')
# n is the number of data points.
# e is the dimension of the initial Gaussian.
# (But it's embedded in the d-dimensional feature space.)
# f is the actual mapping, a python function
# mapping from R^d to R^d.
def findMapping(n, e, f, learningRate):
y = samplesFromTarget(n)
d = y.shape[1]
# TODO It's dumb not to init with the e principal components of the data.
init = samplesFromInit(n, d, e)
# TODO vectorize f
x = np.array([f(i) for i in init])
p = optimalPairing(x,y)
x = x[p]
# pretty(np.hstack((x,y)))
source, gradient = x, learningRate*(y-x)
dumpMapping = False
if dumpMapping:
f = LocalMapping(source, gradient)
for xp,yp in zip(x,y):
print xp, yp, f(xp), np.linalg.norm(xp-yp), np.linalg.norm(f(xp)-yp)
return source, gradient
def iteration():
d = 2
e = 2
n = 50
learningRate = 0.3
minibatchCount = 50
plotEvery = 10
plotCount = minibatchCount/plotEvery
f = GlobalMapping(np.zeros((0,d)), np.zeros((0,d)), None)
gaussSample = samplesFromInit(100, d, e)
fig, axarr = plt.subplots(minibatchCount/plotEvery, 3)
fig.set_size_inches(10.0*2, 10.0*plotCount)
fig.subplots_adjust(hspace=0.2, wspace=0.2)
for i in range(minibatchCount):
source, gradient = findMapping(n, e, f, learningRate)
# That's much the same as
# f = lambda x: LocalMapping(source, gradient)(f(x))
f = GlobalMapping(source, gradient, f)
print i,
sys.stdout.flush()
if i%plotEvery==0:
plotIndex = i/plotEvery
drawMapping(axarr[plotIndex][0], f)
drawMapping(axarr[plotIndex][1], LocalMapping(source, gradient))
sampleFromTarget = samplesFromTarget(100)
axarr[plotIndex][2].scatter(sampleFromTarget[:,0], sampleFromTarget[:,1], color='red')
sampleFromLearned = np.array([ f(p) for p in gaussSample ])
axarr[plotIndex][2].scatter(sampleFromLearned[:,0], sampleFromLearned[:,1])
print
plt.savefig("vis.pdf")
def iterationMNIST():
d = 784
e = 10
n = 50
learningRate = 1.0
minibatchCount = 90
plotEvery = 10
plotCount = minibatchCount/plotEvery
f = GlobalMapping(np.zeros((0,d)), np.zeros((0,d)), None)
gaussSample = samplesFromInit(100, d, e)
fig, axarr = plt.subplots(minibatchCount/plotEvery, 3)
fig.set_size_inches(10.0*2, 10.0*plotCount)
fig.subplots_adjust(hspace=0.2, wspace=0.2)
for i in range(minibatchCount):
source, gradient = findMapping(n, e, f, learningRate)
# That's much the same as
# f = lambda x: LocalMapping(source, gradient)(f(x))
f = GlobalMapping(source, gradient, f)
print i,
sys.stdout.flush()
if i%plotEvery==0:
plotIndex = i/plotEvery
drawMapping(axarr[plotIndex][0], f)
drawMapping(axarr[plotIndex][1], LocalMapping(source, gradient))
sampleFromTarget = samplesFromTarget(100)
axarr[plotIndex][2].scatter(sampleFromTarget[:,0], sampleFromTarget[:,1], color='red')
sampleFromLearned = np.array([ f(p) for p in gaussSample ])
axarr[plotIndex][2].scatter(sampleFromLearned[:,0], sampleFromLearned[:,1])
print
plt.savefig("vis.pdf")
def mnist():
datasetFile = "../rbm/data/mnist.pkl.gz"
f = gzip.open(datasetFile, 'rb')
datasets = cPickle.load(f)
train_set, valid_set, test_set = datasets
f.close()
return train_set
def main():
iteration()
if __name__ == "__main__":
main()