-
Notifications
You must be signed in to change notification settings - Fork 1
/
rnn.h
89 lines (74 loc) · 1.56 KB
/
rnn.h
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
#pragma once
#include <vector>
#include <cmath>
#include <Eigen/Dense>
#include <random>
using Matrix = Eigen::MatrixXd;
class RNN
{
public:
RNN(unsigned lexicSize, unsigned hiddenSize, unsigned seqLength);
void forward(std::vector<unsigned> &inputs);
void backProp(std::vector<unsigned> &targets);
void update();
std::vector<unsigned> generate(unsigned seed, unsigned int iter);
static double tanh(double x)
{
return std::tanh(x);
}
static double dtanh(double x)
{
double t = std::tanh(x);
return 1 - t * t;
}
static double exp(double x)
{
return std::exp(x);
}
static double adagradInv(double x)
{
return 1 / std::sqrt(x + 1e-8);
}
static double clip(double x)
{
if (x > 5)
return 5;
else if (x < -5)
return -5;
else
return x;
}
private:
void adagrad(Matrix& param, Matrix& dparam, Matrix& mem);
// hyperparameters
unsigned lexicSize = 26;
unsigned hiddenSize = 100; // size of hidden layer of neuron
unsigned seqLength = 30; // number of steps to unroll the RNN for
double learningRate = 1e-1;
// parameters
Matrix Wxh;
Matrix Whh;
Matrix Why;
Matrix bh;
Matrix by;
Matrix hprev;
// gradients
Matrix dWxh;
Matrix dWhh;
Matrix dWhy;
Matrix dbh;
Matrix dby;
// rnn pipeline
std::vector<Matrix> hs; // hiddenstate
std::vector<Matrix> xs; // one hot vectors
std::vector<Matrix> ys; // unormalized log prob for next
std::vector<Matrix> ps; // prob for next
// memory for adagrad
Matrix mWxh;
Matrix mWhh;
Matrix mWhy;
Matrix mbh;
Matrix mby;
std::random_device rd;
std::mt19937 gen;
};