-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathucl_gaussian_binary_rbm_sparse.py
658 lines (557 loc) · 18.4 KB
/
ucl_gaussian_binary_rbm_sparse.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
import sys
import time
import numpy as np
import dl_utils as ut
import data_fm as fm
import linecache
rng = np.random
rng.seed(1234)
class RBM(object):
"""
Class representing a basic restricted Boltzmann machine, with
binary stochastic visible units and binary stochastic hidden
units.
"""
def __init__(self, nvis, nhid, mfvis=True, mfhid=False, initvar=0.1):
nweights = nvis * nhid
vb_offset = nweights
hb_offset = nweights + nvis
# One parameter matrix, with views onto it specified below.
self.params = np.empty((nweights + nvis + nhid))
# Weights between the hiddens and visibles
self.weights = self.params[:vb_offset].reshape(nvis, nhid)
# Biases on the visible units
self.visbias = self.params[vb_offset:hb_offset]
# Biases on the hidden units
self.hidbias = self.params[hb_offset:]
# Attributes for scratch arrays used during sampling.
self._hid_states = None
self._vis_states = None
# Instance-specific mean field settings.
self._mfvis = mfvis
self._mfhid = mfhid
@property
def numvis(self):
"""The number of visible units (i.e. dimension of the input)."""
return self.visbias.shape[0]
@property
def numhid(self):
"""The number of hidden units in this model."""
return self.hidbias.shape[0]
def _prepare_buffer(self, ncases, kind):
"""
Prepare the _hid_states and _vis_states buffers for
use for a minibatch of size `ncases`, reshaping or
reallocating as necessary. `kind` is one of 'hid', 'vis'.
"""
if kind not in ['hid', 'vis']:
raise ValueError('kind argument must be hid or vis')
name = '_%s_states' % kind
num = getattr(self, 'num%s' % kind)
buf = getattr(self, name)
if buf is None or buf.shape[0] < ncases:
if buf is not None:
del buf
buf = np.empty((ncases, num))
setattr(self, name, buf)
buf[...] = np.NaN
return buf[:ncases]
def hid_activate(self, input, mf=False):
"""
Activate the hidden units by sampling from their conditional
distribution given each of the rows of `inputs. If `mf` is True,
return the deterministic, real-valued probabilities of activation
in place of stochastic binary samples ('mean-field').
"""
input = np.atleast_2d(input)
ncases, ndim = input.shape
hid = self._prepare_buffer(ncases, 'hid')
self._update_hidden(input, hid, mf)
return hid
def _update_hidden(self, vis, hid, mf=False):
"""
Update hidden units by writing new values to array `hid`.
If `mf` is False, hidden unit values are sampled from their
conditional distribution given the visible unit configurations
specified in each row of `vis`. If `mf` is True, the
deterministic, real-valued probabilities of activation are
written instead of stochastic binary samples ('mean-field').
"""
hid[...] = np.dot(vis, self.weights)
hid[...] += self.hidbias
hid *= -1.
np.exp(hid, hid)
hid += 1.
hid **= -1.
if not mf:
self.sample_hid(hid)
def _update_visible(self, vis, hid, mf=False):
"""
Update visible units by writing new values to array `hid`.
If `mf` is False, visible unit values are sampled from their
conditional distribution given the hidden unit configurations
specified in each row of `hid`. If `mf` is True, the
deterministic, real-valued probabilities of activation are
written instead of stochastic binary samples ('mean-field').
"""
# Implements 1/(1 + exp(-WX) with in-place operations
vis[...] = np.dot(hid, self.weights.T)
vis[...] += self.visbias
vis *= -1.
np.exp(vis, vis)
vis += 1.
vis **= -1.
if not mf:
self.sample_vis(vis)
@classmethod
def binary_threshold(cls, probs):
"""
Given a set of real-valued activation probabilities,
sample binary values with the given Bernoulli parameter,
and update the array in-placewith the Bernoulli samples.
"""
samples = rng.uniform(size=probs.shape)
# Simulate Bernoulli trials with p = probs[i,j] by generating random
# uniform and counting any number less than probs[i,j] as success.
probs[samples < probs] = 1.
# Anything not set to 1 should be 0 once floored.
np.floor(probs, probs)
# Binary hidden units
sample_hid = binary_threshold
# Binary visible units
sample_vis = binary_threshold
def gibbs_walk(self, nsteps, hid):
hid = np.atleast_2d(hid)
ncases = hid.shape[0]
# Allocate (or reuse) a buffer with which to store
# the states of the visible units
vis = self._prepare_buffer(ncases, 'vis')
for iter in xrange(nsteps):
# Update the visible units conditioning on the hidden units.
self._update_visible(vis, hid, self._mfvis)
# Always do mean-field on the last hidden unit update to get a
# less noisy estimate of the negative phase correlations.
if iter < nsteps - 1:
mfhid = self._mfhid
else:
mfhid = True
# Update the hidden units conditioning on the visible units.
self._update_hidden(vis, hid, mfhid)
return self._vis_states[:ncases], self._hid_states[:ncases]
class GaussianBinaryRBM(RBM):
def _update_visible(self, vis, hid, mf=False):
vis[...] = np.dot(hid, self.weights.T)
vis += self.visbias
if not mf:
self.sample_vis(vis)
@classmethod
def sample_vis(self, vis):
vis += rng.normal(size=vis.shape)
def get_fake_line(line):
newline=[]
for l in line:
a,b=l.split(":")
newline.append(l)
newline.append(str(int(a)-1)+":0")
return newline
def get_batch_x(file,index,size):
xarray = []
for i in range(index, index + size):
line = linecache.getline(file, i)
if line.strip() != '':
s = line.strip().replace(':', ' ').split(' ')
x = {}
for f in range(1, len(s), 2):
x[int(s[f])] = int(s[f+1])
x[int(s[f])-1] = 0 #fake
#line=line[line.index(' ')+1:]
#x=get_fake_line(line.split())
xarray.append(x)
# print xarray
return xarray
class CDTrainer(object):
"""An object that trains a model using vanilla contrastive divergence."""
def __init__(self, model, weightcost=0.0002, rates=(1e-4, 1e-4, 1e-4),
cachebatchsums=True):
self._model = model
self._visbias_rate, self._hidbias_rate, self._weight_rate = rates
self._weightcost = weightcost
self._cachebatchsums = cachebatchsums
self._weightstep = np.zeros(model.weights.shape)
def train(self,file_path,epochs,ncases,fm_model_file,results=None,cdsteps=1, minibatch=100, momentum=0.9):
"""
Train an RBM with contrastive divergence, using `nsteps`
steps of alternating Gibbs sampling to draw the negative phase
samples.
"""
model = self._model
if self._cachebatchsums:
batchsums = {}
for epoch in xrange(epochs):
# An epoch is a single pass through the training data.
epoch_start = time.clock()
# Mean squared error isn't really the right thing to measure
# for RBMs with binary visible units, but gives a good enough
# indication of whether things are moving in the right way.
mse = 0
offset=0
while True:
batch=get_batch_x(file_path,(offset+1),minibatch)
if results !=None:
i=0
for r in results:
i+=1
if i%2==1:
weights=r
else:
if i==2:
newbatch=np.zeros((len(batch),weights.shape[1]))
bias=r
hnum = weights.shape[1]
case_num = len(batch)
j = 0
for x in batch:
sums = np.zeros(hnum)
for f in x:
if x[f] == 1:
sums += weights[f]
newbatch[j] = sums
j += 1
batch=newbatch+bias
# batch=np.dot(batch,weights)+bias
else:
bias=r
batch=np.dot(batch,weights)+bias
batch = 1.0 / (1.0 + np.exp(-batch))
batchsize = batch.shape[0]
# Mean field pass on the hidden units f
hid = model.hid_activate(batch, mf=True)
# Correlations between the data and the hidden unit activations
poscorr = np.dot(batch.T, hid)
# Activities of the hidden units
posact = hid.sum(axis=0)
# Threshold the hidden units so that they can't convey
# more than 1 bit of information in the subsequent
# sampling (assuming the hidden units are binary,
# which they most often are).
model.sample_hid(hid)
# Simulate Gibbs sampling for a given number of steps.
vis, hid = model.gibbs_walk(cdsteps, hid)
# Update the weights with the difference in correlations
# between the positive and negative phases.
thisweightstep = poscorr
thisweightstep -= np.dot(vis.T, hid)
thisweightstep /= batchsize
thisweightstep -= self._weightcost * model.weights
thisweightstep *= self._weight_rate
self._weightstep *= momentum
self._weightstep += thisweightstep
model.weights += self._weightstep
# The gradient of the visible biases is the difference in
# summed visible activities for the minibatch.
if self._cachebatchsums:
if offset not in batchsums:
batchsum = batch.sum(axis=0)
batchsums[offset] = batchsum
else:
batchsum = batchsums[offset]
else:
batchsum = batch.sum(axis=0)
visbias_step = batchsum - vis.sum(axis=0)
visbias_step *= self._visbias_rate / batchsize
model.visbias += visbias_step
# The gradient of the hidden biases is the difference in
# summed hidden activities for the minibatch.
hidbias_step = posact - hid.sum(axis=0)
hidbias_step *= self._hidbias_rate / batchsize
model.hidbias += hidbias_step
# print 'vis',vis
# print 'bat',batch
# Compute the squared error in-place.
vis -= batch
vis **= 2.
# Add to the total epoch estimate.
mse += vis.sum() / ncases
offset+=batch.shape[0]
# print 'minibatch',minibatch
# print 'batsize',batch.shape[0]
if batch.shape[0]<minibatch:
break
print "Done epoch %d: %f seconds, MSE=%f" % \
(epoch + 1, time.clock() - epoch_start, mse)
sys.stdout.flush()
class sparse_RBM(object):
def __init__(self, nvis, nhid, nsparsevis, mfvis=True, mfhid=False, initvar=0.1):
nweights = nvis * nhid
vb_offset = nweights
hb_offset = nweights + nvis
# One parameter matrix, with views onto it specified below.
self.params = np.empty((nweights + nvis + nhid))
# Weights between the hiddens and visibles
self.weights = self.params[:vb_offset].reshape(nvis, nhid)
# Biases on the visible units
self.visbias = self.params[vb_offset:hb_offset]
# Biases on the hidden units
self.hidbias = self.params[hb_offset:]
# Attributes for scratch arrays used during sampling.
self._hid_states = None
self._vis_states = None
self._sparsevis_states=None
# Instance-specific mean field settings.
self._mfvis = mfvis
self._mfhid = mfhid
self.nsparsevis=nsparsevis
self.line=None
self.line_dic=None
@property
def numvis(self):
"""The number of visible units (i.e. dimension of the input)."""
return self.visbias.shape[0]
@property
def numhid(self):
"""The number of hidden units in this model."""
return self.hidbias.shape[0]
@property
def numsparsevis(self):
"""The number of visible units (i.e. dimension of the input)."""
return self.nsparsevis
def _prepare_buffer(self, ncases, kind):
"""
Prepare the _hid_states and _vis_states buffers for
use for a minibatch of size `ncases`, reshaping or
reallocating as necessary. `kind` is one of 'hid', 'vis'.
"""
if kind not in ['hid', 'vis','sparsevis']:
raise ValueError('kind argument must be hid or vis')
name = '_%s_states' % kind
num = getattr(self, 'num%s' % kind)
buf = getattr(self, name)
if buf is None or buf.shape[0] < ncases:
if buf is not None:
del buf
buf = np.empty((ncases, num))
setattr(self, name, buf)
buf[...] = np.NaN
return buf[:ncases]
def hid_activate(self, input, mf=False):
hid = self._prepare_buffer(1, 'hid')
hid[...]=0
self._update_hidden(input, hid, mf)
return hid
def _update_hidden(self, vis, hid, mf=False):
hid[...]=0
for i in range(self.numhid):
sum=0
j=0
# for l in self.line:
# a,b=l.split(':')
# a=int(a)
# sum+=(self.weights[a][i])*float(vis[0][j])
# j+=1
for f in sorted(self.line_dic):
sum+=(self.weights[f][i])*float(vis[0][j])
j+=1
hid[0][i]=sum
hid[...] += self.hidbias
hid *= -1.
np.exp(hid, hid)
hid += 1.
hid **= -1.
if not mf:
self.sample_hid(hid)
def _update_visible(self, vis, hid, mf=False):
# Implements 1/(1 + exp(-WX) with in-place operations
vis[...]=0
i=0
# for l in self.line:
# a,b=l.split(':')
# a=int(a)
# vis[0][i]=np.dot(hid,self.weights[a,:].T)
# vis[0][i]+=self.visbias[a]
# i+=1
for f in sorted(self.line_dic):
vis[0][i]=np.dot(hid,self.weights[f,:].T)
vis[0][i]+=self.visbias[f]
i+=1
vis *= -1.
np.exp(vis, vis)
vis += 1.
vis **= -1.
if not mf:
self.sample_vis(vis)
@classmethod
def binary_threshold(cls, probs):
samples = rng.uniform(size=probs.shape)
probs[samples < probs] = 1.
# Anything not set to 1 should be 0 once floored.
np.floor(probs, probs)
# Binary hidden units
sample_hid = binary_threshold
# Binary visible units
sample_vis = binary_threshold
def gibbs_walk(self, nsteps, hid):
hid = np.atleast_2d(hid)
ncases = hid.shape[0]
# Allocate (or reuse) a buffer with which to store
# the states of the visible units
vis = self._prepare_buffer(ncases, 'sparsevis')
for iter in xrange(nsteps):
# Update the visible units conditioning on the hidden units.
self._update_visible(vis, hid, self._mfvis)
# Always do mean-field on the last hidden unit update to get a
# less noisy estimate of the negative phase correlations.
if iter < nsteps - 1:
mfhid = self._mfhid
else:
mfhid = True
# Update the hidden units conditioning on the visible units.
self._update_hidden(vis, hid, mfhid)
# print 'gibbs_walk',self._hid_states[:ncases]
return self._sparsevis_states[:ncases], self._hid_states[:ncases]
class sparse_CDTrainer(object):
def __init__(self, model, weightcost=0.0002, rates=(1e-4, 1e-4, 1e-4),
cachebatchsums=True):
self._model = model
self._visbias_rate, self._hidbias_rate, self._weight_rate = rates
self._weightcost = weightcost
self._cachebatchsums = cachebatchsums
self._weightstep = np.zeros((model.numsparsevis,model.numhid))
def train(self,file_path,epochs,ncases,cdsteps=1, momentum=0.9):
model = self._model
if self._cachebatchsums:
batchsums = {}
for epoch in xrange(epochs):
epoch_start = time.clock()
mse = 0
offset=0
with open(file_path, "r") as ins:
array = []
for line in ins:
# if offset%10000==0:
# print 'offset:',offset
# print time.clock() - epoch_start
if line.strip()!="":
s = line.strip().replace(':', ' ').split(' ')
x = {}
for f in range(1, len(s), 2):
x[int(s[f])-1] = 0
x[int(s[f])] = int(s[f+1])
model.line_dic=x
# line=line[line.index(' ')+1:]
# model.line=get_fake_line(line.split())
v=[]
i=0
for f in sorted(model.line_dic):
i+=1
v.append(int(model.line_dic[f]))
v=np.asarray(v).reshape(1,i)
batch=v
hid = model.hid_activate(batch, mf=True)
poscorr = np.dot(batch.T, hid)
posact = hid.sum(axis=0)
model.sample_hid(hid)
# Simulate Gibbs sampling for a given number of steps.
vis, hid = model.gibbs_walk(cdsteps, hid)
# # Update the weights with the difference in correlations
# # between the positive and negative phases.
thisweightstep = poscorr
thisweightstep -= np.dot(vis.T, hid)
# thisweightstep -= self._weightcost * model.weights
i=-1
# for l in model.line:
# i+=1
# a,b=l.split(":")
# # for j in range(model.numhid):
# # thisweightstep[i][j]-=self._weightcost*model.weights[int(a)][j]
# thisweightstep[i]-=self._weightcost*model.weights[int(a)]
#
for f in sorted(model.line_dic):
i+=1
thisweightstep[i]-=self._weightcost*model.weights[int(f)]
thisweightstep *= self._weight_rate
self._weightstep *= momentum
self._weightstep += thisweightstep
i=-1
# for l in model.line:
# i+=1
# a,b=l.split(":")
# model.weights[int(a)]+=self._weightstep[i]
# # for j in range(model.numhid):
# # model.weights[int(a)][j]+=self._weightstep[i][j]
# model.weights[int(a)] += self._weightstep[i]
for f in sorted(model.line_dic):
i+=1
model.weights[int(f)]+=self._weightstep[i]
model.weights[int(f)] += self._weightstep[i]
# # The gradient of the visible biases is the difference in
# # summed visible activities for the minibatch.
if self._cachebatchsums:
if offset not in batchsums:
batchsum = batch.sum(axis=0)
batchsums[offset] = batchsum
else:
batchsum = batchsums[offset]
else:
batchsum = batch.sum(axis=0)
visbias_step = batchsum - vis.sum(axis=0)
visbias_step *= self._visbias_rate
# model.visbias += visbias_step
i=-1
# for l in model.line:
# i+=1
# a,b=l.split(":")
# model.visbias[int(a)] += visbias_step[i]
for f in sorted(model.line_dic):
i+=1
model.visbias[int(f)] += visbias_step[i]
# The gradient of the hidden biases is the difference in
# summed hidden activities for the minibatch.
hidbias_step = posact - hid.sum(axis=0)
hidbias_step *= self._hidbias_rate
model.hidbias += hidbias_step
# print 'sparse:'
# print 'vis',vis
# print 'bat',batch
# Compute the squared error in-place.
vis -= batch
vis **= 2.
# Add to the total epoch estimate.
mse += vis.sum() / ncases
offset+=1
print "Done epoch %d: %f seconds, MSE=%f" % \
(epoch + 1, time.clock() - epoch_start, mse)
sys.stdout.flush()
def get_rbm_weights(file, arr, ncases, fm_model_file,batch_size=1):
epochs=10
row=0
col=0
results=[]
weights=[]
bias=[]
index=0
n_sparse_vis=32
for line in arr:
index+=1
if index==1:
col=int(line)
else:
row=col
col=int(line)
if index==2:
# print 'row',row
# print 'col',col
# print 'n_sparse_vis',n_sparse_vis
rbm = sparse_RBM(row, col,n_sparse_vis)
rbm.params[:] = rng.uniform(-1./10, 1./10, len(rbm.params))
trainer = sparse_CDTrainer(rbm)
trainer.train(file,epochs,ncases,cdsteps=1)
results.append(rbm.weights)
results.append(rbm.hidbias)
elif index>2:
rbm = RBM(row, col)
rbm.params[:] = rng.uniform(-1./10, 1./10, len(rbm.params))
trainer = CDTrainer(rbm)
trainer.train(file, epochs,ncases,results=results,minibatch=batch_size,fm_model_file=fm_model_file)
results.append(rbm.weights)
results.append(rbm.hidbias)
return results