changeset 372:75bab24bb2d8

Moved more logic into model.py
author Joseph Turian <turian@gmail.com>
date Mon, 07 Jul 2008 02:06:15 -0400
parents 22463a194c90
children 42cc94cf6c12
files sparse_random_autoassociator/main.py sparse_random_autoassociator/model.py
diffstat 2 files changed, 49 insertions(+), 30 deletions(-) [+]
line wrap: on
line diff
--- a/sparse_random_autoassociator/main.py	Mon Jul 07 01:57:49 2008 -0400
+++ b/sparse_random_autoassociator/main.py	Mon Jul 07 02:06:15 2008 -0400
@@ -25,15 +25,10 @@
        - Loss is irrespective of the xnonzero magnitude.
        - We will always use all nonzero entries, even if the training
        instance is very non-sparse.
-       
-    @bug: If there are not ZERO_SAMPLE_SIZE zeroes, we will enter an
-    endless loop.
 """
 
 
-import numpy, random
-import globals
-random.seed(globals.SEED)
+import numpy
 
 nonzero_instances = []
 nonzero_instances.append({1: 0.1, 5: 0.5, 9: 1})
@@ -47,18 +42,5 @@
     # Select an instance
     instance = nonzero_instances[i % len(nonzero_instances)]
 
-    # Get the nonzero indices
-    nonzero_indexes = instance.keys()
-    nonzero_indexes.sort()
-
-    # Get the zero indices
-    # @bug: If there are not ZERO_SAMPLE_SIZE zeroes, we will enter an endless loop.
-    zero_indexes = []
-    while len(zero_indexes) < globals.ZERO_SAMPLE_SIZE:
-        idx = random.randint(0, globals.INPUT_DIMENSION - 1)
-        if idx in nonzero_indexes or idx in zero_indexes: continue
-        zero_indexes.append(idx)
-    zero_indexes.sort()
-
     # SGD update over instance
-    model.update(instance, nonzero_indexes, zero_indexes)
+    model.update(instance)
--- a/sparse_random_autoassociator/model.py	Mon Jul 07 01:57:49 2008 -0400
+++ b/sparse_random_autoassociator/model.py	Mon Jul 07 02:06:15 2008 -0400
@@ -5,33 +5,70 @@
 
 from graph import trainfn
 import parameters
+
+import globals
+from globals import LR
+
 import numpy
-from globals import LR
+import random
+random.seed(globals.SEED)
+
+def _select_indices(instance):
+    """
+    Choose nonzero and zero indices (feature columns) of the instance.
+    We select B{all} nonzero indices.
+    We select L{globals.ZERO_SAMPLE_SIZE} zero indices randomly,
+    without replacement.
+    @bug: If there are not ZERO_SAMPLE_SIZE zeroes, we will enter
+    an endless loop.
+    @return: (nonzero_indices, zero_indices)
+    """
+    # Get the nonzero indices
+    nonzero_indices = instance.keys()
+    nonzero_indices.sort()
+
+    # Get the zero indices
+    # @bug: If there are not ZERO_SAMPLE_SIZE zeroes, we will enter an endless loop.
+    zero_indices = []
+    while len(zero_indices) < globals.ZERO_SAMPLE_SIZE:
+        idx = random.randint(0, globals.INPUT_DIMENSION - 1)
+        if idx in nonzero_indices or idx in zero_indices: continue
+        zero_indices.append(idx)
+    zero_indices.sort()
+
+    return (nonzero_indices, zero_indices)
 
 class Model:
     def __init__(self):
         self.parameters = parameters.Parameters(randomly_initialize=True)
 
-    def update(self, instance, nonzero_indexes, zero_indexes):
-        xnonzero = numpy.asarray([instance[idx] for idx in nonzero_indexes])
+    def update(self, instance):
+        """
+        Update the L{Model} using one training instance.
+        @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).
+        """
+        (nonzero_indices, zero_indices) = _select_indices(instance)
+        xnonzero = numpy.asarray([instance[idx] for idx in nonzero_indices])
         print
         print "xnonzero:", xnonzero
 
-        (ynonzero, yzero, loss, gw1nonzero, gb1, gw2nonzero, gw2zero, gb2nonzero, gb2zero) = trainfn(xnonzero, self.parameters.w1[nonzero_indexes, :], self.parameters.b1, self.parameters.w2[:, nonzero_indexes], self.parameters.w2[:, zero_indexes], self.parameters.b2[nonzero_indexes], self.parameters.b2[zero_indexes])
+        (ynonzero, yzero, loss, gw1nonzero, gb1, gw2nonzero, gw2zero, gb2nonzero, gb2zero) = trainfn(xnonzero, self.parameters.w1[nonzero_indices, :], self.parameters.b1, self.parameters.w2[:, nonzero_indices], self.parameters.w2[:, zero_indices], self.parameters.b2[nonzero_indices], self.parameters.b2[zero_indices])
         print "OLD ynonzero:", ynonzero
         print "OLD yzero:", yzero
         print "OLD total loss:", loss
 
         # SGD update
-        self.parameters.w1[nonzero_indexes, :]  -= LR * gw1nonzero
+        self.parameters.w1[nonzero_indices, :]  -= LR * gw1nonzero
         self.parameters.b1						-= LR * gb1
-        self.parameters.w2[:, nonzero_indexes]  -= LR * gw2nonzero
-        self.parameters.w2[:, zero_indexes]		-= LR * gw2zero
-        self.parameters.b2[nonzero_indexes]		-= LR * gb2nonzero
-        self.parameters.b2[zero_indexes]		-= LR * gb2zero
+        self.parameters.w2[:, nonzero_indices]  -= LR * gw2nonzero
+        self.parameters.w2[:, zero_indices]		-= LR * gw2zero
+        self.parameters.b2[nonzero_indices]		-= LR * gb2nonzero
+        self.parameters.b2[zero_indices]		-= LR * gb2zero
 
         # Recompute the loss, to make sure it's descreasing
-        (ynonzero, yzero, loss, gw1nonzero, gb1, gw2nonzero, gw2zero, gb2nonzero, gb2zero) = trainfn(xnonzero, self.parameters.w1[nonzero_indexes, :], self.parameters.b1, self.parameters.w2[:, nonzero_indexes], self.parameters.w2[:, zero_indexes], self.parameters.b2[nonzero_indexes], self.parameters.b2[zero_indexes])
+        (ynonzero, yzero, loss, gw1nonzero, gb1, gw2nonzero, gw2zero, gb2nonzero, gb2zero) = trainfn(xnonzero, self.parameters.w1[nonzero_indices, :], self.parameters.b1, self.parameters.w2[:, nonzero_indices], self.parameters.w2[:, zero_indices], self.parameters.b2[nonzero_indices], self.parameters.b2[zero_indices])
         print "NEW ynonzero:", ynonzero
         print "NEW yzero:", yzero
         print "NEW total loss:", loss