comparison sandbox/rbm/model.py @ 395:70019965f888

Basic, broken RBM implementation
author Joseph Turian <turian@gmail.com>
date Tue, 08 Jul 2008 20:14:21 -0400
parents sandbox/simple_autoassociator/model.py@36baeb7125a4
children e0c9357456e0
comparison
equal deleted inserted replaced
394:f2d112dc53be 395:70019965f888
1 """
2 The model for an autoassociator for sparse inputs, using Ronan Collobert + Jason
3 Weston's sampling trick (2008).
4 """
5
6 import parameters
7
8 import globals
9 from globals import LR
10
11 import numpy
12 from numpy import dot
13 import random
14 random.seed(globals.SEED)
15
16 import pylearn.nnet_ops
17
18 def sigmoid(v):
19 # if x < -30.0: return 0.0
20 # if x > 30.0: return 1.0
21 return 1.0 / (1.0 + numpy.exp(-v))
22
23 def sample(v):
24 assert len(v.shape) == 1
25 x = numpy.zeros(v.shape)
26 for i in range(v.shape[0]):
27 assert v[i] >= 0 and v[i] <= 1
28 if random.random() < v[i]: x[i] = 0
29 else: x[i] = 1
30 return x
31
32 class Model:
33 def __init__(self):
34 self.parameters = parameters.Parameters(randomly_initialize=True)
35
36 def update(self, instance):
37 """
38 Update the L{Model} using one training instance.
39 @param instance: A dict from feature index to (non-zero) value.
40 @todo: Should assert that nonzero_indices and zero_indices
41 are correct (i.e. are truly nonzero/zero).
42 """
43 v0 = numpy.zeros(globals.INPUT_DIMENSION)
44 for idx in instance.keys():
45 v0[idx] = instance[idx]
46
47 q0 = sigmoid(self.parameters.b + dot(v0, self.parameters.w))
48 h0 = sample(q0)
49 p0 = sigmoid(self.parameters.c + dot(h0, self.parameters.w.T))
50 v1 = sample(p0)
51 q1 = sigmoid(self.parameters.b + dot(v1, self.parameters.w))
52 print
53 print "v[0]:", v0
54 print "Q(h[0][i] = 1 | v[0]):", q0
55 print "h[0]:", h0
56 print "P(v[1][j] = 1 | h[0]):", p0
57 print "v[1]:", v1
58 print "Q(h[1][i] = 1 | v[1]):", q1
59
60 print h0.shape
61 print v0.T.shape
62 print dot(h0, v0.T)
63 print dot(q1, v1.T)
64 self.parameters.w += LR * (dot(h0, v0.T) - dot(q1, v1.T))
65 self.parameters.b += LR * (h0 - q1)
66 self.parameters.c += LR * (v0 - v1)