1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 """
23 The `criterion` module provides some differentiable loss functions in order to
24 perform a gradient descent on a hand-crafted neural network.
25
26 The currently implemented criterions are :
27 - `CrossEntropy`
28 - `MeanSquareError`
29 - `MeanAbsoluteError`
30 - `NegativeLogLikelihood`
31
32 See their respective documentations for their mathematical expressions and their
33 use cases.
34
35 :see: `module`
36 """
37
38 import theano.tensor as T
39
41 """
42 The Criterion class handles the loss computation between **ŷ** (the `outputs` vector of a `Module`)
43 and **y** (the `targets` vector). This loss has to be differentiable, in order to perform a gradient descent.
44
45 :attention: This is an abstract class, it must be derived to be used.
46 """
48 """
49 Constructs a new `Criterion` object.
50
51 :Parameters:
52 outputs : :theano:`TensorVariable`
53 The symbolic outputs vector of a network
54 targets : :theano:`TensorVariable`
55 The symbolic targets vector
56 """
57
58 self.outputs = outputs
59 """
60 :ivar: The symbolic outputs vector of the network, denoted :math:`\mathbf{\hat{y}}`.
61 :type: :theano:`TensorVariable`
62 """
63
64 self.targets = targets
65 """
66 :ivar: The symbolic targets vector, denoted :math:`\mathbf{y}`, that will be estimated by the `outputs`.
67 :type: :theano:`TensorVariable`
68 """
69
70 self.expression = None
71 """
72 :ivar: The symbolic expression that expresses the loss between the `outputs` and the `targets` vectors.
73 :type: :theano:`TensorVariable`
74 """
75
76 self.prepare()
77
79 """
80 Computes the symbolic expression of the loss.
81
82 :attention: It must be implemented in derived classes.
83 """
84 raise NotImplementedError("This class must be derived.")
85
86
88 """
89 The cross-entropy criterion is well suited for targets vector
90 that are normalized between 0 and 1, used along with a final
91 `Sigmoid` activation module.
92
93 It has been experimentally demonstrated that an `AutoEncoder`
94 trains faster with a cross-entropy criterion.
95
96 The cross-entropy loss can be written as follows :
97
98 :math:`L_{CE} = -1/N \ \sum_{k=1}^{N} (y\cdot log(\hat{y}) + (1-y)\cdot log(1-\hat{y}))`
99 """
100
102 """
103 Constructs a new `CrossEntropy` criterion.
104
105 :Parameters:
106 outputs : :theano:`TensorVariable`
107 The symbolic `outputs` vector of the network
108 targets : :theano:`TensorVariable`
109 The symbolic `targets` vector
110 """
111 Criterion.__init__(self, outputs, targets)
112
114 """ Computes the cross-entropy symbolic expression. """
115 self.expression = -T.mean(self.targets*T.log(self.outputs) + (1-self.targets)*T.log(1-self.outputs))
116
117
119 """
120 The mean square error criterion is used in the least squares method,
121 it is well suited for data fitting.
122
123 The mean square loss can be written as follows :
124
125 :math:`L_{MSE} = -1/N \ \sum_{k=1}^{N} (y-\hat{y})^2`
126 """
127
129 """
130 Constructs a new `MeanSquareError` criterion.
131
132 :Parameters:
133 outputs : :theano:`TensorVariable`
134 The symbolic `outputs` vector of the network
135 targets : :theano:`TensorVariable`
136 The symbolic `targets` vector
137 """
138 Criterion.__init__(self, outputs, targets)
139
141 """ Computes the mean square error symbolic expression. """
142 self.expression = T.mean(T.sqr(self.outputs - self.targets))
143
144
146 """
147 The mean absolute error criterion is used in the least absolute deviations method.
148
149 The mean absolute loss can be written as follows :
150
151 :math:`L_{MSE} = -1/N \ \sum_{k=1}^{N} |y-\hat{y}|`
152 """
153
155 """
156 Constructs a new `MeanAbsoluteError` criterion.
157
158 :Parameters:
159 outputs : :theano:`TensorVariable`
160 The symbolic `outputs` vector of a network
161 targets : :theano:`TensorVariable`
162 The symbolic `targets` vector
163 """
164 Criterion.__init__(self, outputs, targets)
165
167 """ Computes the mean absolute error symbolic expression. """
168 self.expression = T.mean(T.abs_(self.outputs - self.targets))
169
171 """
172 The Negative Log Likelihood criterion is used in classification task.
173
174 It is meant to be connected to a Softmax Module as the last layer of the MLP.
175
176 The output size should be the same as the number of possible classes.
177
178 The target size should be the same as the number of possible classes, with value in between [0..1].
179 If not weighted, all 0 except 1 on the correct class.
180
181 The Negative Log Likelihood can be written as follows :
182
183 :math:`L_{NLL} = -log(<\hat{y},y>)`
184 """
185
187 """
188 Constructs a new `NegativeLogLikelihood` criterion.
189
190 :Parameters:
191 outputs : :theano:`TensorVariable`
192 The symbolic `outputs` vector of a network
193 targets : :theano:`TensorVariable`
194 The symbolic `targets` vector
195 """
196 Criterion.__init__(self, outputs, targets)
197
199 """ Computes the Negative Log Likelihood symbolic expression. """
200 self.expression = -T.log(T.sum(self.outputs*self.targets))
201