-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathRBM.py
executable file
·297 lines (227 loc) · 9.01 KB
/
RBM.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
import pdb as pdb
import numpy as np
import pylab as p
from scipy import c_, reshape, random, rot90, stats, mgrid
#import psyco
#psyco.full()
sigmoid = lambda X, A : (1.0/(1.0 + np.exp(-A*X)))
class RBM:
def __init__(self, nvis, nhid):
"""
Initialise parameters for demo
"""
# Data generation
self.Ndat = 500 # number of data points
self.dat = self.genData()
#self.dat =
# Parameters
self.sig = 0.2 # standard deviation for ??
self.epsW = 0.5 # step size
self.epsA = 0.5 # step size
self.nvis = nvis # number of hidden layers
self.nhid = nhid # number of visible layers
self.cost = 0.00001 #
self.moment = 0.90 #
# State of the visible layer
self.Svis0 = np.zeros( nvis+1, dtype=np.float32)
self.Svis0[-1] = 1.0
self.Svis = np.zeros( nvis+1, dtype=np.float32)
self.Svis[-1] = 1.0
# State of the hidden layer
self.Shid = np.zeros( nhid+1, dtype=np.float32)
# Weight matrix and its delta
self.W = np.random.standard_normal((nvis+1, nhid+1))/10
self.dW = np.random.standard_normal((nvis+1, nhid+1))/1000
#
self.Avis = 0.1*np.ones( nvis+1, dtype=np.float32)
self.Ahid = np.ones( nhid+1, dtype=np.float32)
self.dA = np.zeros(nvis+1, dtype=np.float32)
def genData(self):
"""
Generate data for demo
"""
c1 = 0.5
r1 = 0.4
r2 = 0.3
# generate enough data to filter
N = 20* self.Ndat
X = np.array(np.random.random_sample(N))
Y = np.array(np.random.random_sample(N))
X1 = X[(X-c1)*(X-c1) + (Y-c1)*(Y-c1) < r1*r1]
Y1 = Y[(X-c1)*(X-c1) + (Y-c1)*(Y-c1) < r1*r1]
X2 = X1[(X1-c1)*(X1-c1) + (Y1-c1)*(Y1-c1) > r2*r2]
Y2 = Y1[(X1-c1)*(X1-c1) + (Y1-c1)*(Y1-c1) > r2*r2]
X3 = X2[ abs(X2-Y2)>0.05 ]
Y3 = Y2[ abs(X2-Y2)>0.05 ]
#X3 = X2[ X2-Y2>0.15 ]
#Y3 = Y2[ X2-Y2>0.15]
X4 = np.zeros( self.Ndat, dtype=np.float32)
Y4 = np.zeros( self.Ndat, dtype=np.float32)
for i in xrange(self.Ndat):
if (X3[i]-Y3[i]) >0.05:
X4[i] = X3[i] + 0.08
Y4[i] = Y3[i] + 0.18
else:
X4[i] = X3[i] - 0.08
Y4[i] = Y3[i] - 0.18
print "X", np.size(X3[0:self.Ndat]), "Y", np.size(Y3)
return(np.vstack((X4[0:self.Ndat],Y4[0:self.Ndat])))
def activ(self, who):
"""
Activate:
visible=0, hidden=1
neurons
"""
if(who=='hidden'):
self.Shid = np.dot(self.Svis, self.W) + self.sig*np.random.standard_normal(self.nhid+1)
self.Shid = sigmoid(self.Shid, self.Ahid)
self.Shid[-1] = 1.0 # bias
if(who=='visible'):
self.Svis = np.dot(self.W, self.Shid) + self.sig*np.random.standard_normal(self.nvis+1)
self.Svis = sigmoid(self.Svis, self.Avis)
self.Svis[-1] = 1.0 # bias
def learn(self, epochmax):
"""
Train the RBM
"""
# Initialise arrays
Err = np.zeros( epochmax, dtype=np.float32)
E = np.zeros( epochmax, dtype=np.float32)
self.stat = np.zeros( epochmax, dtype=np.float32)
self.stat2 = np.zeros( epochmax, dtype=np.float32)
ksteps = 1
for epoch in range(1,epochmax):
# Initialise weights for positive and negative directions
wpos = np.zeros( (self.nvis+1, self.nhid+1), dtype=np.float32)
wneg = np.zeros( (self.nvis+1, self.nhid+1), dtype=np.float32)
# Initialise activations for positive and negative directions
apos = np.zeros( self.nhid+1 , dtype=np.float32)
aneg = np.zeros( self.nhid+1 , dtype=np.float32)
# Steps size adaptation
ksteps=50 if(epoch>0) else ksteps
ksteps=((epoch-epoch%100)/100+40) if(epoch>1000) else ksteps
self.ksteps = ksteps
# Iterate through data sample
for point in xrange(self.Ndat):
# Select a coordinate point
#print(self.dat[:][point])
self.Svis0[0:2] = self.dat[:,point] # Save a copy of the visible state
self.Svis = self.Svis0 # Initialise the visible state
# positive phase activation
self.activ('hidden')
# Use hidden state to calc the positive direction w and a
wpos = wpos + np.outer(self.Svis, self.Shid)
apos = apos + self.Shid*self.Shid
# negative phase activation
#self.activ('visible')
#self.activ('hidden')
for recstep in xrange(ksteps):
self.activ('visible')
self.activ('hidden')
tmp = np.outer(self.Svis, self.Shid)
wneg = wneg + tmp
aneg = aneg + self.Shid*self.Shid
delta = self.Svis0[0:2]-self.Svis[0:2]
# Update error metrics
Err[epoch] = Err[epoch] + np.sum(delta*delta)
E[epoch] = E[epoch] - np.sum(np.dot(self.W.T, tmp))
# Update weight matrix
self.dW = self.dW*self.moment + self.epsW * ((wpos-wneg)/np.size(self.dat) - self.cost*self.W)
self.W = self.W + self.dW
self.Ahid = self.Ahid + self.epsA*(apos-aneg)/(np.size(self.dat)*self.Ahid*self.Ahid)
# Normalise errors
Err[epoch] = Err[epoch]/(self.nvis*np.size(self.dat))
E[epoch] = E[epoch]/np.size(self.dat)
# Debug messages
if (epoch==1) or (epoch%100==0) or (epoch==epochmax):
print "epoch:", epoch, "err:", np.round_(Err[epoch], 6), "ksteps:", ksteps
# Store statistics
self.stat[ epoch] = self.W[0,0]
self.stat2[epoch] = self.Ahid[0]
self.Err = Err
self.E = E
def wview(self):
"""
Plot weights for visualisation.
"""
import pylab as p
p.plot(xrange(np.size(self.W[2])),self.W[2], 'bo')
p.show()
def reconstruct(self, Npoint, Nsteps):
"""
Run some data through the network.
"""
X = np.array(np.random.random_sample(Npoint))
Y = np.array(np.random.random_sample(Npoint))
datnew = np.vstack((X, Y))
self.datout = np.zeros( (2,Npoint), dtype=np.float32)
for point in xrange(Npoint):
self.Svis[0:2] = datnew[:,point]
for recstep in xrange(Nsteps):
self.activ('hidden')
self.activ('visible')
self.datout[:,point] = self.Svis[0:2]
def contour(self, p, dat):
"""
KDE contour plot.
"""
X, Y = mgrid[0.0:1.0:100j, 0.0:1.0:100j]
positions = c_[X.ravel(), Y.ravel()]
val = c_[dat[0,:], dat[1,:]]
kernel = stats.kde.gaussian_kde(val.T)
Z = reshape(kernel(positions.T).T, X.T.shape)
p.imshow( rot90(Z) , cmap=p.cm.YlGnBu, extent=[0, 1, 0, 1])
p.plot(dat[0,:], dat[1,:], 'r.')
p.axis([0.0, 1.0, 0.0, 1.0])
if __name__ == "__main__":
# Reset randomiser seed
np.random.seed(12345)
# Create RBM instance
rbm = RBM(2,8)
pdb.set_trace()
kkk=0 # 0 is nicer plot with KDE of points
# Train the RBM
rbm.learn(4000)
p.figure(1)
p.plot(xrange(np.size(rbm.E)),rbm.E, 'b+')
p.figure(2)
p.plot(xrange(np.size(rbm.Err)),rbm.Err, 'r.')
p.figure(3)
if kkk==1:
p.plot(rbm.dat[0,:],rbm.dat[1,:], 'bo')
p.axis([0.0, 1.0, 0.0, 1.0])
else:
rbm.contour(p, rbm.dat)
p.savefig("dat.png",dpi=100)
# Test the RBM
rbm.reconstruct(rbm.Ndat, 1)
p.figure(4)
if kkk==1:
p.plot(rbm.datout[0,:],rbm.datout[1,:], 'b.')
p.axis([0.0, 1.0, 0.0, 1.0])
else:
rbm.contour(p, rbm.datout)
# Test the RBM
rbm.reconstruct(rbm.Ndat, 20)
p.figure(5)
if kkk==1:
p.plot(rbm.datout[0,:],rbm.datout[1,:], 'b.')
p.axis([0.0, 1.0, 0.0, 1.0])
else:
rbm.contour(p, rbm.datout)
# Test the RBM
rbm.reconstruct(rbm.Ndat, rbm.ksteps)
p.figure(6)
if kkk==1:
p.plot(rbm.datout[0,:],rbm.datout[1,:], 'b.')
p.axis([0.0, 1.0, 0.0, 1.0])
else:
rbm.contour(p, rbm.datout)
p.savefig("reconstruct.png",dpi=100)
p.figure(7)
p.plot(xrange(np.size(rbm.stat)), rbm.stat, "b.")
p.figure(8)
p.plot(xrange(np.size(rbm.stat2)), rbm.stat2, "b.")
print(np.around(rbm.W,5))
print(rbm.Ahid)
p.show()