Mercurial > pylearn
annotate algorithms/daa.py @ 482:b15dad843c8c
bugfix
author | Olivier Breuleux <breuleuo@iro.umontreal.ca> |
---|---|
date | Tue, 28 Oct 2008 00:23:13 -0400 |
parents | 8fcd0f3d9a17 |
children | e8c37244b54f |
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 |
8fcd0f3d9a17
added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
7 class DenoisingAA(T.RModule): |
8fcd0f3d9a17
added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
8 |
8fcd0f3d9a17
added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
9 def __init__(self, input = None, regularize = True, tie_weights = True): |
8fcd0f3d9a17
added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
10 super(DenoisingAA, self).__init__() |
8fcd0f3d9a17
added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
11 |
8fcd0f3d9a17
added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
12 # MODEL CONFIGURATION |
8fcd0f3d9a17
added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
13 self.regularize = regularize |
8fcd0f3d9a17
added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
14 self.tie_weights = tie_weights |
8fcd0f3d9a17
added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
15 |
8fcd0f3d9a17
added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
16 # ACQUIRE/MAKE INPUT |
8fcd0f3d9a17
added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
17 if not input: |
8fcd0f3d9a17
added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
18 input = T.matrix('input') |
8fcd0f3d9a17
added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
19 self.input = theano.External(input) |
8fcd0f3d9a17
added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
20 |
8fcd0f3d9a17
added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
21 # HYPER-PARAMETERS |
8fcd0f3d9a17
added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
22 self.lr = theano.Member(T.scalar()) |
8fcd0f3d9a17
added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
23 |
8fcd0f3d9a17
added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
24 # PARAMETERS |
8fcd0f3d9a17
added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
25 self.w1 = theano.Member(T.matrix()) |
8fcd0f3d9a17
added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
26 if not tie_weights: |
8fcd0f3d9a17
added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
27 self.w2 = theano.Member(T.matrix()) |
8fcd0f3d9a17
added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
28 else: |
8fcd0f3d9a17
added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
29 self.w2 = self.w1.T |
8fcd0f3d9a17
added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
30 self.b1 = theano.Member(T.vector()) |
8fcd0f3d9a17
added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
31 self.b2 = theano.Member(T.vector()) |
8fcd0f3d9a17
added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
32 |
8fcd0f3d9a17
added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
33 |
8fcd0f3d9a17
added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
34 # REGULARIZATION COST |
8fcd0f3d9a17
added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
35 self.regularization = self.build_regularization() |
8fcd0f3d9a17
added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
36 |
8fcd0f3d9a17
added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
37 |
8fcd0f3d9a17
added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
38 ### NOISELESS ### |
8fcd0f3d9a17
added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
39 |
8fcd0f3d9a17
added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
40 # HIDDEN LAYER |
8fcd0f3d9a17
added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
41 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
|
42 self.hidden = self.hid_activation_function(self.hidden_activation) |
8fcd0f3d9a17
added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
43 |
8fcd0f3d9a17
added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
44 # RECONSTRUCTION LAYER |
8fcd0f3d9a17
added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
45 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
|
46 self.output = self.out_activation_function(self.output_activation) |
8fcd0f3d9a17
added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
47 |
8fcd0f3d9a17
added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
48 # RECONSTRUCTION COST |
8fcd0f3d9a17
added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
49 self.reconstruction_costs = self.build_reconstruction_costs(self.output) |
8fcd0f3d9a17
added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
50 self.reconstruction_cost = T.mean(self.reconstruction_costs) |
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 # TOTAL COST |
8fcd0f3d9a17
added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
53 self.cost = self.reconstruction_cost |
8fcd0f3d9a17
added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
54 if self.regularize: |
8fcd0f3d9a17
added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
55 self.cost = self.cost + self.regularization |
8fcd0f3d9a17
added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
56 |
8fcd0f3d9a17
added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
57 |
8fcd0f3d9a17
added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
58 ### WITH NOISE ### |
8fcd0f3d9a17
added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
59 self.corrupted_input = self.build_corrupted_input() |
8fcd0f3d9a17
added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
60 |
8fcd0f3d9a17
added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
61 # HIDDEN LAYER |
8fcd0f3d9a17
added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
62 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
|
63 self.nhidden = self.hid_activation_function(self.nhidden_activation) |
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 # RECONSTRUCTION LAYER |
8fcd0f3d9a17
added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
66 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
|
67 self.noutput = self.out_activation_function(self.noutput_activation) |
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 # RECONSTRUCTION COST |
8fcd0f3d9a17
added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
70 self.nreconstruction_costs = self.build_reconstruction_costs(self.noutput) |
8fcd0f3d9a17
added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
71 self.nreconstruction_cost = T.mean(self.nreconstruction_costs) |
8fcd0f3d9a17
added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
72 |
8fcd0f3d9a17
added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
73 # TOTAL COST |
8fcd0f3d9a17
added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
74 self.ncost = self.nreconstruction_cost |
8fcd0f3d9a17
added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
75 if self.regularize: |
8fcd0f3d9a17
added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
76 self.ncost = self.ncost + self.regularization |
8fcd0f3d9a17
added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
77 |
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 # GRADIENTS AND UPDATES |
8fcd0f3d9a17
added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
80 if self.tie_weights: |
8fcd0f3d9a17
added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
81 self.params = self.w1, self.b1, self.b2 |
8fcd0f3d9a17
added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
82 else: |
8fcd0f3d9a17
added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
83 self.params = self.w1, self.w2, self.b1, self.b2 |
8fcd0f3d9a17
added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
84 gradients = T.grad(self.ncost, self.params) |
8fcd0f3d9a17
added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
85 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
|
86 |
8fcd0f3d9a17
added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
87 # INTERFACE METHODS |
8fcd0f3d9a17
added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
88 self.update = theano.Method(self.input, self.ncost, updates) |
8fcd0f3d9a17
added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
89 self.compute_cost = theano.Method(self.input, self.cost) |
8fcd0f3d9a17
added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
90 self.noisify = theano.Method(self.input, self.corrupted_input) |
8fcd0f3d9a17
added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
91 self.reconstruction = theano.Method(self.input, self.output) |
8fcd0f3d9a17
added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
92 self.representation = theano.Method(self.input, self.hidden) |
8fcd0f3d9a17
added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
93 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
|
94 |
8fcd0f3d9a17
added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
95 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
|
96 if (input_size is None) ^ (hidden_size is None): |
8fcd0f3d9a17
added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
97 raise ValueError("Must specify hidden_size and target_size or neither.") |
8fcd0f3d9a17
added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
98 super(DenoisingAA, self)._instance_initialize(obj, **init) |
8fcd0f3d9a17
added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
99 if seed is not None: |
8fcd0f3d9a17
added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
100 R = N.random.RandomState(seed) |
8fcd0f3d9a17
added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
101 else: |
8fcd0f3d9a17
added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
102 R = N.random |
8fcd0f3d9a17
added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
103 if input_size is not None: |
8fcd0f3d9a17
added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
104 sz = (input_size, hidden_size) |
8fcd0f3d9a17
added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
105 inf = 1/N.sqrt(input_size) |
8fcd0f3d9a17
added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
106 hif = 1/N.sqrt(hidden_size) |
8fcd0f3d9a17
added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
107 obj.w1 = R.uniform(size = sz, low = -inf, high = inf) |
8fcd0f3d9a17
added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
108 if not self.tie_weights: |
8fcd0f3d9a17
added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
109 obj.w2 = R.uniform(size = list(reversed(sz)), low = -inf, high = inf) |
8fcd0f3d9a17
added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
110 obj.b1 = N.zeros(hidden_size) |
8fcd0f3d9a17
added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
111 obj.b2 = N.zeros(input_size) |
8fcd0f3d9a17
added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
112 if seed is not None: |
482 | 113 obj.seed(seed) |
476
8fcd0f3d9a17
added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
114 obj.__hide__ = ['params'] |
8fcd0f3d9a17
added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
115 |
8fcd0f3d9a17
added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
116 def build_regularization(self): |
8fcd0f3d9a17
added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
117 return T.zero() # no regularization! |
8fcd0f3d9a17
added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
118 |
8fcd0f3d9a17
added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
119 |
8fcd0f3d9a17
added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
120 class SigmoidXEDenoisingAA(DenoisingAA): |
8fcd0f3d9a17
added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
121 |
8fcd0f3d9a17
added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
122 def build_corrupted_input(self): |
8fcd0f3d9a17
added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
123 self.noise_level = theano.Member(T.scalar()) |
8fcd0f3d9a17
added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
124 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
|
125 |
8fcd0f3d9a17
added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
126 def hid_activation_function(self, activation): |
8fcd0f3d9a17
added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
127 return NN.sigmoid(activation) |
8fcd0f3d9a17
added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
128 |
8fcd0f3d9a17
added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
129 def out_activation_function(self, activation): |
8fcd0f3d9a17
added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
130 return NN.sigmoid(activation) |
8fcd0f3d9a17
added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
131 |
8fcd0f3d9a17
added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
132 def build_reconstruction_costs(self, output): |
8fcd0f3d9a17
added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
133 reconstruction_cost_matrix = -(self.input * T.log(output) + (1 - self.input) * T.log(1 - output)) |
8fcd0f3d9a17
added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
134 return T.sum(reconstruction_cost_matrix, axis=1) |
8fcd0f3d9a17
added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
135 |
8fcd0f3d9a17
added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
136 def build_regularization(self): |
8fcd0f3d9a17
added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
137 self.l2_coef = theano.Member(T.scalar()) |
8fcd0f3d9a17
added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
138 if self.tie_weights: |
8fcd0f3d9a17
added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
139 return self.l2_coef * T.sum(self.w1 * self.w1) |
8fcd0f3d9a17
added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
140 else: |
8fcd0f3d9a17
added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
141 return self.l2_coef * T.sum(self.w1 * self.w1) + T.sum(self.w2 * self.w2) |
8fcd0f3d9a17
added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
142 |
8fcd0f3d9a17
added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
143 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
|
144 init.setdefault('noise_level', 0) |
8fcd0f3d9a17
added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
145 init.setdefault('l2_coef', 0) |
8fcd0f3d9a17
added a few algorithms
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
146 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
|
147 |