Mercurial > pylearn
annotate mlp.py @ 133:b4657441dd65
Corrected typos
author | Yoshua Bengio <bengioy@iro.umontreal.ca> |
---|---|
date | Fri, 09 May 2008 13:38:54 -0400 |
parents | f6505ec32dc3 |
children | 3f4e5c9bdc5e |
rev | line source |
---|---|
132
f6505ec32dc3
Updated documentation slightly
Joseph Turian <turian@gmail.com>
parents:
129
diff
changeset
|
1 """ |
f6505ec32dc3
Updated documentation slightly
Joseph Turian <turian@gmail.com>
parents:
129
diff
changeset
|
2 A straightforward classicial feedforward |
f6505ec32dc3
Updated documentation slightly
Joseph Turian <turian@gmail.com>
parents:
129
diff
changeset
|
3 one-hidden-layer neural net, with L2 regularization. |
f6505ec32dc3
Updated documentation slightly
Joseph Turian <turian@gmail.com>
parents:
129
diff
changeset
|
4 This is one of the simplest example of L{Learner}, and illustrates |
f6505ec32dc3
Updated documentation slightly
Joseph Turian <turian@gmail.com>
parents:
129
diff
changeset
|
5 the use of theano. |
f6505ec32dc3
Updated documentation slightly
Joseph Turian <turian@gmail.com>
parents:
129
diff
changeset
|
6 """ |
111
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
7 |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
8 from learner import * |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
9 from theano import tensor as t |
118
d0a1bd0378c6
Finished draft of OneHiddenLayerNNetClassifier to debut learner.py
Yoshua Bengio <bengioy@iro.umontreal.ca>
parents:
111
diff
changeset
|
10 from nnet_ops import * |
133 | 11 import math |
111
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
12 |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
13 |
129
4c2280edcaf5
Fixed typos in learner.py
Yoshua Bengio <bengioy@iro.umontreal.ca>
parents:
126
diff
changeset
|
14 class OneHiddenLayerNNetClassifier(OnlineGradientTLearner): |
111
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
15 """ |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
16 Implement a straightforward classicial feedforward |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
17 one-hidden-layer neural net, with L2 regularization. |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
18 |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
19 The predictor parameters are obtained by minibatch/online gradient descent. |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
20 Training can proceed sequentially (with multiple calls to update with |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
21 different disjoint subsets of the training sets). |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
22 |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
23 Hyper-parameters: |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
24 - L2_regularizer |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
25 - learning_rate |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
26 - n_hidden |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
27 |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
28 For each (input_t,output_t) pair in a minibatch,:: |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
29 |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
30 output_activations_t = b2+W2*tanh(b1+W1*input_t) |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
31 output_t = softmax(output_activations_t) |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
32 output_class_t = argmax(output_activations_t) |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
33 class_error_t = 1_{output_class_t != target_t} |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
34 nll_t = -log(output_t[target_t]) |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
35 |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
36 and the training criterion is:: |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
37 |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
38 loss = L2_regularizer*(||W1||^2 + ||W2||^2) + sum_t nll_t |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
39 |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
40 The parameters are [b1,W1,b2,W2] and are obtained by minimizing the loss by |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
41 stochastic minibatch gradient descent:: |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
42 |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
43 parameters[i] -= learning_rate * dloss/dparameters[i] |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
44 |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
45 The fields and attributes expected and produced by use and update are the following: |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
46 |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
47 - Input and output fields (example-wise quantities): |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
48 |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
49 - 'input' (always expected by use and update) |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
50 - 'target' (optionally expected by use and always by update) |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
51 - 'output' (optionally produced by use) |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
52 - 'output_class' (optionally produced by use) |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
53 - 'class_error' (optionally produced by use) |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
54 - 'nll' (optionally produced by use) |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
55 |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
56 - optional attributes (optionally expected as input_dataset attributes) |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
57 (warning, this may be dangerous, the 'use' method will use those provided in the |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
58 input_dataset rather than those learned during 'update'; currently no support |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
59 for providing these to update): |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
60 |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
61 - 'L2_regularizer' |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
62 - 'b1' |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
63 - 'W1' |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
64 - 'b2' |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
65 - 'W2' |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
66 - 'parameters' = [b1, W1, b2, W2] |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
67 - 'regularization_term' |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
68 |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
69 """ |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
70 |
133 | 71 def __init__(self,n_hidden,n_classes,learning_rate,max_n_epochs,init_range=1.,n_inputs=None,minibatch_size=None): |
72 self._n_inputs = n_inputs | |
121 | 73 self._n_outputs = n_classes |
74 self._n_hidden = n_hidden | |
75 self._init_range = init_range | |
133 | 76 self._max_n_epochs = max_n_epochs |
77 self._minibatch_size = minibatch_size | |
121 | 78 self.learning_rate = learning_rate # this is the float |
79 self._learning_rate = t.scalar('learning_rate') # this is the symbol | |
80 self._input = t.matrix('input') # n_examples x n_inputs | |
126 | 81 self._target = t.ivector('target') # n_examples x n_outputs |
121 | 82 self._L2_regularizer = t.scalar('L2_regularizer') |
83 self._W1 = t.matrix('W1') | |
84 self._W2 = t.matrix('W2') | |
85 self._b1 = t.row('b1') | |
86 self._b2 = t.row('b2') | |
126 | 87 self._regularization_term = self._L2_regularizer * (t.sum(self._W1*self._W1) + t.sum(self._W2*self._W2)) |
121 | 88 self._output_activations =self._b2+t.dot(t.tanh(self._b1+t.dot(self._input,self._W1.T)),self._W2.T) |
89 self._nll,self._output = crossentropy_softmax_1hot(self._output_activations,self._target) | |
90 self._output_class = t.argmax(self._output,1) | |
91 self._class_error = self._output_class != self._target | |
92 self._minibatch_criterion = self._nll + self._regularization_term / t.shape(self._input)[0] | |
129
4c2280edcaf5
Fixed typos in learner.py
Yoshua Bengio <bengioy@iro.umontreal.ca>
parents:
126
diff
changeset
|
93 OnlineGradientTLearner.__init__(self) |
121 | 94 |
111
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
95 def attributeNames(self): |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
96 return ["parameters","b1","W2","b2","W2", "L2_regularizer","regularization_term"] |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
97 |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
98 def parameterAttributes(self): |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
99 return ["b1","W1", "b2", "W2"] |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
100 |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
101 def useInputAttributes(self): |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
102 return self.parameterAttributes() |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
103 |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
104 def useOutputAttributes(self): |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
105 return [] |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
106 |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
107 def updateInputAttributes(self): |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
108 return self.parameterAttributes() + ["L2_regularizer"] |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
109 |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
110 def updateMinibatchInputFields(self): |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
111 return ["input","target"] |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
112 |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
113 def updateEndOutputAttributes(self): |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
114 return ["regularization_term"] |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
115 |
118
d0a1bd0378c6
Finished draft of OneHiddenLayerNNetClassifier to debut learner.py
Yoshua Bengio <bengioy@iro.umontreal.ca>
parents:
111
diff
changeset
|
116 def lossAttribute(self): |
d0a1bd0378c6
Finished draft of OneHiddenLayerNNetClassifier to debut learner.py
Yoshua Bengio <bengioy@iro.umontreal.ca>
parents:
111
diff
changeset
|
117 return "minibatch_criterion" |
d0a1bd0378c6
Finished draft of OneHiddenLayerNNetClassifier to debut learner.py
Yoshua Bengio <bengioy@iro.umontreal.ca>
parents:
111
diff
changeset
|
118 |
111
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
119 def defaultOutputFields(self, input_fields): |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
120 output_fields = ["output", "output_class",] |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
121 if "target" in input_fields: |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
122 output_fields += ["class_error", "nll"] |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
123 return output_fields |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
124 |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
125 def allocate(self,minibatch): |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
126 minibatch_n_inputs = minibatch["input"].shape[1] |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
127 if not self._n_inputs: |
118
d0a1bd0378c6
Finished draft of OneHiddenLayerNNetClassifier to debut learner.py
Yoshua Bengio <bengioy@iro.umontreal.ca>
parents:
111
diff
changeset
|
128 self._n_inputs = minibatch_n_inputs |
d0a1bd0378c6
Finished draft of OneHiddenLayerNNetClassifier to debut learner.py
Yoshua Bengio <bengioy@iro.umontreal.ca>
parents:
111
diff
changeset
|
129 self.b1 = numpy.zeros(self._n_hidden) |
d0a1bd0378c6
Finished draft of OneHiddenLayerNNetClassifier to debut learner.py
Yoshua Bengio <bengioy@iro.umontreal.ca>
parents:
111
diff
changeset
|
130 self.b2 = numpy.zeros(self._n_outputs) |
111
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
131 self.forget() |
118
d0a1bd0378c6
Finished draft of OneHiddenLayerNNetClassifier to debut learner.py
Yoshua Bengio <bengioy@iro.umontreal.ca>
parents:
111
diff
changeset
|
132 elif self._n_inputs!=minibatch_n_inputs: |
d0a1bd0378c6
Finished draft of OneHiddenLayerNNetClassifier to debut learner.py
Yoshua Bengio <bengioy@iro.umontreal.ca>
parents:
111
diff
changeset
|
133 # if the input changes dimension on the fly, we resize and forget everything |
111
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
134 self.forget() |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
135 |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
136 def forget(self): |
118
d0a1bd0378c6
Finished draft of OneHiddenLayerNNetClassifier to debut learner.py
Yoshua Bengio <bengioy@iro.umontreal.ca>
parents:
111
diff
changeset
|
137 if self._n_inputs: |
d0a1bd0378c6
Finished draft of OneHiddenLayerNNetClassifier to debut learner.py
Yoshua Bengio <bengioy@iro.umontreal.ca>
parents:
111
diff
changeset
|
138 r = self._init_range/math.sqrt(self._n_inputs) |
d0a1bd0378c6
Finished draft of OneHiddenLayerNNetClassifier to debut learner.py
Yoshua Bengio <bengioy@iro.umontreal.ca>
parents:
111
diff
changeset
|
139 self.W1 = numpy.random.uniform(low=-r,high=r, |
d0a1bd0378c6
Finished draft of OneHiddenLayerNNetClassifier to debut learner.py
Yoshua Bengio <bengioy@iro.umontreal.ca>
parents:
111
diff
changeset
|
140 size=(self._n_hidden,self._n_inputs)) |
d0a1bd0378c6
Finished draft of OneHiddenLayerNNetClassifier to debut learner.py
Yoshua Bengio <bengioy@iro.umontreal.ca>
parents:
111
diff
changeset
|
141 r = self._init_range/math.sqrt(self._n_hidden) |
d0a1bd0378c6
Finished draft of OneHiddenLayerNNetClassifier to debut learner.py
Yoshua Bengio <bengioy@iro.umontreal.ca>
parents:
111
diff
changeset
|
142 self.W2 = numpy.random.uniform(low=-r,high=r, |
d0a1bd0378c6
Finished draft of OneHiddenLayerNNetClassifier to debut learner.py
Yoshua Bengio <bengioy@iro.umontreal.ca>
parents:
111
diff
changeset
|
143 size=(self._n_outputs,self._n_hidden)) |
d0a1bd0378c6
Finished draft of OneHiddenLayerNNetClassifier to debut learner.py
Yoshua Bengio <bengioy@iro.umontreal.ca>
parents:
111
diff
changeset
|
144 self.b1[:]=0 |
d0a1bd0378c6
Finished draft of OneHiddenLayerNNetClassifier to debut learner.py
Yoshua Bengio <bengioy@iro.umontreal.ca>
parents:
111
diff
changeset
|
145 self.b2[:]=0 |
133 | 146 self._n_epochs=0 |
111
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
147 |
133 | 148 def isLastEpoch(self): |
149 self._n_epochs +=1 | |
150 return self._n_epochs>=self._max_n_epochs | |
111
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
151 |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
152 class MLP(MinibatchUpdatesTLearner): |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
153 """ |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
154 Implement a feedforward multi-layer perceptron, with or without L1 and/or L2 regularization. |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
155 |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
156 The predictor parameters are obtained by minibatch/online gradient descent. |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
157 Training can proceed sequentially (with multiple calls to update with |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
158 different disjoint subsets of the training sets). |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
159 |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
160 Hyper-parameters: |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
161 - L1_regularizer |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
162 - L2_regularizer |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
163 - neuron_sparsity_regularizer |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
164 - initial_learning_rate |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
165 - learning_rate_decrease_rate |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
166 - n_hidden_per_layer (a list of integers) |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
167 - activation_function ("sigmoid","tanh", or "ratio") |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
168 |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
169 The output/task type (classification, regression, etc.) is obtained by specializing MLP. |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
170 |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
171 For each (input[t],output[t]) pair in a minibatch,:: |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
172 |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
173 activation[0] = input_t |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
174 for k=1 to n_hidden_layers: |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
175 activation[k]=activation_function(b[k]+ W[k]*activation[k-1]) |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
176 output_t = output_activation_function(b[n_hidden_layers+1]+W[n_hidden_layers+1]*activation[n_hidden_layers]) |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
177 |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
178 and the b and W are obtained by minimizing the following by stochastic minibatch gradient descent:: |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
179 |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
180 L2_regularizer sum_{ijk} W_{kij}^2 + L1_regularizer sum_{kij} |W_{kij}| |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
181 + neuron_sparsity_regularizer sum_{ki} |b_{ki} + infinity| |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
182 - sum_t log P_{output_model}(target_t | output_t) |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
183 |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
184 The fields and attributes expected and produced by use and update are the following: |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
185 |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
186 - Input and output fields (example-wise quantities): |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
187 |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
188 - 'input' (always expected by use and update) |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
189 - 'target' (optionally expected by use and always by update) |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
190 - 'output' (optionally produced by use) |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
191 - error fields produced by sub-class of MLP |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
192 |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
193 - optional attributes (optionally expected as input_dataset attributes) |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
194 (warning, this may be dangerous, the 'use' method will use those provided in the |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
195 input_dataset rather than those learned during 'update'; currently no support |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
196 for providing these to update): |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
197 |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
198 - 'L1_regularizer' |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
199 - 'L2_regularizer' |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
200 - 'b' |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
201 - 'W' |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
202 - 'parameters' = [b[1], W[1], b[2], W[2], ...] |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
203 - 'regularization_term' |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
204 |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
205 """ |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
206 |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
207 def attributeNames(self): |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
208 return ["parameters","b","W","L1_regularizer","L2_regularizer","neuron_sparsity_regularizer","regularization_term"] |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
209 |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
210 def useInputAttributes(self): |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
211 return ["b","W"] |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
212 |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
213 def useOutputAttributes(self): |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
214 return [] |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
215 |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
216 def updateInputAttributes(self): |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
217 return ["b","W","L1_regularizer","L2_regularizer","neuron_sparsity_regularizer"] |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
218 |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
219 def updateMinibatchInputFields(self): |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
220 return ["input","target"] |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
221 |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
222 def updateMinibatchInputAttributes(self): |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
223 return ["b","W"] |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
224 |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
225 def updateMinibatchOutputAttributes(self): |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
226 return ["new_XtX","new_XtY"] |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
227 |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
228 def updateEndInputAttributes(self): |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
229 return ["theta","XtX","XtY"] |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
230 |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
231 def updateEndOutputAttributes(self): |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
232 return ["new_theta","b","W","regularization_term"] # CHECK: WILL b AND W CONTAIN OLD OR NEW THETA? @todo i.e. order of computation = ? |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
233 |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
234 def parameterAttributes(self): |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
235 return ["b","W"] |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
236 |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
237 def defaultOutputFields(self, input_fields): |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
238 output_fields = ["output"] |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
239 if "target" in input_fields: |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
240 output_fields.append("squared_error") |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
241 return output_fields |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
242 |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
243 def __init__(self): |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
244 self._input = t.matrix('input') # n_examples x n_inputs |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
245 self._target = t.matrix('target') # n_examples x n_outputs |
121 | 246 self._L2_regularizer = t.scalar('L2_regularizer') |
111
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
247 self._theta = t.matrix('theta') |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
248 self._W = self._theta[:,1:] |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
249 self._b = self._theta[:,0] |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
250 self._XtX = t.matrix('XtX') |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
251 self._XtY = t.matrix('XtY') |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
252 self._extended_input = t.prepend_one_to_each_row(self._input) |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
253 self._output = t.dot(self._input,self._W.T) + self._b # (n_examples , n_outputs) matrix |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
254 self._squared_error = t.sum_within_rows(t.sqr(self._output-self._target)) # (n_examples ) vector |
118
d0a1bd0378c6
Finished draft of OneHiddenLayerNNetClassifier to debut learner.py
Yoshua Bengio <bengioy@iro.umontreal.ca>
parents:
111
diff
changeset
|
255 self._regularizer = self._L2_regularizer * t.dot(self._W,self._W) |
111
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
256 self._new_XtX = add_inplace(self._XtX,t.dot(self._extended_input.T,self._extended_input)) |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
257 self._new_XtY = add_inplace(self._XtY,t.dot(self._extended_input.T,self._target)) |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
258 self._new_theta = t.solve_inplace(self._theta,self._XtX,self._XtY) |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
259 |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
260 OneShotTLearner.__init__(self) |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
261 |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
262 def allocate(self,minibatch): |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
263 minibatch_n_inputs = minibatch["input"].shape[1] |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
264 minibatch_n_outputs = minibatch["target"].shape[1] |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
265 if not self._n_inputs: |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
266 self._n_inputs = minibatch_n_inputs |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
267 self._n_outputs = minibatch_n_outputs |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
268 self.XtX = numpy.zeros((1+self._n_inputs,1+self._n_inputs)) |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
269 self.XtY = numpy.zeros((1+self._n_inputs,self._n_outputs)) |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
270 self.theta = numpy.zeros((self._n_outputs,1+self._n_inputs)) |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
271 self.forget() |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
272 elif self._n_inputs!=minibatch_n_inputs or self._n_outputs!=minibatch_n_outputs: |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
273 # if the input or target changes dimension on the fly, we resize and forget everything |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
274 self.forget() |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
275 |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
276 def forget(self): |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
277 if self._n_inputs and self._n_outputs: |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
278 self.XtX.resize((1+self.n_inputs,1+self.n_inputs)) |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
279 self.XtY.resize((1+self.n_inputs,self.n_outputs)) |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
280 self.XtX.data[:,:]=0 |
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
281 self.XtY.data[:,:]=0 |
118
d0a1bd0378c6
Finished draft of OneHiddenLayerNNetClassifier to debut learner.py
Yoshua Bengio <bengioy@iro.umontreal.ca>
parents:
111
diff
changeset
|
282 numpy.diag(self.XtX.data)[1:]=self.L2_regularizer |
111
88257dfedf8c
Added another work in progress, for mlp's
bengioy@bengiomac.local
parents:
diff
changeset
|
283 |