mean_squared_error.cpp
1// OpenNN: Open Neural Networks Library
2// www.opennn.net
3//
4// M E A N S Q U A R E D E R R O R C L A S S
5//
6// Artificial Intelligence Techniques SL
7// artelnics@artelnics.com
8
9#include "mean_squared_error.h"
10
11namespace OpenNN
12{
13
18
20{
21}
22
23
30
31MeanSquaredError::MeanSquaredError(NeuralNetwork* new_neural_network_pointer, DataSet* new_data_set_pointer)
32 : LossIndex(new_neural_network_pointer, new_data_set_pointer)
33{
34}
35
36
38
40{
41}
42
43
48
51 LossIndexBackPropagation& back_propagation) const
52{
53 Tensor<type, 0> sum_squared_error;
54
55 const Index batch_samples_number = batch.inputs_2d.dimension(0);
56
57 const type coefficient = static_cast<type>(batch_samples_number);
58
59 sum_squared_error.device(*thread_pool_device) = back_propagation.errors.contract(back_propagation.errors, SSE);
60
61 back_propagation.error = sum_squared_error(0)/coefficient;
62}
63
64
65void MeanSquaredError::calculate_error_lm(const DataSetBatch& batch,
67 LossIndexBackPropagationLM& back_propagation) const
68{
69 Tensor<type, 0> sum_squared_error;
70
71 const Index batch_samples_number = batch.inputs_2d.dimension(0);
72
73 sum_squared_error.device(*thread_pool_device) = (back_propagation.squared_errors*back_propagation.squared_errors).sum();
74
75 const type coefficient = static_cast<type>(batch_samples_number);
76
77 back_propagation.error = sum_squared_error(0)/coefficient;
78}
79
80
81void MeanSquaredError::calculate_output_delta(const DataSetBatch& batch,
82 NeuralNetworkForwardPropagation&,
83 LossIndexBackPropagation& back_propagation) const
84{
85 #ifdef OPENNN_DEBUG
86 check();
87 #endif
88
89 const Index trainable_layers_number = neural_network_pointer->get_trainable_layers_number();
90
91 LayerBackPropagation* output_layer_back_propagation = back_propagation.neural_network.layers(trainable_layers_number-1);
92
93 const Index batch_samples_number = batch.inputs_2d.dimension(0);
94
95 const type coefficient = static_cast<type>(2.0)/static_cast<type>(batch_samples_number);
96
97 switch(output_layer_back_propagation->layer_pointer->get_type())
98 {
99 case Layer::Type::Perceptron:
100 {
101 PerceptronLayerBackPropagation* perceptron_layer_back_propagation
102 = static_cast<PerceptronLayerBackPropagation*>(output_layer_back_propagation);
103
104 perceptron_layer_back_propagation->delta.device(*thread_pool_device) = coefficient*back_propagation.errors;
105 }
106 break;
107
108 case Layer::Type::Probabilistic:
109 {
110 ProbabilisticLayerBackPropagation* probabilistic_layer_back_propagation
111 = static_cast<ProbabilisticLayerBackPropagation*>(output_layer_back_propagation);
112
113 probabilistic_layer_back_propagation->delta.device(*thread_pool_device) = coefficient*back_propagation.errors;
114 }
115 break;
116
117 case Layer::Type::Recurrent:
118 {
119 RecurrentLayerBackPropagation* recurrent_layer_back_propagation
120 = static_cast<RecurrentLayerBackPropagation*>(output_layer_back_propagation);
121
122 recurrent_layer_back_propagation->delta.device(*thread_pool_device) = coefficient*back_propagation.errors;
123 }
124 break;
125
126 case Layer::Type::LongShortTermMemory:
127 {
128 LongShortTermMemoryLayerBackPropagation* long_short_term_memory_layer_back_propagation
129 = static_cast<LongShortTermMemoryLayerBackPropagation*>(output_layer_back_propagation);
130
131 long_short_term_memory_layer_back_propagation->delta.device(*thread_pool_device) = coefficient*back_propagation.errors;
132 }
133 break;
134
135 default: break;
136 }
137}
138
139
140void MeanSquaredError::calculate_output_delta_lm(const DataSetBatch&,
141 NeuralNetworkForwardPropagation&,
142 LossIndexBackPropagationLM& loss_index_back_propagation) const
143{
144#ifdef OPENNN_DEBUG
145 check();
146#endif
147
148 const Index trainable_layers_number = neural_network_pointer->get_trainable_layers_number();
149
150 LayerBackPropagationLM* output_layer_back_propagation = loss_index_back_propagation.neural_network.layers(trainable_layers_number-1);
151
152 Layer* output_layer_pointer = output_layer_back_propagation->layer_pointer;
153
154 switch(output_layer_pointer->get_type())
155 {
156 case Layer::Type::Perceptron:
157 {
158 PerceptronLayerBackPropagationLM* perceptron_layer_back_propagation
159 = static_cast<PerceptronLayerBackPropagationLM*>(output_layer_back_propagation);
160
161 memcpy(perceptron_layer_back_propagation->delta.data(),
162 loss_index_back_propagation.errors.data(),
163 static_cast<size_t>(loss_index_back_propagation.errors.size())*sizeof(type));
164
165 divide_columns(perceptron_layer_back_propagation->delta, loss_index_back_propagation.squared_errors);
166 }
167 break;
168
169 case Layer::Type::Probabilistic:
170 {
171 ProbabilisticLayerBackPropagationLM* probabilistic_layer_back_propagation
172 = static_cast<ProbabilisticLayerBackPropagationLM*>(output_layer_back_propagation);
173
174 memcpy(probabilistic_layer_back_propagation->delta.data(),
175 loss_index_back_propagation.errors.data(),
176 static_cast<size_t>(loss_index_back_propagation.errors.size())*sizeof(type));
177
178 divide_columns(probabilistic_layer_back_propagation->delta, loss_index_back_propagation.squared_errors);
179 }
180 break;
181
182 default:
183 {
184 ostringstream buffer;
185
186 buffer << "OpenNN Exception: MeanSquaredError class.\n"
187 << "Levenberg-Marquardt can only be used with Perceptron and Probabilistic layers.\n";
188
189 throw logic_error(buffer.str());
190 }
191 }
192}
193
194
195void MeanSquaredError::calculate_error_gradient_lm(const DataSetBatch& batch,
196 LossIndexBackPropagationLM& loss_index_back_propagation_lm) const
197{
198#ifdef OPENNN_DEBUG
199
200 check();
201
202#endif
203
204 const Index batch_samples_number = batch.get_samples_number();
205
206 const type coefficient = type(2)/static_cast<type>(batch_samples_number);
207
208 loss_index_back_propagation_lm.gradient.device(*thread_pool_device)
209 = loss_index_back_propagation_lm.squared_errors_jacobian.contract(loss_index_back_propagation_lm.squared_errors, AT_B);
210
211 loss_index_back_propagation_lm.gradient.device(*thread_pool_device)
212 = coefficient * loss_index_back_propagation_lm.gradient;
213}
214
215
216void MeanSquaredError::calculate_error_hessian_lm(const DataSetBatch& batch,
217 LossIndexBackPropagationLM& loss_index_back_propagation_lm) const
218{
219 #ifdef OPENNN_DEBUG
220 check();
221 #endif
222
223 const Index batch_samples_number = batch.inputs_2d.dimension(0);
224
225 const type coefficient = (static_cast<type>(2.0)/static_cast<type>(batch_samples_number));
226
227 loss_index_back_propagation_lm.hessian.device(*thread_pool_device)
228 = loss_index_back_propagation_lm.squared_errors_jacobian.contract(loss_index_back_propagation_lm.squared_errors_jacobian, AT_B);
229
230 loss_index_back_propagation_lm.hessian.device(*thread_pool_device)
231 = coefficient*loss_index_back_propagation_lm.hessian;
232}
233
234
236
238{
239 return "MEAN_SQUARED_ERROR";
240}
241
242
244
246{
247 return "Mean squared error";
248}
249
250
253
255{
256 // Error type
257
258 file_stream.OpenElement("MeanSquaredError");
259
260 file_stream.CloseElement();
261}
262
263}
264
265
266// OpenNN: Open Neural Networks Library.
267// Copyright(C) 2005-2021 Artificial Intelligence Techniques, SL.
268//
269// This library is free software; you can redistribute it and/or
270// modify it under the terms of the GNU Lesser General Public
271// License as published by the Free Software Foundation; either
272// version 2.1 of the License, or any later version.
273//
274// This library is distributed in the hope that it will be useful,
275// but WITHOUT ANY WARRANTY; without even the implied warranty of
276// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
277// Lesser General Public License for more details.
278
279// You should have received a copy of the GNU Lesser General Public
280// License along with this library; if not, write to the Free Software
281// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
This class represents the concept of data set for data modelling problems, such as approximation,...
Definition: data_set.h:57
This abstract class represents the concept of loss index composed of an error term and a regularizati...
Definition: loss_index.h:48
NeuralNetwork * neural_network_pointer
Pointer to a neural network object.
Definition: loss_index.h:254
void check() const
Definition: loss_index.cpp:295
virtual ~MeanSquaredError()
Destructor.
void calculate_error(const DataSetBatch &, const NeuralNetworkForwardPropagation &, LossIndexBackPropagation &) const
MeanSquaredError::calculate_error.
string get_error_type() const
Returns a string with the name of the mean squared error loss type, "MEAN_SQUARED_ERROR".
void write_XML(tinyxml2::XMLPrinter &) const
string get_error_type_text() const
Returns a string with the name of the mean squared error loss type in text format.
virtual void CloseElement(bool compactMode=false)
If streaming, close the Element.
Definition: tinyxml2.cpp:2834
A loss index composed of several terms, this structure represent the First Order for this function.
Definition: loss_index.h:383