Mercurial > pylearn
comparison sandbox/rbm/model.py @ 399:8796b91a9f09
RBM minibatch works
author | Joseph Turian <turian@gmail.com> |
---|---|
date | Tue, 08 Jul 2008 21:42:21 -0400 |
parents | 6e55ccb7e2bf |
children | 269d5c5a4209 |
comparison
equal
deleted
inserted
replaced
398:6e55ccb7e2bf | 399:8796b91a9f09 |
---|---|
41 | 41 |
42 class Model: | 42 class Model: |
43 def __init__(self): | 43 def __init__(self): |
44 self.parameters = parameters.Parameters(randomly_initialize=True) | 44 self.parameters = parameters.Parameters(randomly_initialize=True) |
45 | 45 |
46 def update(self, instance): | 46 def update(self, instances): |
47 """ | 47 """ |
48 Update the L{Model} using one training instance. | 48 Update the L{Model} using one training instance. |
49 @param instance: A dict from feature index to (non-zero) value. | 49 @param instance: A dict from feature index to (non-zero) value. |
50 @todo: Should assert that nonzero_indices and zero_indices | 50 @todo: Should assert that nonzero_indices and zero_indices |
51 are correct (i.e. are truly nonzero/zero). | 51 are correct (i.e. are truly nonzero/zero). |
52 """ | 52 """ |
53 v0 = numpy.zeros((1, globals.INPUT_DIMENSION)) | 53 v0 = numpy.zeros((len(instances), globals.INPUT_DIMENSION)) |
54 for idx in instance.keys(): | 54 minibatch = len(instances) |
55 v0[0][idx] = instance[idx] | 55 for i in range(minibatch): |
56 for idx in instances[i].keys(): | |
57 v0[i][idx] = instances[i][idx] | |
56 | 58 |
57 q0 = sigmoid(self.parameters.b + dot(v0, self.parameters.w)) | 59 q0 = sigmoid(self.parameters.b + dot(v0, self.parameters.w)) |
58 h0 = sample(q0) | 60 h0 = sample(q0) |
59 p0 = sigmoid(self.parameters.c + dot(h0, self.parameters.w.T)) | 61 p0 = sigmoid(self.parameters.c + dot(h0, self.parameters.w.T)) |
60 v1 = sample(p0) | 62 v1 = sample(p0) |
61 q1 = sigmoid(self.parameters.b + dot(v1, self.parameters.w)) | 63 q1 = sigmoid(self.parameters.b + dot(v1, self.parameters.w)) |
62 print | 64 print |
63 print "v[0]:", v0 | 65 # print "v[0]:", v0 |
64 print "Q(h[0][i] = 1 | v[0]):", q0 | 66 # print "Q(h[0][i] = 1 | v[0]):", q0 |
65 print "h[0]:", h0 | 67 # print "h[0]:", h0 |
66 print "P(v[1][j] = 1 | h[0]):", p0 | 68 # print "P(v[1][j] = 1 | h[0]):", p0 |
67 print "XENT(P(v[1][j] = 1 | h[0]) | v0):", numpy.sum(crossentropy(p0, v0)) | 69 print "XENT(P(v[1][j] = 1 | h[0]) | v0):", numpy.sum(crossentropy(p0, v0)) |
68 print "v[1]:", v1 | 70 # print "v[1]:", v1 |
69 print "Q(h[1][i] = 1 | v[1]):", q1 | 71 # print "Q(h[1][i] = 1 | v[1]):", q1 |
70 | 72 |
71 self.parameters.w += LR * (dot(v0.T, h0) - dot(v1.T, q1)) | 73 # print |
72 self.parameters.b += LR * (h0 - q1) | 74 # print v0.T.shape |
73 self.parameters.c += LR * (v0 - v1) | 75 # print h0.shape |
76 # print dot(v0.T, h0).shape | |
77 # print self.parameters.w.shape | |
78 self.parameters.w += LR * (dot(v0.T, h0) - dot(v1.T, q1)) / minibatch | |
79 # print | |
80 # print h0.shape | |
81 # print q1.shape | |
82 # print self.parameters.b.shape | |
83 self.parameters.b += LR * numpy.sum(h0 - q1, axis=0) / minibatch | |
84 # print v0.shape, v1.shape | |
85 # print | |
86 # print self.parameters.c.shape | |
87 self.parameters.c += LR * numpy.sum(v0 - v1, axis=0) / minibatch | |
74 # print self.parameters | 88 # print self.parameters |