-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathDeepLSTM.hpp
59 lines (45 loc) · 2.17 KB
/
DeepLSTM.hpp
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
#pragma once
#include "LSTM.hpp"
class DeepLSTM{
public:
DeepLSTM(){};
DeepLSTM(const int inputDim, const int hiddenDim, const int depth);
DeepLSTM(const int inputDim, const int additionalInputDim, const int hiddenDim, const int depth);
class State;
class Grad;
std::vector<LSTM> lstms;
void init(Rand& rnd, const Real scale = 1.0);
void forward(const VecD& xt, const DeepLSTM::State* prev, DeepLSTM::State* cur, int startDepth = -1, int endDepth = -1);
void forward(const VecD& xt, DeepLSTM::State* cur, int startDepth = -1, int endDepth = -1);
void backward(DeepLSTM::State* prev, DeepLSTM::State* cur, DeepLSTM::Grad& grad, const VecD& xt, int startDepth = -1, int endDepth = -1);
void backward(DeepLSTM::State* cur, DeepLSTM::Grad& grad, const VecD& xt, int startDepth = -1, int endDepth = -1);
void sgd(const DeepLSTM::Grad& grad, const Real learningRate);
void save(std::ofstream& ofs);
void load(std::ifstream& ifs);
void forward(const VecD& xt, const VecD& at, const DeepLSTM::State* prev, DeepLSTM::State* cur, int startDepth = -1, int endDepth = -1);
void forward(const VecD& xt, const VecD& at, DeepLSTM::State* cur, int startDepth = -1, int endDepth = -1);
void backward(DeepLSTM::State* prev, DeepLSTM::State* cur, DeepLSTM::Grad& grad, const VecD& xt, const VecD& at, int startDepth = -1, int endDepth = -1);
void backward(DeepLSTM::State* cur, DeepLSTM::Grad& grad, const VecD& xt, const VecD& at, int startDepth = -1, int endDepth = -1);
void operator += (const DeepLSTM& lstm);
void operator /= (const Real val);
};
class DeepLSTM::State{
public:
~State() {this->clear();}
State(const DeepLSTM& dlstm);
std::vector<LSTM::State*> lstm;
void clear();
};
class DeepLSTM::Grad{
public:
Grad(){}
Grad(const DeepLSTM& dlstm);
std::vector<LSTM::Grad> lstm;
void init(int depth = -1);
Real norm(int depth = -1);
void sgd(const Real learningRate, const unsigned int depth, DeepLSTM& lstm);
void adagrad(const Real learningRate, DeepLSTM& lstm, const Real initVal = 1.0);
void momentum(const Real learningRate, const Real m, DeepLSTM& lstm);
void operator += (const DeepLSTM::Grad& grad);
void operator /= (const Real val);
};