20 #ifndef NNET_ACTIVATION_H_ 21 #define NNET_ACTIVATION_H_ 31 static const unsigned n_in = 10;
48 template <
class data_T,
class res_T,
typename CONFIG_T>
50 for (
unsigned ii = 0;
ii < CONFIG_T::n_in;
ii++) {
58 template <
class data_T,
class res_T,
typename CONFIG_T>
59 void relu(data_T
data[CONFIG_T::n_in], res_T
res[CONFIG_T::n_in]) {
61 for (
unsigned ii = 0;
ii < CONFIG_T::n_in;
ii++) {
70 template <
class data_T,
class res_T,
int MAX_INT,
typename CONFIG_T>
73 for (
unsigned ii = 0;
ii < CONFIG_T::n_in;
ii++) {
77 else if (datareg > MAX_INT)
84 template <
class data_T,
class res_T,
typename CONFIG_T>
85 void relu6(data_T
data[CONFIG_T::n_in], res_T
res[CONFIG_T::n_in]) {
86 relu_max<data_T, res_T, 6, CONFIG_T>(
data,
res);
92 template <
class out_T>
97 template <
class res_T,
typename CONFIG_T,
int N_TABLE>
105 res_T real_val = sigmoid_fcn_float<res_T>(in_val);
107 table_out[
ii] = (res_T)real_val;
111 template <
class data_T,
class res_T,
typename CONFIG_T>
114 res_T sigmoid_table[CONFIG_T::table_size];
115 init_sigmoid_table<res_T, CONFIG_T, CONFIG_T::table_size>(sigmoid_table);
120 for (
unsigned ii = 0;
ii < CONFIG_T::n_in;
ii++) {
121 data_round =
data[
ii] * CONFIG_T::table_size / 16;
122 index = data_round + 8 * CONFIG_T::table_size / 16;
125 if (
index > CONFIG_T::table_size - 1)
126 index = CONFIG_T::table_size - 1;
136 template <
typename CONFIG_T,
int N_TABLE>
144 table_out[
ii] = real_val;
148 template <
typename CONFIG_T,
int N_TABLE>
157 table_out[
ii] = 1.0 / in_val;
163 template <
class data_T,
class res_T,
typename CONFIG_T>
166 typename CONFIG_T::table_t exp_table[CONFIG_T::table_size];
167 init_exp_table<CONFIG_T, CONFIG_T::table_size>(exp_table);
169 typename CONFIG_T::table_t invert_table[CONFIG_T::table_size];
170 init_invert_table<CONFIG_T, CONFIG_T::table_size>(invert_table);
173 typename CONFIG_T::table_t exp_res[CONFIG_T::n_in];
174 typename CONFIG_T::table_t exp_diff_res[CONFIG_T::n_in]
178 for (
int ii = 0;
ii < CONFIG_T::n_in;
ii++) {
181 for (
int ii = 0;
ii < CONFIG_T::n_in;
ii++) {
182 for (
int jj = 0;
jj < CONFIG_T::n_in;
jj++) {
184 exp_diff_res[
ii][
jj] = 1;
186 data_round = (
data[
jj] -
data[
ii]) * CONFIG_T::table_size / 16;
187 index = data_round + 8 * CONFIG_T::table_size / 16;
190 if (
index > CONFIG_T::table_size - 1)
191 index = CONFIG_T::table_size - 1;
194 exp_res[
ii] += exp_diff_res[
ii][
jj];
199 for (
unsigned ii = 0;
ii < CONFIG_T::n_in;
ii++) {
200 int exp_res_index = exp_res[
ii] * CONFIG_T::table_size / 64;
201 if (exp_res_index < 0)
203 if (exp_res_index > CONFIG_T::table_size - 1)
204 exp_res_index = CONFIG_T::table_size - 1;
206 res[
ii] = (res_T)invert_table[exp_res_index];
213 template <
typename CONFIG_T,
int N_TABLE>
220 typename CONFIG_T::table_t real_val =
tanh(in_val);
222 table_out[
ii] = real_val;
226 template <
class data_T,
class res_T,
typename CONFIG_T>
227 void tanh(data_T
data[CONFIG_T::n_in], res_T
res[CONFIG_T::n_in]) {
229 typename CONFIG_T::table_t tanh_table[CONFIG_T::table_size];
230 init_tanh_table<CONFIG_T, CONFIG_T::table_size>(tanh_table);
235 for (
int ii = 0;
ii < CONFIG_T::n_in;
ii++) {
236 data_round =
data[
ii] * CONFIG_T::table_size / 8;
237 index = data_round + 4 * CONFIG_T::table_size / 8;
241 if (
index > CONFIG_T::table_size - 1)
242 index = CONFIG_T::table_size - 1;
void init_exp_table(typename CONFIG_T::table_t table_out[N_TABLE])
void tanh(data_T data[CONFIG_T::n_in], res_T res[CONFIG_T::n_in])
static std::string const input
out_T sigmoid_fcn_float(float input)
void init_invert_table(typename CONFIG_T::table_t table_out[N_TABLE])
void linear(data_T data[CONFIG_T::n_in], res_T res[CONFIG_T::n_in])
void softmax(data_T data[CONFIG_T::n_in], res_T res[CONFIG_T::n_in])
static const unsigned reuse_factor
void init_tanh_table(typename CONFIG_T::table_t table_out[N_TABLE])
void relu6(data_T data[CONFIG_T::n_in], res_T res[CONFIG_T::n_in])
static constexpr int N_TABLE
static const unsigned n_in
static const unsigned table_size
char data[epos_bytes_allocation]
void relu_max(data_T data[CONFIG_T::n_in], res_T res[CONFIG_T::n_in])
void init_sigmoid_table(res_T table_out[N_TABLE])
void sigmoid(data_T data[CONFIG_T::n_in], res_T res[CONFIG_T::n_in])
void relu(data_T data[CONFIG_T::n_in], res_T res[CONFIG_T::n_in])
float exp_fcn_float(float input)