changeset 396:e0c9357456e0

Bug fixed in RBM
author Joseph Turian <turian@gmail.com>
date Tue, 08 Jul 2008 20:37:36 -0400
parents 70019965f888
children 25a3212287cd
files sandbox/rbm/globals.py sandbox/rbm/model.py sandbox/rbm/parameters.py
diffstat 3 files changed, 17 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
--- a/sandbox/rbm/globals.py	Tue Jul 08 20:14:21 2008 -0400
+++ b/sandbox/rbm/globals.py	Tue Jul 08 20:37:36 2008 -0400
@@ -5,8 +5,8 @@
 #INPUT_DIMENSION = 1000
 #INPUT_DIMENSION = 100
 INPUT_DIMENSION = 4
-HIDDEN_DIMENSION = 10
-#HIDDEN_DIMENSION = 4
+#HIDDEN_DIMENSION = 10
+HIDDEN_DIMENSION = 6
 LEARNING_RATE = 0.1
 LR = LEARNING_RATE
 SEED = 666
--- a/sandbox/rbm/model.py	Tue Jul 08 20:14:21 2008 -0400
+++ b/sandbox/rbm/model.py	Tue Jul 08 20:37:36 2008 -0400
@@ -21,12 +21,13 @@
     return 1.0 / (1.0 + numpy.exp(-v))
 
 def sample(v):
-    assert len(v.shape) == 1
+    assert len(v.shape) == 2
     x = numpy.zeros(v.shape)
-    for i in range(v.shape[0]):
-        assert v[i] >= 0 and v[i] <= 1
-        if random.random() < v[i]: x[i] = 0
-        else: x[i] = 1
+    for j in range(v.shape[0]):
+        for i in range(v.shape[1]):
+            assert v[j][i] >= 0 and v[j][i] <= 1
+            if random.random() < v[j][i]: x[j][i] = 1
+            else: x[j][i] = 0
     return x
 
 class Model:
@@ -40,9 +41,9 @@
         @todo: Should assert that nonzero_indices and zero_indices
         are correct (i.e. are truly nonzero/zero).
         """
-        v0 = numpy.zeros(globals.INPUT_DIMENSION)
+        v0 = numpy.zeros((1, globals.INPUT_DIMENSION))
         for idx in instance.keys():
-            v0[idx] = instance[idx]
+            v0[0][idx] = instance[idx]
 
         q0 = sigmoid(self.parameters.b + dot(v0, self.parameters.w))
         h0 = sample(q0)
@@ -57,10 +58,8 @@
         print "v[1]:", v1
         print "Q(h[1][i] = 1 | v[1]):", q1
 
-        print h0.shape
-        print v0.T.shape
-        print dot(h0, v0.T)
-        print dot(q1, v1.T)
-        self.parameters.w += LR * (dot(h0, v0.T) - dot(q1, v1.T))
+        print self.parameters.w.shape
+        self.parameters.w += LR * (dot(v0.T, h0) - dot(v1.T, q1))
         self.parameters.b += LR * (h0 - q1)
         self.parameters.c += LR * (v0 - v1)
+        print self.parameters
--- a/sandbox/rbm/parameters.py	Tue Jul 08 20:14:21 2008 -0400
+++ b/sandbox/rbm/parameters.py	Tue Jul 08 20:37:36 2008 -0400
@@ -18,12 +18,12 @@
         if randomly_initialize:
             numpy.random.seed(seed)
             self.w = (numpy.random.rand(input_dimension, hidden_dimension)-0.5)/input_dimension
-            self.b = numpy.zeros(hidden_dimension)
-            self.c = numpy.zeros(input_dimension)
+            self.b = numpy.zeros((1, hidden_dimension))
+            self.c = numpy.zeros((1, input_dimension))
         else:
             self.w = numpy.zeros((input_dimension, hidden_dimension))
-            self.b = numpy.zeros(hidden_dimension)
-            self.c = numpy.zeros(input_dimension)
+            self.b = numpy.zeros((1, hidden_dimension))
+            self.c = numpy.zeros((1, input_dimension))
 
     def __str__(self):
         s = ""