-
Notifications
You must be signed in to change notification settings - Fork 7
/
lstm1.inc
453 lines (416 loc) · 16.2 KB
/
lstm1.inc
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
//original source https://github.com/byronknoll/lstm-compress
// v3
#include <valarray>
#include <vector>
#include <memory>
float lstmerr=0.000021;
namespace LSTM {
class Sigmoid {
public:
Sigmoid(int logit_size);
float Logit(float p) const;
static float Logistic(float p);
private:
float SlowLogit(float p);
int logit_size_;
std::vector<float> logit_table_;
};
Sigmoid::Sigmoid(int logit_size) : logit_size_(logit_size),
logit_table_(logit_size, 0) {
for (int i = 0; i < logit_size_; ++i) {
logit_table_[i] = SlowLogit((i + 0.5) / logit_size_);
}
}
float Sigmoid::Logit(float p) const {
int index = p * logit_size_;
if (index >= logit_size_) index = logit_size_ - 1;
else if (index < 0) index = 0;
return logit_table_[index];
}
float Sigmoid::Logistic(float p) {
return 1 / (1 + exp(-p));
}
float Sigmoid::SlowLogit(float p) {
return log(p / (1 - p));
}
struct NeuronLayer {
NeuronLayer(unsigned int input_size, unsigned int num_cells, int horizon,
int offset) : error_(num_cells), ivar_(horizon), gamma_(1.0, num_cells),
gamma_u_(num_cells), gamma_m_(num_cells), gamma_v_(num_cells),
beta_(num_cells), beta_u_(num_cells), beta_m_(num_cells),
beta_v_(num_cells), weights_(std::valarray<float>(input_size), num_cells),
state_(std::valarray<float>(num_cells), horizon),
update_(std::valarray<float>(input_size), num_cells),
m_(std::valarray<float>(input_size), num_cells),
v_(std::valarray<float>(input_size), num_cells),
transpose_(std::valarray<float>(num_cells), input_size - offset),
norm_(std::valarray<float>(num_cells), horizon) {};
std::valarray<float> error_, ivar_, gamma_, gamma_u_, gamma_m_, gamma_v_,
beta_, beta_u_, beta_m_, beta_v_;
std::valarray<std::valarray<float>> weights_, state_, update_, m_, v_,
transpose_, norm_;
};
class LstmLayer {
public:
LstmLayer(unsigned int input_size, unsigned int auxiliary_input_size,
unsigned int output_size, unsigned int num_cells, int horizon,
float gradient_clip, float learning_rate);
void ForwardPass(const std::valarray<float>& input, int input_symbol,
std::valarray<float>* hidden, int hidden_start);
void BackwardPass(const std::valarray<float>& input, int epoch,
int layer, int input_symbol, std::valarray<float>* hidden_error);
static inline float Rand() {
return static_cast <float> (rand()) / static_cast <float> (RAND_MAX);
}
std::vector<std::valarray<std::valarray<float>>*> Weights();
private:
std::valarray<float> state_, state_error_, stored_error_;
std::valarray<std::valarray<float>> tanh_state_, input_gate_state_,
last_state_;
float gradient_clip_, learning_rate_;
unsigned int num_cells_, epoch_, horizon_, input_size_, output_size_;
unsigned long long update_steps_ = 0;
NeuronLayer forget_gate_, input_node_, output_gate_;
void ClipGradients(std::valarray<float>* arr);
void ForwardPass(NeuronLayer& neurons, const std::valarray<float>& input,
int input_symbol);
void BackwardPass(NeuronLayer& neurons, const std::valarray<float>&input,
int epoch, int layer, int input_symbol,
std::valarray<float>* hidden_error);
void Adam(std::valarray<float>* g, std::valarray<float>* m,
std::valarray<float>* v, std::valarray<float>* w, float learning_rate,
float t) {
float beta1 = 0.025, beta2 = 0.9999, alpha = learning_rate * 0.1 /
sqrt(5e-5 * t + 1), eps = 1e-6;
(*m) *= beta1;
(*m) += (1 - beta1) * (*g);
(*v) *= beta2;
(*v) += (1 - beta2) * (*g) * (*g);
(*w) -= alpha * (((*m) / (float)(1 - pow(beta1, t))) /
(sqrt((*v) / (float)(1 - pow(beta2, t)) + eps)));
}
};
LstmLayer::LstmLayer(unsigned int input_size, unsigned int auxiliary_input_size,
unsigned int output_size, unsigned int num_cells, int horizon,
float gradient_clip, float learning_rate) :
state_(num_cells), state_error_(num_cells), stored_error_(num_cells),
tanh_state_(std::valarray<float>(num_cells), horizon),
input_gate_state_(std::valarray<float>(num_cells), horizon),
last_state_(std::valarray<float>(num_cells), horizon),
gradient_clip_(gradient_clip), learning_rate_(learning_rate),
num_cells_(num_cells), epoch_(0), horizon_(horizon),
input_size_(auxiliary_input_size), output_size_(output_size),
forget_gate_(input_size, num_cells, horizon, output_size_ + input_size_),
input_node_(input_size, num_cells, horizon, output_size_ + input_size_),
output_gate_(input_size, num_cells, horizon, output_size_ + input_size_) {
float low = -0.2;
float range = 0.4;
for (unsigned int i = 0; i < num_cells_; ++i) {
for (unsigned int j = 0; j < forget_gate_.weights_[i].size(); ++j) {
forget_gate_.weights_[i][j] = low + Rand() * range;
input_node_.weights_[i][j] = low + Rand() * range;
output_gate_.weights_[i][j] = low + Rand() * range;
}
forget_gate_.weights_[i][forget_gate_.weights_[i].size() - 1] = 1;
}
}
void LstmLayer::ForwardPass(const std::valarray<float>& input, int input_symbol,
std::valarray<float>* hidden, int hidden_start) {
last_state_[epoch_] = state_;
ForwardPass(forget_gate_, input, input_symbol);
ForwardPass(input_node_, input, input_symbol);
ForwardPass(output_gate_, input, input_symbol);
for (unsigned int i = 0; i < num_cells_; ++i) {
forget_gate_.state_[epoch_][i] = Sigmoid::Logistic(
forget_gate_.state_[epoch_][i]);
input_node_.state_[epoch_][i] = tanh(input_node_.state_[epoch_][i]);
output_gate_.state_[epoch_][i] = Sigmoid::Logistic(
output_gate_.state_[epoch_][i]);
}
input_gate_state_[epoch_] = 1.0f - forget_gate_.state_[epoch_];
state_ *= forget_gate_.state_[epoch_];
state_ += input_node_.state_[epoch_] * input_gate_state_[epoch_];
tanh_state_[epoch_] = tanh(state_);
std::slice slice = std::slice(hidden_start, num_cells_, 1);
(*hidden)[slice] = output_gate_.state_[epoch_] * tanh_state_[epoch_];
++epoch_;
if (epoch_ == horizon_) epoch_ = 0;
}
void LstmLayer::ForwardPass(NeuronLayer& neurons,
const std::valarray<float>& input, int input_symbol) {
for (unsigned int i = 0; i < num_cells_; ++i) {
float f = neurons.weights_[i][input_symbol];
for (unsigned int j = 0; j < input.size(); ++j) {
f += input[j] * neurons.weights_[i][output_size_ + j];
}
neurons.norm_[epoch_][i] = f;
}
neurons.ivar_[epoch_] = 1.0 / sqrt(((neurons.norm_[epoch_] *
neurons.norm_[epoch_]).sum() / num_cells_) + 1e-5);
neurons.norm_[epoch_] *= neurons.ivar_[epoch_];
neurons.state_[epoch_] = neurons.norm_[epoch_] * neurons.gamma_ +
neurons.beta_;
}
void LstmLayer::ClipGradients(std::valarray<float>* arr) {
for (unsigned int i = 0; i < arr->size(); ++i) {
if ((*arr)[i] < -gradient_clip_) (*arr)[i] = -gradient_clip_;
else if ((*arr)[i] > gradient_clip_) (*arr)[i] = gradient_clip_;
}
}
void LstmLayer::BackwardPass(const std::valarray<float>&input, int epoch,
int layer, int input_symbol, std::valarray<float>* hidden_error) {
if (epoch == (int)horizon_ - 1) {
stored_error_ = *hidden_error;
state_error_ = 0;
} else {
stored_error_ += *hidden_error;
}
output_gate_.error_ = tanh_state_[epoch] * stored_error_ *
output_gate_.state_[epoch] * (1.0f - output_gate_.state_[epoch]);
state_error_ += stored_error_ * output_gate_.state_[epoch] * (1.0f -
(tanh_state_[epoch] * tanh_state_[epoch]));
input_node_.error_ = state_error_ * input_gate_state_[epoch] * (1.0f -
(input_node_.state_[epoch] * input_node_.state_[epoch]));
forget_gate_.error_ = (last_state_[epoch] - input_node_.state_[epoch]) *
state_error_ * forget_gate_.state_[epoch] * input_gate_state_[epoch];
*hidden_error = 0;
if (epoch > 0) {
state_error_ *= forget_gate_.state_[epoch];
stored_error_ = 0;
} else {
++update_steps_;
}
BackwardPass(forget_gate_, input, epoch, layer, input_symbol, hidden_error);
BackwardPass(input_node_, input, epoch, layer, input_symbol, hidden_error);
BackwardPass(output_gate_, input, epoch, layer, input_symbol, hidden_error);
ClipGradients(&state_error_);
ClipGradients(&stored_error_);
ClipGradients(hidden_error);
}
void LstmLayer::BackwardPass(NeuronLayer& neurons,
const std::valarray<float>&input, int epoch, int layer, int input_symbol,
std::valarray<float>* hidden_error) {
if (epoch == (int)horizon_ - 1) {
neurons.gamma_u_ = 0;
neurons.beta_u_ = 0;
for (unsigned int i = 0; i < num_cells_; ++i) {
neurons.update_[i] = 0;
int offset = output_size_ + input_size_;
for (unsigned int j = 0; j < neurons.transpose_.size(); ++j) {
neurons.transpose_[j][i] = neurons.weights_[i][j + offset];
}
}
}
neurons.beta_u_ += neurons.error_;
neurons.gamma_u_ += neurons.error_ * neurons.norm_[epoch];
neurons.error_ *= neurons.gamma_ * neurons.ivar_[epoch];
neurons.error_ -= ((neurons.error_ * neurons.norm_[epoch]).sum() /
num_cells_) * neurons.norm_[epoch];
if (layer > 0) {
for (unsigned int i = 0; i < num_cells_; ++i) {
float f = 0;
for (unsigned int j = 0; j < num_cells_; ++j) {
f += neurons.error_[j] * neurons.transpose_[num_cells_ + i][j];
}
(*hidden_error)[i] += f;
}
}
if (epoch > 0) {
for (unsigned int i = 0; i < num_cells_; ++i) {
float f = 0;
for (unsigned int j = 0; j < num_cells_; ++j) {
f += neurons.error_[j] * neurons.transpose_[i][j];
}
stored_error_[i] += f;
}
}
std::slice slice = std::slice(output_size_, input.size(), 1);
for (unsigned int i = 0; i < num_cells_; ++i) {
neurons.update_[i][slice] += neurons.error_[i] * input;
neurons.update_[i][input_symbol] += neurons.error_[i];
}
if (epoch == 0) {
for (unsigned int i = 0; i < num_cells_; ++i) {
Adam(&neurons.update_[i], &neurons.m_[i], &neurons.v_[i],
&neurons.weights_[i], learning_rate_, update_steps_);
}
Adam(&neurons.gamma_u_, &neurons.gamma_m_, &neurons.gamma_v_,
&neurons.gamma_, learning_rate_, update_steps_);
Adam(&neurons.beta_u_, &neurons.beta_m_, &neurons.beta_v_,
&neurons.beta_, learning_rate_, update_steps_);
}
}
std::vector<std::valarray<std::valarray<float>>*> LstmLayer::Weights() {
std::vector<std::valarray<std::valarray<float>>*> weights;
weights.push_back(&forget_gate_.weights_);
weights.push_back(&input_node_.weights_);
weights.push_back(&output_gate_.weights_);
return weights;
}
class Lstm {
public:
Lstm(unsigned int input_size, unsigned int output_size, unsigned int
num_cells, unsigned int num_layers, int horizon, float learning_rate,
float gradient_clip);
std::valarray<float>& Perceive(unsigned int input);
std::valarray<float>& Predict(unsigned int input);
int ep();
private:
std::vector<std::unique_ptr<LstmLayer>> layers_;
std::vector<unsigned int> input_history_;
std::valarray<float> hidden_, hidden_error_;
std::valarray<std::valarray<std::valarray<float>>> layer_input_,
output_layer_;
std::valarray<std::valarray<float>> output_;
float learning_rate_;
unsigned int num_cells_, epoch_, horizon_, input_size_, output_size_;
};
Lstm::Lstm(unsigned int input_size, unsigned int output_size, unsigned int
num_cells, unsigned int num_layers, int horizon, float learning_rate,
float gradient_clip) : input_history_(horizon),
hidden_(num_cells * num_layers + 1), hidden_error_(num_cells),
layer_input_(std::valarray<std::valarray<float>>(std::valarray<float>
(input_size + 1 + num_cells * 2), num_layers), horizon),
output_layer_(std::valarray<std::valarray<float>>(std::valarray<float>
(num_cells * num_layers + 1), output_size), horizon),
output_(std::valarray<float>(1.0 / output_size, output_size), horizon),
learning_rate_(learning_rate), num_cells_(num_cells), epoch_(0),
horizon_(horizon), input_size_(input_size), output_size_(output_size) {
hidden_[hidden_.size() - 1] = 1;
for (int epoch = 0; epoch < horizon; ++epoch) {
layer_input_[epoch][0].resize(1 + num_cells + input_size);
for (unsigned int i = 0; i < num_layers; ++i) {
layer_input_[epoch][i][layer_input_[epoch][i].size() - 1] = 1;
}
}
for (unsigned int i = 0; i < num_layers; ++i) {
layers_.push_back(std::unique_ptr<LstmLayer>(new LstmLayer(
layer_input_[0][i].size() + output_size, input_size_, output_size_,
num_cells, horizon, gradient_clip, learning_rate)));
}
}
std::valarray<float>& Lstm::Perceive(unsigned int input) {
int last_epoch = epoch_ - 1;
if (last_epoch == -1) last_epoch = horizon_ - 1;
int old_input = input_history_[last_epoch];
input_history_[last_epoch] = input;
if (epoch_ == 0) {
for (int epoch = horizon_ - 1; epoch >= 0; --epoch) {
for (int layer = layers_.size() - 1; layer >= 0; --layer) {
int offset = layer * num_cells_;
for (unsigned int i = 0; i < output_size_; ++i) {
float error = 0;
if (i == input_history_[epoch]) error = output_[epoch][i] - 1;
else error = output_[epoch][i];
if(error<-lstmerr || error>lstmerr){
for (unsigned int j = 0; j < hidden_error_.size(); ++j) {
hidden_error_[j] += output_layer_[epoch][i][j + offset] * error;
}
}
}
int prev_epoch = epoch - 1;
if (prev_epoch == -1) prev_epoch = horizon_ - 1;
int input_symbol = input_history_[prev_epoch];
if (epoch == 0) input_symbol = old_input;
layers_[layer]->BackwardPass(layer_input_[epoch][layer], epoch, layer,
input_symbol, &hidden_error_);
}
}
}
for (unsigned int i = 0; i < output_size_; ++i) {
float error = 0;
if (i == input) error = output_[last_epoch][i] - 1;
else error = output_[last_epoch][i];
output_layer_[epoch_][i] = output_layer_[last_epoch][i];
output_layer_[epoch_][i] -= learning_rate_ * error * hidden_;
}
return Predict(input);
}
std::valarray<float>& Lstm::Predict(unsigned int input) {
for (unsigned int i = 0; i < layers_.size(); ++i) {
auto start = begin(hidden_) + i * num_cells_;
std::copy(start, start + num_cells_, begin(layer_input_[epoch_][i]) +
input_size_);
layers_[i]->ForwardPass(layer_input_[epoch_][i], input, &hidden_, i *
num_cells_);
if (i < layers_.size() - 1) {
auto start2 = begin(layer_input_[epoch_][i + 1]) + num_cells_ +
input_size_;
std::copy(start, start + num_cells_, start2);
}
}
for (unsigned int i = 0; i < output_size_; ++i) {
float sum = 0;
for (unsigned int j = 0; j < hidden_.size(); ++j) {
sum += hidden_[j] * output_layer_[epoch_][i][j];
}
output_[epoch_][i] = exp(sum);
}
output_[epoch_] /= output_[epoch_].sum();
int epoch = epoch_;
++epoch_;
if (epoch_ == horizon_) epoch_ = 0;
return output_[epoch];
}
int Lstm::ep() {
return epoch_;
}
class ByteModel {
public:
ByteModel(unsigned int num_cells, unsigned int num_layers, int horizon,
float learning_rate);
unsigned int Discretize(float p) ;
unsigned int Predict();
void Perceive(int bit);
int epoch();
int expected();
protected:
int top_, mid_, bot_;
std::valarray<float> probs_;
unsigned int bit_context_;
int ex;
Lstm lstm_;
};
ByteModel::ByteModel(unsigned int num_cells, unsigned int num_layers,
int horizon, float learning_rate) : top_(255), mid_(0), bot_(0),
probs_(1.0 / 256, 256), bit_context_(1),ex(0), lstm_(0, 256, num_cells,
num_layers, horizon, learning_rate,2) {}
unsigned int ByteModel::Discretize(float p) {
return 1 + 4094 * p;
}
unsigned int ByteModel::Predict() {
float num = 0, denom = 0;
mid_ = bot_ + ((top_ - bot_) / 2);
for (int i = bot_; i <= top_; ++i) {
denom += probs_[i];
if (i > mid_) num += probs_[i];
}
ex = bot_;
float max_prob_val = probs_[bot_];
for (int i = bot_ + 1; i <= top_; i++) {
if (probs_[i] > max_prob_val) {
max_prob_val = probs_[i];
ex = i ;
}
}
if (denom == 0) return Discretize(0.5);
return Discretize(num / denom);
}
void ByteModel::Perceive(int bit) {
if (bit) {
bot_ = mid_ + 1;
} else {
top_ = mid_;
}
bit_context_ += bit_context_ + bit;
if (bit_context_ >= 256) {
bit_context_ -= 256;
probs_ = lstm_.Perceive(bit_context_);
bit_context_ = 1;
top_ = 255;
bot_ = 0;
}
}
int ByteModel::epoch() {return lstm_.ep();}
int ByteModel::expected() {return ex;}
}