scaling.cpp
1// OpenNN: Open Neural Networks Library
2// www.opennn.net
3//
4// S C A L I N G S O U R C E
5//
6// Artificial Intelligence Techniques, SL
7// artelnics@artelnics.com
8
9#include "scaling.h"
10
11namespace OpenNN
12{
13
14void scale_minimum_maximum_binary(Tensor<type, 2>& matrix,
15 const type& value_1,
16 const type& value_2,
17 const Index& column_index)
18{
19 const Index rows_number = matrix.dimension(0);
20
21 type slope = type(0);
22 type intercept = type(0);
23
24 if(value_1 > value_2)
25 {
26 slope = type(1)/(value_1-value_2);
27 intercept = -value_2/(value_1-value_2);
28 }
29 else
30 {
31 slope = type(1)/(value_2 - value_1);
32
33 intercept = -value_1/(value_2-value_1);
34 }
35
36 for(Index i = 0; i < rows_number; i++)
37 {
38 matrix(i, column_index) = slope*matrix(i, column_index)+intercept;
39 }
40}
41
42
47
48void scale_mean_standard_deviation(Tensor<type, 2>& matrix,
49 const Index& column_index,
50 const Descriptives& column_descriptives)
51{
52 const type slope = (column_descriptives.standard_deviation) < static_cast<type>(1e-3) ?
53 type(0) :
54 static_cast<type>(1)/column_descriptives.standard_deviation;
55
56 const type intercept = (column_descriptives.standard_deviation) < static_cast<type>(1e-3) ?
57 type(0) :
58 -static_cast<type>(1)*column_descriptives.mean/column_descriptives.standard_deviation;
59
60 for(Index i = 0; i < matrix.dimension(0); i++)
61 {
62 matrix(i, column_index) = matrix(i, column_index)*slope + intercept;
63 }
64}
65
66
71
72void scale_standard_deviation(Tensor<type, 2>& matrix,
73 const Index& column_index,
74 const Descriptives& column_descriptives)
75{
76 for(Index i = 0; i < matrix.dimension(0); i++)
77 {
78 matrix(i, column_index) = (matrix(i, column_index)) / column_descriptives.standard_deviation;
79 }
80}
81
82
87
88void scale_minimum_maximum(Tensor<type, 2>& matrix,
89 const Index& column_index,
90 const Descriptives& column_descriptives,
91 const type& min_range, const type& max_range)
92{
93 const type slope = abs(column_descriptives.maximum-column_descriptives.minimum) < static_cast<type>(1e-3) ?
94 type(0) :
95 type(max_range-min_range)/(column_descriptives.maximum-column_descriptives.minimum);
96
97 const type intercept = abs(column_descriptives.maximum-column_descriptives.minimum) < static_cast<type>(1e-3) ?
98 type(0) :
99 type(min_range*column_descriptives.maximum-max_range*column_descriptives.minimum)/(column_descriptives.maximum-column_descriptives.minimum);
100
101 for(Index i = 0; i < matrix.dimension(0); i++)
102 {
103 matrix(i, column_index) = matrix(i, column_index)*slope + intercept;
104 }
105
106}
107
108Tensor<type, 1> scale_minimum_maximum(const Tensor<type, 1>& x)
109{
110 const Tensor<type, 0> minimum = x.minimum();
111 const Tensor<type, 0> maximum = x.maximum();
112
113 const type min_range = type(-1);
114 const type max_range = type(1);
115
116 const type slope = (max_range-min_range)/(maximum()-minimum());
117 const type intercept = (min_range*maximum()-max_range*minimum())/(maximum()-minimum());
118
119 Tensor<type, 1> scaled_x(x.size());
120
121 for(Index i = 0; i < scaled_x.size(); i++)
122 {
123 scaled_x(i) = slope*x(i)+intercept;
124 }
125
126 return scaled_x;
127}
128
129
130Tensor<type, 2> scale_minimum_maximum(const Tensor<type, 2>& x)
131{
132 const Index rows_number = x.dimension(0);
133 const Index columns_number = x.dimension(1);
134
135 Tensor<type, 2> scaled_x(rows_number, columns_number);
136
137 const Tensor<type, 1> columns_minimums = OpenNN::columns_minimums(x);
138
139 const Tensor<type, 1> columns_maximums = OpenNN::columns_maximums(x);
140
141 const type min_range = type(-1);
142 const type max_range = type(1);
143
144 for(Index j = 0; j < columns_number; j++)
145 {
146 const type minimum = columns_minimums(j);
147 const type maximum = columns_maximums(j);
148
149 const type slope = (max_range-min_range)/(maximum - minimum);
150 const type intercept = (min_range*maximum-max_range*minimum)/(maximum - minimum);
151
152 for(Index i = 0; i < rows_number; i++)
153 {
154 scaled_x(i,j) = slope*x(i,j)+intercept;
155 }
156 }
157
158 return scaled_x;
159}
160
161void scale_logarithmic(Tensor<type, 2>& matrix, const Index& column_index)
162{
163 // Check negative values
164
165 for(Index i = 0; i < matrix.dimension(0); i++)
166 {
167 if(!isnan(matrix(i,column_index)) && matrix(i,column_index) <= type(0))
168 {
169 ostringstream buffer;
170
171 buffer << "OpenNN Exception: DataSet class.\n"
172 << "void scale_logarithmic(Tensor<type, 2>&, const Index&, const Descriptives&) method.\n"
173 << "Logarithmic scale method cannot be used with non-positive variables. \n";
174
175 throw logic_error(buffer.str());
176 }
177 }
178
179 for(Index i = 0; i < matrix.dimension(0); i++)
180 {
181 matrix(i,column_index) = log(matrix(i,column_index));
182 }
183}
184
185
190
191void unscale_minimum_maximum(Tensor<type, 2>& matrix,
192 const Index& column_index,
193 const Descriptives& column_descriptives,
194 const type& min_range, const type& max_range)
195{
196 const type slope = abs(max_range-min_range) < static_cast<type>(1e-3)
197 ? type(0)
198 : (column_descriptives.maximum-column_descriptives.minimum)/type(max_range-min_range);
199
200 const type intercept = abs(max_range-min_range) < static_cast<type>(1e-3)
201 ? type(0)
202 : -(min_range*column_descriptives.maximum-max_range*column_descriptives.minimum)/type(max_range-min_range);
203
204 for(Index i = 0; i < matrix.dimension(0); i++)
205 {
206 matrix(i, column_index) = matrix(i, column_index)*slope + intercept;
207 }
208}
209
210
215
216void unscale_mean_standard_deviation(Tensor<type, 2>& matrix, const Index& column_index, const Descriptives& column_descriptives)
217{
218 const type slope = abs(column_descriptives.mean) < static_cast<type>(1e-3) ? type(0)
219 : column_descriptives.standard_deviation;
220
221 const type intercept = abs(column_descriptives.mean) < static_cast<type>(1e-3)
222 ? column_descriptives.minimum
223 : column_descriptives.mean;
224
225 for(Index i = 0; i < matrix.dimension(0); i++)
226 {
227 matrix(i, column_index) = matrix(i, column_index)*slope + intercept;
228 }
229}
230
231
236
237void unscale_standard_deviation(Tensor<type, 2>& matrix, const Index& column_index, const Descriptives& column_descriptives)
238{
239 const type slope = abs(column_descriptives.mean) < static_cast<type>(1e-3)
240 ? type(0)
241 : column_descriptives.standard_deviation;
242
243 const type intercept = abs(column_descriptives.mean) < static_cast<type>(1e-3)
244 ? column_descriptives.minimum
245 : type(0);
246
247 for(Index i = 0; i < matrix.dimension(0); i++)
248 {
249 matrix(i, column_index) = matrix(i, column_index)*slope + intercept;
250 }
251}
252
253
258
259void unscale_logarithmic(Tensor<type, 2>& matrix, const Index& column_index)
260{
261 for(Index i = 0; i < matrix.dimension(0); i++)
262 {
263 matrix(i, column_index) = exp(matrix(i, column_index));
264 }
265}
266}
267
268
269// OpenNN: Open Neural Networks Library.
270// Copyright(C) 2005-2021 Artificial Intelligence Techniques, SL.
271//
272// This library is free software; you can redistribute it and/or
273// modify it under the terms of the GNU Lesser General Public
274// License as published by the Free Software Foundation; either
275// version 2.1 of the License, or any later version.
276//
277// This library is distributed in the hope that it will be useful,
278// but WITHOUT ANY WARRANTY; without even the implied warranty of
279// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
280// Lesser General Public License for more details.
281
282// You should have received a copy of the GNU Lesser General Public
283// License along with this library; if not, write to the Free Software
284// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
HALF_CONSTEXPR half abs(half arg)
Definition: half.hpp:2735
half log(half arg)
Definition: half.hpp:3050
HALF_CONSTEXPR bool isnan(half arg)
Definition: half.hpp:4385
half exp(half arg)
Definition: half.hpp:2936