diff sandbox/rbm/model.py @ 406:c2e6a8fcc35e

Globals are now parameters for the RBM model
author Joseph Turian <turian@gmail.com>
date Thu, 10 Jul 2008 02:10:23 -0400
parents be4209cd568f
children 3cd4cfda2599
line wrap: on
line diff
--- a/sandbox/rbm/model.py	Thu Jul 10 01:17:40 2008 -0400
+++ b/sandbox/rbm/model.py	Thu Jul 10 02:10:23 2008 -0400
@@ -5,13 +5,9 @@
 
 import parameters
 
-import globals
-from globals import LR
-
 import numpy
 from numpy import dot
 import random
-random.seed(globals.SEED)
 
 import pylearn.nnet_ops
 import pylearn.sparse_instance
@@ -53,8 +49,17 @@
     """
     @todo: input dimensions should be stored here! not as a global.
     """
-    def __init__(self):
-        self.parameters = parameters.Parameters(randomly_initialize=True)
+    def __init__(self, input_dimension, hidden_dimension, learning_rate = 0.1, momentum = 0.9, weight_decay = 0.0002, random_seed = 666):
+        self.input_dimension    = input_dimension
+        self.hidden_dimension   = hidden_dimension
+        self.learning_rate      = learning_rate
+        self.momentum           = momentum
+        self.weight_decay       = weight_decay
+        self.random_seed        = random_seed
+
+        random.seed(random_seed)
+
+        self.parameters = parameters.Parameters(input_dimension=self.input_dimension, hidden_dimension=self.hidden_dimension, randomly_initialize=False, random_seed=self.random_seed)
         self.prev_dw = 0
         self.prev_db = 0
         self.prev_dc = 0
@@ -79,11 +84,11 @@
         @param instance: A dict from feature index to (non-zero) value.
         @todo: Should assert that nonzero_indices and zero_indices
         are correct (i.e. are truly nonzero/zero).
-        @todo: Multiply WEIGHT_DECAY by LEARNING_RATE, as done in Semantic Hashing?
+        @todo: Multiply L{self.weight_decay} by L{self.learning_rate}, as done in Semantic Hashing?
         @todo: Decay the biases too?
         """
         minibatch = len(instances)
-        v0 = pylearn.sparse_instance.to_vector(instances, globals.INPUT_DIMENSION)
+        v0 = pylearn.sparse_instance.to_vector(instances, self.input_dimension)
         print "old XENT:", numpy.sum(self.deterministic_reconstruction_error(v0))
         q0 = sigmoid(self.parameters.b + dot(v0, self.parameters.w))
         h0 = sample(q0)
@@ -91,11 +96,11 @@
         v1 = sample(p0)
         q1 = sigmoid(self.parameters.b + dot(v1, self.parameters.w))
 
-        dw = LR * (dot(v0.T, h0) - dot(v1.T, q1)) / minibatch + globals.MOMENTUM * self.prev_dw
-        db = LR * numpy.sum(h0 - q1, axis=0) / minibatch + globals.MOMENTUM * self.prev_db
-        dc = LR * numpy.sum(v0 - v1, axis=0) / minibatch + globals.MOMENTUM * self.prev_dc
+        dw = self.learning_rate * (dot(v0.T, h0) - dot(v1.T, q1)) / minibatch + self.momentum * self.prev_dw
+        db = self.learning_rate * numpy.sum(h0 - q1, axis=0) / minibatch + self.momentum * self.prev_db
+        dc = self.learning_rate * numpy.sum(v0 - v1, axis=0) / minibatch + self.momentum * self.prev_dc
 
-        self.parameters.w *= (1 - globals.WEIGHT_DECAY)
+        self.parameters.w *= (1 - self.weight_decay)
 
         self.parameters.w += dw
         self.parameters.b += db
@@ -121,14 +126,14 @@
 #        print h0.shape
 #        print dot(v0.T, h0).shape
 #        print self.parameters.w.shape
-#        self.parameters.w += LR * (dot(v0.T, h0) - dot(v1.T, q1)) / minibatch
+#        self.parameters.w += self.learning_rate * (dot(v0.T, h0) - dot(v1.T, q1)) / minibatch
 #        print
 #        print h0.shape
 #        print q1.shape
 #        print self.parameters.b.shape
-#        self.parameters.b += LR * numpy.sum(h0 - q1, axis=0) / minibatch
+#        self.parameters.b += self.learning_rate * numpy.sum(h0 - q1, axis=0) / minibatch
 #        print v0.shape, v1.shape
 #        print
 #        print self.parameters.c.shape
-#        self.parameters.c += LR * numpy.sum(v0 - v1, axis=0) / minibatch
+#        self.parameters.c += self.learning_rate * numpy.sum(v0 - v1, axis=0) / minibatch
 #        print self.parameters