comparison sparse_random_autoassociator/model.py @ 370:a1bbcde6b456

Moved sparse_random_autoassociator from my repository
author Joseph Turian <turian@gmail.com>
date Mon, 07 Jul 2008 01:54:46 -0400
parents
children 75bab24bb2d8
comparison
equal deleted inserted replaced
369:90a29489b5c8 370:a1bbcde6b456
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 import numpy
9 from globals import LR
10
11 class Model:
12 def __init__(self):
13 self.parameters = parameters.Parameters(randomly_initialize=True)
14
15 def update(self, instance, nonzero_indexes, zero_indexes):
16 xnonzero = numpy.asarray([instance[idx] for idx in nonzero_indexes])
17 print
18 print "xnonzero:", xnonzero
19
20 (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])
21 print "OLD ynonzero:", ynonzero
22 print "OLD yzero:", yzero
23 print "OLD total loss:", loss
24
25 # SGD update
26 self.parameters.w1[nonzero_indexes, :] -= LR * gw1nonzero
27 self.parameters.b1 -= LR * gb1
28 self.parameters.w2[:, nonzero_indexes] -= LR * gw2nonzero
29 self.parameters.w2[:, zero_indexes] -= LR * gw2zero
30 self.parameters.b2[nonzero_indexes] -= LR * gb2nonzero
31 self.parameters.b2[zero_indexes] -= LR * gb2zero
32
33 # Recompute the loss, to make sure it's descreasing
34 (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])
35 print "NEW ynonzero:", ynonzero
36 print "NEW yzero:", yzero
37 print "NEW total loss:", loss