-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsingle-auto.py
122 lines (91 loc) · 2.82 KB
/
single-auto.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
"""
Try making an autoencoder using the NEF
Notes:
- setting intercepts to -0.5 improves training RMSE versus random or 0.0
- setting max rates higher improves training RMSE
(why? training is just done on rates, so why should this make a difference)
- giving encoders limited receptive fields improves training RMSE
"""
import collections
import os
import gzip
import cPickle as pickle
import urllib
import numpy as np
import matplotlib.pyplot as plt
plt.ion()
import plotting
import nengo
from nengo.utils.distributions import UniformHypersphere
# --- load the data
filename = 'mnist.pkl.gz'
if not os.path.exists(filename):
url = 'http://deeplearning.net/data/mnist/mnist.pkl.gz'
urllib.urlretrieve(url, filename=filename)
with gzip.open(filename, 'rb') as f:
train, valid, test = pickle.load(f)
images, _ = train
n_vis = images.shape[1]
n_hid = 500
if 1:
# images -= images.mean(0)
# images /= images.std(0) + 1e-3
images = 2 * images - 1
images = images[:1000]
if 1:
# generate
rng = np.random
rf_shape = (9, 9)
M, N = 28, 28
m, n = rf_shape
# find random positions for top-left corner of each RF
i = rng.randint(low=0, high=M-m+1, size=n_hid)
j = rng.randint(low=0, high=N-n+1, size=n_hid)
mask = np.zeros((n_hid, M, N), dtype='bool')
for k in xrange(n_hid):
mask[k, i[k]:i[k]+m, j[k]:j[k]+n] = True
mask = mask.reshape(n_hid, n_vis)
encoders = rng.normal(size=(n_hid, n_vis)) * mask
else:
# rng = np.random
# encoders = rng.normal(size=(n_hid, n_vis))
# encoders = UniformHypersphere(n_vis, surface=True)
encoders = None
# --- make the network
model = nengo.Network()
with model:
u = nengo.Node(output=images[0])
up = nengo.Probe(u)
a = nengo.Ensemble(n_hid, n_vis, eval_points=images, encoders=encoders,
intercepts=[-0.5]*n_hid, max_rates=[200]*n_hid)
o = nengo.Node(size_in=n_vis)
op = nengo.Probe(o, synapse=0.03)
ca = nengo.Connection(u, a)
co = nengo.Connection(a, o)
from hunse_tools.timing import tic, toc
tic()
sim = nengo.Simulator(model)
toc()
tic()
sim.run(1.0)
toc()
x = sim.data[up].reshape(-1, 28, 28)
y = sim.data[op].reshape(-1, 28, 28)
plt.figure(1)
plt.clf()
plt.subplot(121)
plt.imshow(x.mean(0), cmap='gray')
plt.subplot(122)
plt.imshow(y.mean(0), cmap='gray')
# --- check RMSEs over a number of builds
n_trials = 10
rmses = np.zeros((n_trials, n_vis))
for i in range(n_trials):
if a.encoders is not None:
a.encoders = rng.normal(size=a.encoders.shape) * mask
sim = nengo.Simulator(model)
info = sim.data[co].solver_info
rmses[i] = info['rmses']
mean = rmses.mean(1)
print "RMSEs: mean %f +/- %f" % (mean.mean(), 2 * mean.std() / np.sqrt(n_trials))
# print "RMSEs: mean %f, std %f, min %f, max %f" % (rmses.mean(), rmses.std(), rmses.min(), rmses.max())