forked from berthubert/hello-dl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
trackedfuncs.hh
203 lines (174 loc) · 4.53 KB
/
trackedfuncs.hh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#pragma once
#include "fvector.hh"
struct FuncStruct
{
typedef float (*floatfunc_t)(const float& f);
typedef fvector<4>(*fvect4func_t)(const fvector<4>& f);
typedef fvector<8>(*fvect8func_t)(const fvector<8>& f);
floatfunc_t func;
floatfunc_t deriv;
fvect4func_t func4;
fvect4func_t deriv4;
fvect8func_t func8;
fvect8func_t deriv8;
std::string name;
template<typename T>
using retfunc = T(*)(const T&);
};
template<typename T> // primary template
FuncStruct::retfunc<T> getFunc(const FuncStruct&);
template<>
inline
FuncStruct::retfunc<float> getFunc(const FuncStruct& fs)
{
return fs.func;
}
template<>
inline
FuncStruct::retfunc<fvector<4>> getFunc(const FuncStruct& fs)
{
return fs.func4;
}
template<> inline
FuncStruct::retfunc<fvector<8>> getFunc(const FuncStruct& fs)
{
return fs.func8;
}
template<typename T> // primary template
FuncStruct::retfunc<T> getDeriv(const FuncStruct&);
template<> inline
FuncStruct::retfunc<float> getDeriv(const FuncStruct& fs)
{
return fs.deriv;
}
template<> inline
FuncStruct::retfunc<fvector<4>> getDeriv(const FuncStruct& fs)
{
return fs.deriv4;
}
template<> inline
FuncStruct::retfunc<fvector<8>> getDeriv(const FuncStruct& fs)
{
return fs.deriv8;
}
inline FuncStruct MakeExpFunc()
{
FuncStruct ret;
ret.func = [](const float& f) -> float { return expf(f);};
ret.deriv = [](const float& f) -> float { return expf(f);};
ret.func4 = [](const fvector<4>& f) { return exp(f);};
ret.deriv4 = [](const fvector<4>& f) -> fvector<4> { return exp(f);};
ret.func8 = [](const fvector<8>& f) { return exp(f);};
ret.deriv8 = [](const fvector<8>& f) -> fvector<8> { return exp(f);};
ret.name="exp";
return ret;
}
inline FuncStruct MakeSquareFunc()
{
FuncStruct ret;
ret.func = [](const float& f) -> float { return f*f;};
ret.deriv = [](const float& f) -> float { return 2*f;};
ret.func4 = [](const fvector<4>& f) { return f*f;};
ret.deriv4 = [](const fvector<4>& f) -> fvector<4> { return 2*f;};
ret.func8 = [](const fvector<8>& f) { return f*f;};
ret.deriv8 = [](const fvector<8>& f) -> fvector<8> { return 2*f;};
ret.name="square";
return ret;
}
inline FuncStruct MakeReluFunc()
{
FuncStruct ret;
ret.func = [](const float& f) -> float { return std::max(0.0F, f); };
ret.deriv = [](const float& f) -> float { return f < 0.0 ? 0.0F : 1.0F; };
ret.func4 = [](const fvector<4>& in) {
fvector<4> cmp = 0;
return (cmp < in) * in;
};
ret.deriv4 = [](const fvector<4>& in) {
fvector<4> cmp = 0;
return (cmp < in) * 1.0;
};
ret.func8 = [](const fvector<8>& in) {
fvector<8> cmp = 0;
return (cmp < in) * in;
};
ret.deriv8 = [](const fvector<8>& in) {
fvector<8> cmp = 0;
return (cmp < in) * 1.0;
};
ret.name="relu";
return ret;
}
inline FuncStruct MakeTanhFunc()
{
FuncStruct ret;
ret.func = [](const float& in) { return tanhf(in); };
ret.deriv = [](const float& in) { float t = tanh(in); return 1-t*t; };
ret.func8 = [](const fvector<8>& in) { return tanh(in); };
ret.deriv8 =[](const fvector<8>& in) { fvector<8> t = tanh(in); return fvector<8>(1.0) - t*t; };
ret.name = "tanh";
return ret;
};
inline FuncStruct MakeSigmoidFunc()
{
FuncStruct ret;
ret.func8 = [](const fvector<8>& in)
{
return 1.0F / (fvector<8>(1.0F) + exp(-in));
};
ret.deriv8 = [](const fvector<8>& in)
{
fvector<8> sigma = 1.0F / (fvector<8>(1.0F) + exp(-in));
return sigma * (fvector<8>(1.0F) - sigma);
};
ret.func = [](const float& in)
{
return 1.0F / (1.0F + expf(-in));
};
ret.deriv = [](const float& in)
{
float sigma = 1.0F / (1.0F + expf(-in));
return sigma * (1.0F - sigma);
};
ret.name="sigmoid";
return ret;
};
inline FuncStruct MakeLogFunc()
{
struct FuncStruct ret;
ret.func = [](const float& in)
{
if (in == 0.0F)
return -80.0F;
return logf(in);
};
ret.deriv = [](const float& in)
{
if (in == 0.0F)
return 80.0F;
return 1.0F / in;
};
ret.func8 = [](const fvector<8>& in)
{
return log(in);
};
ret.deriv8 = [](const fvector<8>& in)
{
return 1.0/in;
};
ret.name = "log";
return ret;
};
static std::vector<FuncStruct> g_fss({
MakeSigmoidFunc(), // 0
MakeReluFunc(), // 1
MakeExpFunc(), // 2
MakeLogFunc(), // 3
MakeTanhFunc(), // 4
MakeSquareFunc()});// 5
inline uint8_t SigmoidFunc(){ return 0; }
inline uint8_t ReluFunc(){ return 1; }
inline uint8_t ExpFunc(){ return 2; }
inline uint8_t LogFunc(){ return 3; }
inline uint8_t TanhFunc(){ return 4; }
inline uint8_t SquareFunc(){ return 5; }