1 /* ///////////////////////////////////////////////////////////////////////
2 * File: standardized_converter.h
7 * Brief: The standardized_converter class
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 /* ///////////////////////////////////////////////////////////////////////
23 #if !defined(EXTL_INTELLIGENCE_ANN_NETWORK_SUPPORT)
24 # error standardized_converter.h is not supported by the current compiler.
27 /* ///////////////////////////////////////////////////////////////////////
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
48 class standardized_converter
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
;
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
; }
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
;
90 index_type i
= 0, j
= 0;
91 size_type sps_n
= sps
.size();
92 EXTL_ASSERT(sps_n
> 1);
95 vector_type
avgs(0.0, input_n
);
96 for (i
= 0; i
< sps_n
; ++i
)
97 avgs
+= sps
[i
].input();
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();
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;
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
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
;
137 /* ///////////////////////////////////////////////////////////////////////
138 * ::extl::intelligence namespace
140 EXTL_INTELLIGENCE_END_WHOLE_NAMESPACE
142 /* //////////////////////////////////////////////////////////////////// */
143 #endif /* EXTL_INTELLIGENCE_ANN_STANDARDIZED_CONVERTER_H */
144 /* //////////////////////////////////////////////////////////////////// */