remove \r
[extl.git] / extl / intelligence / ann / standardized_converter.h
blobb6cae1bf49262fd893d08bea317384bd778df640
1 /* ///////////////////////////////////////////////////////////////////////
2 * File: standardized_converter.h
4 * Created: 09.04.05
5 * Updated: 09.04.05
7 * Brief: The standardized_converter class
9 * [<Home>]
10 * Copyright (c) 2008-2020, Waruqi All rights reserved.
11 * //////////////////////////////////////////////////////////////////// */
13 #ifndef EXTL_INTELLIGENCE_ANN_STANDARDIZED_CONVERTER_H
14 #define EXTL_INTELLIGENCE_ANN_STANDARDIZED_CONVERTER_H
16 /*!\file standardized_converter.h
17 * \brief standardized_converter class
20 /* ///////////////////////////////////////////////////////////////////////
21 * Compatibility
23 #if !defined(EXTL_INTELLIGENCE_ANN_NETWORK_SUPPORT)
24 # error standardized_converter.h is not supported by the current compiler.
25 #endif
27 /* ///////////////////////////////////////////////////////////////////////
28 * Includes
30 #include "prefix.h"
32 /* ///////////////////////////////////////////////////////////////////////
33 * ::extl::intelligence namespace
35 EXTL_INTELLIGENCE_BEGIN_WHOLE_NAMESPACE
38 /*!brief standardized_converter
40 * \param InN the input demension
41 * \param OutN the output demension
43 * \ingroup extl_group_intelligence
45 template< e_size_t InN
46 , e_size_t OutN
48 class standardized_converter
50 /// \name Types
51 /// @{
52 public:
53 typedef standardized_converter class_type;
54 typedef e_size_t size_type;
55 typedef e_bool_t bool_type;
56 typedef e_size_t index_type;
57 typedef e_float_t float_type;
58 typedef typename_type_k vvector_selector<float_type>::vvector_type vector_type;
59 typedef vector_type floats_type;
60 typedef typename_type_k sample_selector<InN, OutN>::float_sample_type sample_type;
61 typedef typename_type_k buffer_selector<sample_type>::large_buffer_type samples_type;
62 /// @}
64 /// \name Members
65 /// @{
66 private:
67 vector_type m_avgs;
68 vector_type m_sds;
69 /// @}
71 /// \name Accessors
72 /// @{
73 public:
74 vector_type& avgs() { return m_avgs; }
75 vector_type const& avgs() const { return m_avgs; }
77 vector_type& sds() { return m_sds; }
78 vector_type const& sds() const { return m_sds; }
79 /// @}
81 /// \name Methods
82 /// @{
83 public:
84 /// standardize samples: (x - avg) / sd
85 /// \note only convert the input data of samples
86 void convert_input(samples_type& sps)
88 size_type input_n = sample_type::en_input_size;
89 // (x - avg) / sd
90 index_type i = 0, j = 0;
91 size_type sps_n = sps.size();
92 EXTL_ASSERT(sps_n > 1);
94 // averages
95 vector_type avgs(0.0, input_n);
96 for (i = 0; i < sps_n; ++i)
97 avgs += sps[i].input();
98 avgs /= sps_n;
100 // standard deviation
101 vector_type sds(0.0, input_n);
102 for (i = 0; i < sps_n; ++i)
103 sds += (sps[i].input() - avgs).dot_pow2();
104 sds /= sps_n - 1;
106 for (j = 0; j < input_n; ++j)
108 sds[j] = xtl_sqrt(sds[j]);
109 if (xtl_abs(sds[j]) < 1e-10) sds[j] = 1e-10;
112 // standardize
113 for (i = 0; i < sps_n; ++i)
114 sps[i].input() = (sps[i].input() - avgs).dot_div(sds);
116 // updates averages and standard deviation
117 m_avgs = avgs;
118 m_sds = sds;
121 /// standardize sample: (x - avg) / sd
122 /// \note only convert the input data of sample
123 void convert_input(sample_type& sp)
125 sp.input() = (sp.input() - m_avgs).dot_div(m_sds);
127 /// restore standardized sample: x * sd + avg
128 /// \note only convert the input data of sample
129 void restore_input(sample_type& sp)
131 sp.input() = sp.input().dot_mul(m_sds) + m_avgs;
133 /// @}
137 /* ///////////////////////////////////////////////////////////////////////
138 * ::extl::intelligence namespace
140 EXTL_INTELLIGENCE_END_WHOLE_NAMESPACE
142 /* //////////////////////////////////////////////////////////////////// */
143 #endif /* EXTL_INTELLIGENCE_ANN_STANDARDIZED_CONVERTER_H */
144 /* //////////////////////////////////////////////////////////////////// */