-
Notifications
You must be signed in to change notification settings - Fork 1
/
LinearHidderLayer.h
175 lines (125 loc) · 3.08 KB
/
LinearHidderLayer.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
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
/*
* LinearHidderLayer.h
*
* Created on: Mar 18, 2015
* Author: mszhang
*/
#ifndef SRC_LINEARHIDDERLAYER_H_
#define SRC_LINEARHIDDERLAYER_H_
#include <armadillo>
using namespace arma;
class LinearHidderLayer {
public:
mat _W;
mat _b;
mat _gradW;
mat _gradb;
mat _eg2W;
mat _eg2b;
bool _bUseB;
public:
LinearHidderLayer() {}
void initial(int nOSize, int nISize, bool bUseB=true) {
//double bound = sqrt(6.0 / (nOSize + nISize+1));
double bound = 0.01;
_W.randu(nOSize, nISize); _W = _W * 2.0 * bound - bound;
_b.randu(nOSize, 1); _b = _b * 2.0 * bound - bound;
_gradW.zeros(nOSize, nISize);
_gradb.zeros(nOSize, 1);
_eg2W.zeros(nOSize, nISize);
_eg2b.zeros(nOSize, 1);
_bUseB = bUseB;
}
void initial(const mat& W, const mat& b) {
static int nOSize, nISize;
_W = W; _b = b;
nOSize = _W.n_rows;
nISize = _W.n_cols;
_gradW.zeros(nOSize, nISize);
_gradb.zeros(nOSize, 1);
_eg2W.zeros(nOSize, nISize);
_eg2b.zeros(nOSize, 1);
_bUseB = false;
}
void initial(const mat& W) {
static int nOSize, nISize;
_W = W;
nOSize = _W.n_rows;
nISize = _W.n_cols;
_b.zeros(nOSize, 1);
_gradW.zeros(nOSize, nISize);
_gradb.zeros(nOSize, 1);
_eg2W.zeros(nOSize, nISize);
_eg2b.zeros(nOSize, 1);
_bUseB = true;
}
virtual ~LinearHidderLayer() {
// TODO Auto-generated destructor stub
}
public:
void ComputeForwardScore(const mat& x, mat& y)
{
y = _W * x;
if(_bUseB)y = y + _b;
}
void ComputeForwardScorePreCompute(const mat& x, mat& y, int start_offset)
{
assert(x.n_rows + start_offset <= _W.n_cols);
y.zeros(_W.n_rows, 1);
for(int idk = 0; idk < _W.n_rows; idk++)
{
for(int idx = 0 ;idx < x.n_rows; idx++)
{
y(idk, 0) += _W(idk, start_offset + idx) * x(idx, 0);
}
}
}
void ComputeBackwardLoss(const mat& x, const mat& y, const mat& ly, mat& lx)
{
//_gradW
_gradW = _gradW + ly*x.t();
//_gradb
if(_bUseB)_gradb = _gradb +ly;
//lx
lx = _W.t()*ly;
}
void updateAdaGrad(double regularizationWeight, double adaAlpha, double adaEps)
{
_gradW = _gradW + _W * regularizationWeight;
_eg2W = _eg2W + _gradW % _gradW;
_W = _W - _gradW * adaAlpha / sqrt(_eg2W + adaEps);
if(_bUseB)
{
_gradb = _gradb + _b * regularizationWeight;
_eg2b = _eg2b + _gradb % _gradb;
_b = _b - _gradb * adaAlpha / sqrt(_eg2b + adaEps);
}
clearGrad();
}
void randomprint(int num)
{
static int nOSize, nISize;
nOSize = _W.n_rows;
nISize = _W.n_cols;
int count = 0;
while(count < num)
{
int idx = rand()%nOSize;
int idy = rand()%nISize;
std::cout << "_W[" << idx << "," << idy << "]=" << _W(idx, idy) << " ";
if(_bUseB)
{
int idz = rand()%nOSize;
std::cout << "_b[" << idz << "]=" << _b(idz, 0) << " ";
}
count++;
}
std::cout << std::endl;
}
void clearGrad()
{
_gradW.zeros();
if(_bUseB)_gradb.zeros();
}
};
#endif /* SRC_LINEARHIDDERLAYER_H_ */