annotate algorithms/daa.py @ 516:2b0e10ac6929

misc
author Olivier Breuleux <breuleuo@iro.umontreal.ca>
date Mon, 03 Nov 2008 00:10:18 -0500
parents dc2d93590da0
children
rev   line source
476
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
1
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
2 import theano
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
3 from theano import tensor as T
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
4 from theano.tensor import nnet as NN
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
5 import numpy as N
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
6
487
94a4c5b7293b DAA code more generic:
Joseph Turian <turian@gmail.com>
parents: 486
diff changeset
7 from pylearn import cost as cost
94a4c5b7293b DAA code more generic:
Joseph Turian <turian@gmail.com>
parents: 486
diff changeset
8
476
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
9 class DenoisingAA(T.RModule):
512
da916044454c added useless comments
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 503
diff changeset
10 """De-noising Auto-encoder
da916044454c added useless comments
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 503
diff changeset
11
da916044454c added useless comments
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 503
diff changeset
12 WRITEME
da916044454c added useless comments
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 503
diff changeset
13
da916044454c added useless comments
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 503
diff changeset
14 Abstract base class. Requires subclass with functions:
da916044454c added useless comments
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 503
diff changeset
15
da916044454c added useless comments
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 503
diff changeset
16 - build_corrupted_input()
da916044454c added useless comments
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 503
diff changeset
17
da916044454c added useless comments
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 503
diff changeset
18 Introductory article about this model WRITEME.
da916044454c added useless comments
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 503
diff changeset
19
da916044454c added useless comments
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 503
diff changeset
20
da916044454c added useless comments
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 503
diff changeset
21 """
476
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
22
487
94a4c5b7293b DAA code more generic:
Joseph Turian <turian@gmail.com>
parents: 486
diff changeset
23 def __init__(self, input = None, regularize = True, tie_weights = True,
94a4c5b7293b DAA code more generic:
Joseph Turian <turian@gmail.com>
parents: 486
diff changeset
24 activation_function=NN.sigmoid, reconstruction_cost_function=cost.cross_entropy):
94a4c5b7293b DAA code more generic:
Joseph Turian <turian@gmail.com>
parents: 486
diff changeset
25 """
512
da916044454c added useless comments
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 503
diff changeset
26 :param input: WRITEME
da916044454c added useless comments
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 503
diff changeset
27
da916044454c added useless comments
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 503
diff changeset
28 :param regularize: WRITEME
da916044454c added useless comments
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 503
diff changeset
29
da916044454c added useless comments
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 503
diff changeset
30 :param tie_weights: WRITEME
da916044454c added useless comments
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 503
diff changeset
31
da916044454c added useless comments
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 503
diff changeset
32 :param activation_function: WRITEME
da916044454c added useless comments
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 503
diff changeset
33
da916044454c added useless comments
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 503
diff changeset
34 :param reconstruction_cost: Should return one cost per example (row)
da916044454c added useless comments
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 503
diff changeset
35
da916044454c added useless comments
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 503
diff changeset
36 :todo: Default noise level for all daa levels
da916044454c added useless comments
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 503
diff changeset
37
487
94a4c5b7293b DAA code more generic:
Joseph Turian <turian@gmail.com>
parents: 486
diff changeset
38 """
476
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
39 super(DenoisingAA, self).__init__()
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
40
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
41 # MODEL CONFIGURATION
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
42 self.regularize = regularize
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
43 self.tie_weights = tie_weights
487
94a4c5b7293b DAA code more generic:
Joseph Turian <turian@gmail.com>
parents: 486
diff changeset
44 self.activation_function = activation_function
94a4c5b7293b DAA code more generic:
Joseph Turian <turian@gmail.com>
parents: 486
diff changeset
45 self.reconstruction_cost_function = reconstruction_cost_function
476
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
46
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
47 # ACQUIRE/MAKE INPUT
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
48 if not input:
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
49 input = T.matrix('input')
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
50 self.input = theano.External(input)
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
51
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
52 # HYPER-PARAMETERS
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
53 self.lr = theano.Member(T.scalar())
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
54
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
55 # PARAMETERS
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
56 self.w1 = theano.Member(T.matrix())
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
57 if not tie_weights:
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
58 self.w2 = theano.Member(T.matrix())
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
59 else:
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
60 self.w2 = self.w1.T
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
61 self.b1 = theano.Member(T.vector())
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
62 self.b2 = theano.Member(T.vector())
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
63
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
64
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
65 # REGULARIZATION COST
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
66 self.regularization = self.build_regularization()
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
67
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
68
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
69 ### NOISELESS ###
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
70
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
71 # HIDDEN LAYER
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
72 self.hidden_activation = T.dot(self.input, self.w1) + self.b1
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
73 self.hidden = self.hid_activation_function(self.hidden_activation)
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
74
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
75 # RECONSTRUCTION LAYER
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
76 self.output_activation = T.dot(self.hidden, self.w2) + self.b2
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
77 self.output = self.out_activation_function(self.output_activation)
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
78
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
79 # RECONSTRUCTION COST
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
80 self.reconstruction_costs = self.build_reconstruction_costs(self.output)
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
81 self.reconstruction_cost = T.mean(self.reconstruction_costs)
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
82
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
83 # TOTAL COST
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
84 self.cost = self.reconstruction_cost
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
85 if self.regularize:
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
86 self.cost = self.cost + self.regularization
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
87
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
88
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
89 ### WITH NOISE ###
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
90 self.corrupted_input = self.build_corrupted_input()
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
91
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
92 # HIDDEN LAYER
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
93 self.nhidden_activation = T.dot(self.corrupted_input, self.w1) + self.b1
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
94 self.nhidden = self.hid_activation_function(self.nhidden_activation)
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
95
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
96 # RECONSTRUCTION LAYER
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
97 self.noutput_activation = T.dot(self.nhidden, self.w2) + self.b2
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
98 self.noutput = self.out_activation_function(self.noutput_activation)
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
99
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
100 # RECONSTRUCTION COST
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
101 self.nreconstruction_costs = self.build_reconstruction_costs(self.noutput)
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
102 self.nreconstruction_cost = T.mean(self.nreconstruction_costs)
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
103
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
104 # TOTAL COST
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
105 self.ncost = self.nreconstruction_cost
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
106 if self.regularize:
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
107 self.ncost = self.ncost + self.regularization
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
108
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
109
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
110 # GRADIENTS AND UPDATES
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
111 if self.tie_weights:
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
112 self.params = self.w1, self.b1, self.b2
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
113 else:
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
114 self.params = self.w1, self.w2, self.b1, self.b2
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
115 gradients = T.grad(self.ncost, self.params)
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
116 updates = dict((p, p - self.lr * g) for p, g in zip(self.params, gradients))
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
117
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
118 # INTERFACE METHODS
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
119 self.update = theano.Method(self.input, self.ncost, updates)
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
120 self.compute_cost = theano.Method(self.input, self.cost)
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
121 self.noisify = theano.Method(self.input, self.corrupted_input)
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
122 self.reconstruction = theano.Method(self.input, self.output)
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
123 self.representation = theano.Method(self.input, self.hidden)
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
124 self.reconstruction_through_noise = theano.Method(self.input, [self.corrupted_input, self.noutput])
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
125
503
c7ce66b4e8f4 Extensions to algorithms, and some cleanup (by defining linear_output result).
Joseph Turian <turian@gmail.com>
parents: 488
diff changeset
126 self.validate = theano.Method(self.input, [self.cost, self.output])
c7ce66b4e8f4 Extensions to algorithms, and some cleanup (by defining linear_output result).
Joseph Turian <turian@gmail.com>
parents: 488
diff changeset
127
476
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
128 def _instance_initialize(self, obj, input_size = None, hidden_size = None, seed = None, **init):
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
129 if (input_size is None) ^ (hidden_size is None):
486
5ccb1662f9f6 Small change to error message
Joseph Turian <turian@gmail.com>
parents: 485
diff changeset
130 raise ValueError("Must specify input_size and hidden_size or neither.")
476
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
131 super(DenoisingAA, self)._instance_initialize(obj, **init)
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
132 if seed is not None:
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
133 R = N.random.RandomState(seed)
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
134 else:
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
135 R = N.random
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
136 if input_size is not None:
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
137 sz = (input_size, hidden_size)
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
138 inf = 1/N.sqrt(input_size)
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
139 hif = 1/N.sqrt(hidden_size)
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
140 obj.w1 = R.uniform(size = sz, low = -inf, high = inf)
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
141 if not self.tie_weights:
515
dc2d93590da0 Small bugfix in hidden weight initialization.
Joseph Turian <turian@iro.umontreal.ca>
parents: 512
diff changeset
142 obj.w2 = R.uniform(size = list(reversed(sz)), low = -hif, high = hif)
476
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
143 obj.b1 = N.zeros(hidden_size)
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
144 obj.b2 = N.zeros(input_size)
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
145 if seed is not None:
482
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents: 476
diff changeset
146 obj.seed(seed)
476
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
147 obj.__hide__ = ['params']
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
148
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
149 def build_regularization(self):
487
94a4c5b7293b DAA code more generic:
Joseph Turian <turian@gmail.com>
parents: 486
diff changeset
150 """
94a4c5b7293b DAA code more generic:
Joseph Turian <turian@gmail.com>
parents: 486
diff changeset
151 @todo: Why do we need this function?
94a4c5b7293b DAA code more generic:
Joseph Turian <turian@gmail.com>
parents: 486
diff changeset
152 """
476
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
153 return T.zero() # no regularization!
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
154
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
155
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
156 class SigmoidXEDenoisingAA(DenoisingAA):
487
94a4c5b7293b DAA code more generic:
Joseph Turian <turian@gmail.com>
parents: 486
diff changeset
157 """
94a4c5b7293b DAA code more generic:
Joseph Turian <turian@gmail.com>
parents: 486
diff changeset
158 @todo: Merge this into the above.
488
e06666ac32d5 Added another todo
Joseph Turian <turian@gmail.com>
parents: 487
diff changeset
159 @todo: Default noise level for all daa levels
487
94a4c5b7293b DAA code more generic:
Joseph Turian <turian@gmail.com>
parents: 486
diff changeset
160 """
476
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
161
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
162 def build_corrupted_input(self):
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
163 self.noise_level = theano.Member(T.scalar())
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
164 return self.random.binomial(T.shape(self.input), 1, 1 - self.noise_level) * self.input
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
165
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
166 def hid_activation_function(self, activation):
487
94a4c5b7293b DAA code more generic:
Joseph Turian <turian@gmail.com>
parents: 486
diff changeset
167 return self.activation_function(activation)
476
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
168
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
169 def out_activation_function(self, activation):
487
94a4c5b7293b DAA code more generic:
Joseph Turian <turian@gmail.com>
parents: 486
diff changeset
170 return self.activation_function(activation)
476
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
171
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
172 def build_reconstruction_costs(self, output):
487
94a4c5b7293b DAA code more generic:
Joseph Turian <turian@gmail.com>
parents: 486
diff changeset
173 return self.reconstruction_cost_function(self.input, output)
476
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
174
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
175 def build_regularization(self):
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
176 self.l2_coef = theano.Member(T.scalar())
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
177 if self.tie_weights:
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
178 return self.l2_coef * T.sum(self.w1 * self.w1)
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
179 else:
485
e8c37244b54f Small bugfix in regularization
Joseph Turian <turian@gmail.com>
parents: 482
diff changeset
180 return self.l2_coef * (T.sum(self.w1 * self.w1) + T.sum(self.w2 * self.w2))
476
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
181
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
182 def _instance_initialize(self, obj, input_size = None, hidden_size = None, seed = None, **init):
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
183 init.setdefault('noise_level', 0)
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
184 init.setdefault('l2_coef', 0)
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
185 super(SigmoidXEDenoisingAA, self)._instance_initialize(obj, input_size, hidden_size, seed, **init)
8fcd0f3d9a17 added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
186