-
-
Notifications
You must be signed in to change notification settings - Fork 236
Expand file tree
/
Copy pathrecurrent_ops.hpp
More file actions
380 lines (316 loc) · 14.6 KB
/
Copy pathrecurrent_ops.hpp
File metadata and controls
380 lines (316 loc) · 14.6 KB
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
// Copyright 2016, Tobias Hermann.
// https://github.com/Dobiasd/frugally-deep
// Distributed under the MIT License.
// (See accompanying LICENSE file or at
// https://opensource.org/licenses/MIT)
#pragma once
#include "fdeep/common.hpp"
#include "fdeep/tensor.hpp"
#include <cmath>
#include <functional>
#include <string>
namespace fdeep {
namespace internal {
using Eigen::Dynamic;
template <int Count>
using RowVector = Eigen::Matrix<float_type, 1, Count>;
inline float_type linear_activation(float_type x)
{
return x;
}
inline float_type tanh_activation(float_type x)
{
return std::tanh(x);
}
inline float_type sigmoid_activation(float_type x)
{
return 1 / (1 + std::exp(-x));
}
inline float_type swish_activation(float_type x)
{
return x / (1 + std::exp(-x));
}
inline float_type relu_activation(float_type x)
{
return std::max<float_type>(x, 0);
}
inline float_type hard_sigmoid_activation(float_type x)
{
// https://github.com/keras-team/keras/blob/f7bc67e6c105c116a2ba7f5412137acf78174b1a/keras/ops/nn.py#L316C6-L316C74
if (x < -3) {
return 0;
}
if (x > 3) {
return 1;
}
return (x / static_cast<float_type>(6)) + static_cast<float_type>(0.5);
}
inline float_type selu_activation(float_type x)
{
const float_type alpha = static_cast<float_type>(1.6732632423543772848170429916717);
const float_type scale = static_cast<float_type>(1.0507009873554804934193349852946);
return scale * (x >= 0 ? x : alpha * (std::exp(x) - 1));
}
inline float_type exponential_activation(float_type x)
{
return static_cast<float_type>(std::exp(x));
}
inline float_type gelu_activation(float_type x)
{
return static_cast<float_type>(0.5) * x * (static_cast<float_type>(1) + static_cast<float_type>(std::erf(x / std::sqrt(static_cast<float_type>(2)))));
}
inline float_type gelu_approximate_activation(float_type x)
{
// 0.5 * x * (1 + tanh(sqrt(2/pi) * (x + 0.044715 * x^3)))
const float_type sqrt_2_over_pi = static_cast<float_type>(0.7978845608028654);
const float_type c = static_cast<float_type>(0.044715);
const float_type inner = sqrt_2_over_pi * (x + c * x * x * x);
return static_cast<float_type>(0.5) * x * (static_cast<float_type>(1) + std::tanh(inner));
}
inline float_type softsign_activation(float_type x)
{
return x / (std::abs(x) + static_cast<float_type>(1));
}
inline float_type elu_activation(float_type x)
{
return x >= 0 ? x : std::exp(x) - 1;
}
inline std::function<float_type(float_type)> get_activation_func(const std::string& activation_func_name)
{
if (activation_func_name == "linear")
return linear_activation;
if (activation_func_name == "tanh")
return tanh_activation;
if (activation_func_name == "sigmoid")
return sigmoid_activation;
if (activation_func_name == "swish" || activation_func_name == "silu")
return swish_activation;
if (activation_func_name == "hard_sigmoid")
return hard_sigmoid_activation;
if (activation_func_name == "relu")
return relu_activation;
if (activation_func_name == "selu")
return selu_activation;
if (activation_func_name == "elu")
return elu_activation;
if (activation_func_name == "exponential")
return exponential_activation;
if (activation_func_name == "gelu")
return gelu_activation;
if (activation_func_name == "softsign")
return softsign_activation;
raise_error("recurrent activation function '" + activation_func_name + "' not yet implemented");
return {};
}
inline tensors lstm_impl(const tensor& input,
const std::size_t n_units,
const bool use_bias,
const bool return_sequences,
const bool return_state,
const float_vec& weights,
const float_vec& recurrent_weights,
const float_vec& bias,
const std::string& activation,
const std::string& recurrent_activation)
{
assertion(n_units > 0, "LSTM units must be > 0.");
const MappedRowMajorMatrixXf W = eigen_row_major_mat_from_shared_values(
weights.size() / (n_units * 4), n_units * 4,
weights.data());
const MappedRowMajorMatrixXf U = eigen_row_major_mat_from_shared_values(
n_units, n_units * 4, recurrent_weights.data());
RowMajorMatrixXf h = RowMajorMatrixXf::Zero(1, static_cast<EigenIndex>(n_units));
RowMajorMatrixXf c = RowMajorMatrixXf::Zero(1, static_cast<EigenIndex>(n_units));
const std::size_t n_timesteps = input.shape().width_;
const std::size_t n_features = input.shape().depth_;
const MappedRowMajorMatrixXf in = eigen_row_major_mat_from_shared_values(
n_timesteps, n_features, input.as_vector()->data());
RowMajorMatrixXf X = in * W;
if (use_bias) {
typedef Eigen::Matrix<float_type, 1, Eigen::Dynamic> Vector_Xf;
const Vector_Xf b = eigen_row_major_mat_from_shared_values(
1, n_units * 4, bias.data());
X.rowwise() += b;
}
const auto act_func = get_activation_func(activation);
const auto act_func_recurrent = get_activation_func(recurrent_activation);
const EigenIndex n = static_cast<EigenIndex>(n_units);
tensors result;
if (return_sequences)
result = { tensor(tensor_shape(n_timesteps, n_units), float_type(0)) };
else
result = { tensor(tensor_shape(n_units), float_type(0)) };
for (EigenIndex k = 0; k < static_cast<EigenIndex>(n_timesteps); ++k) {
const RowMajorMatrixXf ifco = h * U;
const RowMajorMatrixXf i = (X.block(k, 0, 1, n) + ifco.block(0, 0, 1, n)).unaryExpr(act_func_recurrent);
const RowMajorMatrixXf f = (X.block(k, n, 1, n) + ifco.block(0, n, 1, n)).unaryExpr(act_func_recurrent);
const RowMajorMatrixXf c_pre = (X.block(k, n * 2, 1, n) + ifco.block(0, n * 2, 1, n)).unaryExpr(act_func);
const RowMajorMatrixXf o = (X.block(k, n * 3, 1, n) + ifco.block(0, n * 3, 1, n)).unaryExpr(act_func_recurrent);
c = f.array() * c.array() + i.array() * c_pre.array();
h = o.array() * c.unaryExpr(act_func).array();
if (return_sequences)
for (EigenIndex idx = 0; idx < n; ++idx)
result.front().set_ignore_rank(tensor_pos(std::size_t(k), std::size_t(idx)), h(idx));
else if (k == static_cast<EigenIndex>(n_timesteps) - 1)
for (EigenIndex idx = 0; idx < n; ++idx)
result.front().set_ignore_rank(tensor_pos(std::size_t(idx)), h(idx));
}
if (return_state) {
auto state_h = tensor(tensor_shape(n_units), float_type(0));
auto state_c = tensor(tensor_shape(n_units), float_type(0));
for (EigenIndex idx = 0; idx < n; ++idx)
state_h.set_ignore_rank(tensor_pos(std::size_t(idx)), h(idx));
for (EigenIndex idx = 0; idx < n; ++idx)
state_c.set_ignore_rank(tensor_pos(std::size_t(idx)), c(idx));
result.push_back(state_h);
result.push_back(state_c);
}
return result;
}
inline tensors gru_impl(const tensor& input,
const std::size_t n_units,
const bool use_bias,
const bool reset_after,
const bool return_sequences,
const bool return_state,
const float_vec& weights,
const float_vec& recurrent_weights,
const float_vec& bias,
const std::string& activation,
const std::string& recurrent_activation)
{
assertion(n_units > 0, "GRU units must be > 0.");
const std::size_t n_timesteps = input.shape().width_;
const std::size_t n_features = input.shape().depth_;
const EigenIndex n = static_cast<EigenIndex>(n_units);
const MappedRowMajorMatrixXf W = eigen_row_major_mat_from_shared_values(
n_features, n_units * 3, weights.data());
const MappedRowMajorMatrixXf U = eigen_row_major_mat_from_shared_values(
n_units, n_units * 3, recurrent_weights.data());
// Keras GRU bias layout:
// reset_after=False, use_bias=True -> shape (3*units,)
// reset_after=True, use_bias=True -> shape (2, 3*units)
if (use_bias) {
const std::size_t expected = reset_after ? 2 * n_units * 3 : n_units * 3;
assertion(bias.size() == expected,
"GRU bias size does not match reset_after setting.");
}
RowVector<Dynamic> b_x(static_cast<EigenIndex>(n_units * 3));
if (use_bias)
std::copy_n(bias.cbegin(), n_units * 3, b_x.data());
else
b_x.setZero();
RowVector<Dynamic> b_h(static_cast<EigenIndex>(n_units * 3));
if (use_bias && reset_after)
std::copy_n(bias.cbegin() + static_cast<std::ptrdiff_t>(n_units * 3),
n_units * 3, b_h.data());
else
b_h.setZero();
RowMajorMatrixXf h = RowMajorMatrixXf::Zero(1, n);
const MappedRowMajorMatrixXf x = eigen_row_major_mat_from_shared_values(
n_timesteps, n_features, input.as_vector()->data());
RowMajorMatrixXf Wx = x * W;
Wx.rowwise() += b_x;
const auto act_func = get_activation_func(activation);
const auto act_func_recurrent = get_activation_func(recurrent_activation);
tensors result;
if (return_sequences)
result = { tensor(tensor_shape(n_timesteps, n_units), float_type(0)) };
else
result = { tensor(tensor_shape(n_units), float_type(0)) };
for (EigenIndex k = 0; k < static_cast<EigenIndex>(n_timesteps); ++k) {
RowVector<Dynamic> r;
RowVector<Dynamic> z;
RowVector<Dynamic> m;
if (reset_after) {
RowMajorMatrixXf Uh = h * U;
Uh += b_h;
z = (Wx.block(k, 0 * n, 1, n) + Uh.block(0, 0 * n, 1, n)).unaryExpr(act_func_recurrent);
r = (Wx.block(k, 1 * n, 1, n) + Uh.block(0, 1 * n, 1, n)).unaryExpr(act_func_recurrent);
m = (Wx.block(k, 2 * n, 1, n) + (r.array() * Uh.block(0, 2 * n, 1, n).array()).matrix()).unaryExpr(act_func);
} else {
z = (Wx.block(k, 0 * n, 1, n) + h * U.block(0, 0 * n, n, n) + b_h.block(0, 0 * n, 1, n)).unaryExpr(act_func_recurrent);
r = (Wx.block(k, 1 * n, 1, n) + h * U.block(0, 1 * n, n, n) + b_h.block(0, 1 * n, 1, n)).unaryExpr(act_func_recurrent);
m = (Wx.block(k, 2 * n, 1, n) + (r.array() * h.array()).matrix() * U.block(0, 2 * n, n, n) + b_h.block(0, 2 * n, 1, n)).unaryExpr(act_func);
}
h = ((1 - z.array()) * m.array() + z.array() * h.array()).matrix();
if (return_sequences)
for (EigenIndex idx = 0; idx < n; ++idx)
result.front().set_ignore_rank(tensor_pos(std::size_t(k), std::size_t(idx)), h(idx));
else if (k == static_cast<EigenIndex>(n_timesteps) - 1)
for (EigenIndex idx = 0; idx < n; ++idx)
result.front().set_ignore_rank(tensor_pos(std::size_t(idx)), h(idx));
}
if (return_state) {
auto state_h = tensor(tensor_shape(n_units), float_type(0));
for (EigenIndex idx = 0; idx < n; ++idx)
state_h.set_ignore_rank(tensor_pos(std::size_t(idx)), h(idx));
result.push_back(state_h);
}
return result;
}
inline tensors simple_rnn_impl(const tensor& input,
const std::size_t n_units,
const bool use_bias,
const bool return_sequences,
const bool return_state,
const float_vec& weights,
const float_vec& recurrent_weights,
const float_vec& bias,
const std::string& activation)
{
assertion(n_units > 0, "SimpleRNN units must be > 0.");
const std::size_t n_timesteps = input.shape().width_;
const std::size_t n_features = input.shape().depth_;
const MappedRowMajorMatrixXf W = eigen_row_major_mat_from_shared_values(
n_features, n_units, weights.data());
const MappedRowMajorMatrixXf U = eigen_row_major_mat_from_shared_values(
n_units, n_units, recurrent_weights.data());
const MappedRowMajorMatrixXf in = eigen_row_major_mat_from_shared_values(
n_timesteps, n_features, input.as_vector()->data());
RowMajorMatrixXf X = in * W;
if (use_bias) {
typedef Eigen::Matrix<float_type, 1, Eigen::Dynamic> Vector_Xf;
const Vector_Xf b = eigen_row_major_mat_from_shared_values(
1, n_units, bias.data());
X.rowwise() += b;
}
const auto act_func = get_activation_func(activation);
const EigenIndex n = static_cast<EigenIndex>(n_units);
RowMajorMatrixXf h = RowMajorMatrixXf::Zero(1, n);
tensors result;
if (return_sequences)
result = { tensor(tensor_shape(n_timesteps, n_units), float_type(0)) };
else
result = { tensor(tensor_shape(n_units), float_type(0)) };
for (EigenIndex k = 0; k < static_cast<EigenIndex>(n_timesteps); ++k) {
h = (X.block(k, 0, 1, n) + h * U).unaryExpr(act_func);
if (return_sequences)
for (EigenIndex idx = 0; idx < n; ++idx)
result.front().set_ignore_rank(tensor_pos(std::size_t(k), std::size_t(idx)), h(idx));
else if (k == static_cast<EigenIndex>(n_timesteps) - 1)
for (EigenIndex idx = 0; idx < n; ++idx)
result.front().set_ignore_rank(tensor_pos(std::size_t(idx)), h(idx));
}
if (return_state) {
auto state_h = tensor(tensor_shape(n_units), float_type(0));
for (EigenIndex idx = 0; idx < n; ++idx)
state_h.set_ignore_rank(tensor_pos(std::size_t(idx)), h(idx));
result.push_back(state_h);
}
return result;
}
inline tensor reverse_time_series_in_tensor(const tensor& ts)
{
tensor reversed = tensor(ts.shape(), float_type(0.0));
std::size_t n = 0;
for (std::size_t x = ts.shape().width_; x-- > 0;) {
for (std::size_t z = 0; z < ts.shape().depth_; ++z)
reversed.set_ignore_rank(tensor_pos(n, z),
ts.get_ignore_rank(tensor_pos(x, z)));
n++;
}
return reversed;
}
}
}