comparison sandbox/sparse_random_autoassociator/model.py @ 393:36baeb7125a4

Made sandbox directory
author Joseph Turian <turian@gmail.com>
date Tue, 08 Jul 2008 18:46:26 -0400
parents sparse_random_autoassociator/model.py@42cc94cf6c12
children
comparison
equal deleted inserted replaced
392:e2cb8d489908 393:36baeb7125a4
1 """
2 The model for an autoassociator for sparse inputs, using Ronan Collobert + Jason
3 Weston's sampling trick (2008).
4 """
5
6 from graph import trainfn
7 import parameters
8
9 import globals
10 from globals import LR
11
12 import numpy
13 import random
14 random.seed(globals.SEED)
15
16 def _select_indices(instance):
17 """
18 Choose nonzero and zero indices (feature columns) of the instance.
19 We select B{all} nonzero indices.
20 We select L{globals.ZERO_SAMPLE_SIZE} zero indices randomly,
21 without replacement.
22 @bug: If there are not ZERO_SAMPLE_SIZE zeroes, we will enter
23 an endless loop.
24 @return: (nonzero_indices, zero_indices)
25 """
26 # Get the nonzero indices
27 nonzero_indices = instance.keys()
28 nonzero_indices.sort()
29
30 # Get the zero indices
31 # @bug: If there are not ZERO_SAMPLE_SIZE zeroes, we will enter an endless loop.
32 zero_indices = []
33 while len(zero_indices) < globals.ZERO_SAMPLE_SIZE:
34 idx = random.randint(0, globals.INPUT_DIMENSION - 1)
35 if idx in nonzero_indices or idx in zero_indices: continue
36 zero_indices.append(idx)
37 zero_indices.sort()
38
39 return (nonzero_indices, zero_indices)
40
41 class Model:
42 def __init__(self):
43 self.parameters = parameters.Parameters(randomly_initialize=True)
44
45 def update(self, instance):
46 """
47 Update the L{Model} using one training instance.
48 @param instance: A dict from feature index to (non-zero) value.
49 @todo: Should assert that nonzero_indices and zero_indices
50 are correct (i.e. are truly nonzero/zero).
51 """
52 (nonzero_indices, zero_indices) = _select_indices(instance)
53 # No update if there aren't any non-zeros.
54 if len(nonzero_indices) == 0: return
55 xnonzero = numpy.asarray([instance[idx] for idx in nonzero_indices])
56 print
57 print "xnonzero:", xnonzero
58
59 (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])
60 print "OLD ynonzero:", ynonzero
61 print "OLD yzero:", yzero
62 print "OLD total loss:", loss
63
64 # SGD update
65 self.parameters.w1[nonzero_indices, :] -= LR * gw1nonzero
66 self.parameters.b1 -= LR * gb1
67 self.parameters.w2[:, nonzero_indices] -= LR * gw2nonzero
68 self.parameters.w2[:, zero_indices] -= LR * gw2zero
69 self.parameters.b2[nonzero_indices] -= LR * gb2nonzero
70 self.parameters.b2[zero_indices] -= LR * gb2zero
71
72 # Recompute the loss, to make sure it's descreasing
73 (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])
74 print "NEW ynonzero:", ynonzero
75 print "NEW yzero:", yzero
76 print "NEW total loss:", loss