Skip to content

Commit

Permalink
Fix to make example_crbm.py work with the modified code.
Browse files Browse the repository at this point in the history
The examples belong to the version of Morb without fast PCD.
This is now also mentioned in the updated README.md.
  • Loading branch information
gvtulder committed Jul 21, 2016
1 parent 6e906b7 commit 3031a82
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 10 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ This is a modified version of Morb. This version provides
* persistent contrastive divergence (PCD) with fast weights;
* a number of small changes and fixes.

Note that the examples were written for the original version of Morb
and may not directly work with this modified version.

This is part of the code used for the convolutional classification RBMs in

> Learning Features for Tissue Classification with the Classification
Expand Down
9 changes: 5 additions & 4 deletions examples/example_crbm.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,22 @@
print ">> Constructing RBM..."
rbm = rbms.BinaryBinaryCRBM(n_visible, n_hidden, n_context)
initial_vmap = { rbm.v: T.matrix('v'), rbm.x: T.matrix('x') }
pmap = rbm.pmap

# try to calculate weight updates using CD-1 stats
print ">> Constructing contrastive divergence updaters..."
s = stats.cd_stats(rbm, initial_vmap, visible_units=[rbm.v], hidden_units=[rbm.h], context_units=[rbm.x], k=1)
s = stats.cd_stats(rbm, initial_vmap, pmap, visible_units=[rbm.v], hidden_units=[rbm.h], context_units=[rbm.x], k=1)

umap = {}
for var in rbm.variables:
pu = var + 0.0005 * updaters.CDUpdater(rbm, var, s)
umap[var] = pu
pu = pmap[var] + 0.0005 * updaters.CDUpdater(rbm, var, s)
umap[pmap[var]] = pu

print ">> Compiling functions..."
t = trainers.MinibatchTrainer(rbm, umap)
m = monitors.reconstruction_mse(s, rbm.v)
mce = monitors.reconstruction_crossentropy(s, rbm.v)
free_energy = T.mean(rbm.free_energy([rbm.h], s['data'])) # take the mean over the minibatch.
free_energy = T.mean(rbm.free_energy([rbm.h], s['data'], pmap)) # take the mean over the minibatch.

# train = t.compile_function(initial_vmap, mb_size=32, monitors=[m], name='train', mode=mode)
train = t.compile_function(initial_vmap, mb_size=32, monitors=[m, mce, free_energy], name='train', mode=mode)
Expand Down
2 changes: 1 addition & 1 deletion morb/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def mean_field(self, vmap, pmap):
return self.mean_field_from_activation({ self: self.activation(vmap, pmap) })

def free_energy_term(self, vmap, pmap):
return self.free_energy_term_from_activation({ self: self.activation(vmap, pmap) })
return self.free_energy_term_from_activation({ self: self.activation(vmap, pmap) }, pmap)

def log_prob(self, vmap, pmap):
activation_vmap = { self: self.activation(vmap, pmap) }
Expand Down
17 changes: 12 additions & 5 deletions morb/rbms.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,14 @@ def __init__(self, n_visible, n_hidden):
self.v = units.BinaryUnits(self, name='v') # visibles
self.h = units.BinaryUnits(self, name='h') # hiddens
# parameters
self.W = parameters.ProdParameters(self, [self.v, self.h], theano.shared(value = self._initial_W(), name='W'), name='W') # weights
self.bv = parameters.BiasParameters(self, self.v, theano.shared(value = self._initial_bv(), name='bv'), name='bv') # visible bias
self.bh = parameters.BiasParameters(self, self.h, theano.shared(value = self._initial_bh(), name='bh'), name='bh') # hidden bias
self.pmap = {
'W': theano.shared(value = self._initial_W(), name='W'),
'bv': theano.shared(value = self._initial_bv(), name='bv'),
'bh': theano.shared(value = self._initial_bh(), name='bh')
}
self.W = parameters.ProdParameters(self, [self.v, self.h], 'W', name='W') # weights
self.bv = parameters.BiasParameters(self, self.v, 'bv', name='bv') # visible bias
self.bh = parameters.BiasParameters(self, self.h, 'bh', name='bh') # hidden bias

def _initial_W(self):
return np.asarray( np.random.uniform(
Expand All @@ -46,8 +51,10 @@ def __init__(self, n_visible, n_hidden, n_context):
# units
self.x = units.Units(self, name='x') # context
# parameters
self.A = parameters.ProdParameters(self, [self.x, self.v], theano.shared(value = self._initial_A(), name='A'), name='A') # context-to-visible weights
self.B = parameters.ProdParameters(self, [self.x, self.h], theano.shared(value = self._initial_B(), name='B'), name='B') # context-to-hidden weights
self.pmap['A'] = theano.shared(value = self._initial_A(), name='A')
self.pmap['B'] = theano.shared(value = self._initial_B(), name='B')
self.A = parameters.ProdParameters(self, [self.x, self.v], 'A', name='A') # context-to-visible weights
self.B = parameters.ProdParameters(self, [self.x, self.h], 'B', name='B') # context-to-hidden weights

def _initial_A(self):
return np.zeros((self.n_context, self.n_visible), dtype = theano.config.floatX)
Expand Down

0 comments on commit 3031a82

Please sign in to comment.