comparison sandbox/sparse_random_autoassociator/graph.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/graph.py@edec18614a70
children
comparison
equal deleted inserted replaced
392:e2cb8d489908 393:36baeb7125a4
1 """
2 Theano graph for an autoassociator for sparse inputs, which will be trained
3 using Ronan Collobert + Jason Weston's sampling trick (2008).
4 @todo: Make nearly everything private.
5 """
6
7 from globals import MARGIN
8
9 from pylearn.nnet_ops import sigmoid, binary_crossentropy
10 from theano import tensor as t
11 from theano.tensor import dot
12 xnonzero = t.dvector()
13 w1nonzero = t.dmatrix()
14 b1 = t.dvector()
15 w2nonzero = t.dmatrix()
16 w2zero = t.dmatrix()
17 b2nonzero = t.dvector()
18 b2zero = t.dvector()
19 h = sigmoid(dot(xnonzero, w1nonzero) + b1)
20 ynonzero = sigmoid(dot(h, w2nonzero) + b2nonzero)
21 yzero = sigmoid(dot(h, w2zero) + b2zero)
22
23 # May want to weight loss wrt nonzero value? e.g. MARGIN violation for
24 # 0.1 nonzero is not as bad as MARGIN violation for 0.2 nonzero.
25 def hingeloss(MARGIN):
26 return -MARGIN * (MARGIN < 0)
27 nonzeroloss = hingeloss(ynonzero - t.max(yzero) - MARGIN)
28 zeroloss = hingeloss(-t.max(-(ynonzero)) - yzero - MARGIN)
29 # xnonzero sensitive loss:
30 #nonzeroloss = hingeloss(ynonzero - t.max(yzero) - MARGIN - xnonzero)
31 #zeroloss = hingeloss(-t.max(-(ynonzero - xnonzero)) - yzero - MARGIN)
32 loss = t.sum(nonzeroloss) + t.sum(zeroloss)
33
34 #loss = t.sum(binary_crossentropy(ynonzero, xnonzero)) + t.sum(binary_crossentropy(yzero, t.constant(0)))
35
36 (gw1nonzero, gb1, gw2nonzero, gw2zero, gb2nonzero, gb2zero) = t.grad(loss, [w1nonzero, b1, w2nonzero, w2zero, b2nonzero, b2zero])
37
38 import theano.compile
39
40 inputs = [xnonzero, w1nonzero, b1, w2nonzero, w2zero, b2nonzero, b2zero]
41 outputs = [ynonzero, yzero, loss, gw1nonzero, gb1, gw2nonzero, gw2zero, gb2nonzero, gb2zero]
42 trainfn = theano.compile.function(inputs, outputs)