remove \r
[extl.git] / extl / intelligence / ann / normalized_converter.h
blobf5428286502c5be103ad84df221d192fdd2f5772
1 /* ///////////////////////////////////////////////////////////////////////
2 * File: normalized_converter.h
4 * Created: 09.04.05
5 * Updated: 09.04.05
7 * Brief: The normalized_converter class
9 * [<Home>]
10 * Copyright (c) 2008-2020, Waruqi All rights reserved.
11 * //////////////////////////////////////////////////////////////////// */
13 #ifndef EXTL_INTELLIGENCE_ANN_NORMALIZED_CONVERTER_H
14 #define EXTL_INTELLIGENCE_ANN_NORMALIZED_CONVERTER_H
16 /*!\file normalized_converter.h
17 * \brief normalized_converter class
20 /* ///////////////////////////////////////////////////////////////////////
21 * Compatibility
23 #if !defined(EXTL_INTELLIGENCE_ANN_NETWORK_SUPPORT)
24 # error normalized_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 normalized_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 normalized_converter
50 /// \name Types
51 /// @{
52 public:
53 typedef normalized_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_maxs;
68 vector_type m_mins;
69 /// @}
71 /// \name Accessors
72 /// @{
73 public:
74 vector_type& maxs() { return m_maxs; }
75 vector_type const& maxs() const { return m_maxs; }
77 vector_type& mins() { return m_mins; }
78 vector_type const& mins() const { return m_mins; }
79 /// @}
81 /// \name Methods
82 /// @{
83 public:
84 /// normalize samples [-0.9, 0.9]: 1.8 * (x - min) / (max - min) - 0.9
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;
90 // [-0.9, 0.9]
91 index_type i = 0, j = 0;
92 size_type sps_n = sps.size();
93 EXTL_ASSERT(sps_n > 1);
95 // m_mins & m_maxs
96 m_mins = sps[0].input();
97 m_maxs = sps[0].input();
98 for (i = 0; i < sps_n; ++i)
100 for (j = 0; j < input_n; ++j)
102 m_mins[j] = sps[i].get_finput(j) < m_mins[j]? sps[i].get_finput(j) : m_mins[j];
103 m_maxs[j] = sps[i].get_finput(j) > m_maxs[j]? sps[i].get_finput(j) : m_maxs[j];
107 // normalize
108 for (i = 0; i < sps_n; ++i)
110 for (j = 0; j < input_n; ++j)
112 double dt = m_maxs[j] - m_mins[j];
113 if (xtl_abs(dt) < 1e-10) dt = 1e-10;
114 sps[i].set_finput(j, (1.8 * (sps[i].get_finput(j) - m_mins[j]) / dt) - 0.9);
119 /// normalize sample [-0.9, 0.9]: 1.8 * (x - min) / (max - min) - 0.9
120 /// \note only convert the input data of sample
121 void convert_input(sample_type& sp)
123 size_type input_n = sample_type::en_input_size;
124 for (index_type i = 0; i < input_n; ++i)
126 double dt = m_maxs[i] - m_mins[i];
127 if (xtl_abs(dt) < 1e-10) dt = 1e-10;
128 sp.set_finput(i, (1.8 * (sp.get_finput(i) - m_mins[i]) / dt) - 0.9);
131 /// restore normalized sample: (x + 0.9) * (max - min) / 1.8 + min
132 /// \note only convert the input data of sample
133 void restore_input(sample_type& sp)
135 size_type input_n = sample_type::en_input_size;
136 for (index_type i = 0; i < input_n; ++i)
138 double dt = m_maxs[i] - m_mins[i];
139 sp.set_finput(i, ((sp.get_finput(i) + 0.9) * dt / 1.8) + m_mins[i]);
142 /// @}
146 /* ///////////////////////////////////////////////////////////////////////
147 * ::extl::intelligence namespace
149 EXTL_INTELLIGENCE_END_WHOLE_NAMESPACE
151 /* //////////////////////////////////////////////////////////////////// */
152 #endif /* EXTL_INTELLIGENCE_ANN_NORMALIZED_CONVERTER_H */
153 /* //////////////////////////////////////////////////////////////////// */