-
Notifications
You must be signed in to change notification settings - Fork 1
/
TanhHidderLayer.h
185 lines (130 loc) · 3.33 KB
/
TanhHidderLayer.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
176
177
178
179
180
181
182
183
184
185
/*
* TanhHidderLayer.h
*
* Created on: Mar 18, 2015
* Author: mszhang
*/
#ifndef SRC_TANHHIDDERLAYER_H_
#define SRC_TANHHIDDERLAYER_H_
#include <armadillo>
using namespace arma;
class TanhHidderLayer {
public:
mat _W;
mat _b;
mat _gradW;
mat _gradb;
mat _eg2W;
mat _eg2b;
bool _bzerob;
int _funcType; // 0: tanh, 1: sigmod, 2: f(x) = x*x*x
public:
TanhHidderLayer(){}
void initial(int nOSize, int nISize, bool bzerob=false) {
//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);
_bzerob = bzerob;
_funcType = 0;
}
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);
_bzerob = false;
_funcType = 0;
}
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);
_bzerob = true;
_funcType = 0;
}
virtual ~TanhHidderLayer() {
// TODO Auto-generated destructor stub
}
void setFunc(int funcType)
{
_funcType = funcType;
}
public:
void ComputeForwardScore(const mat& x, mat& mid_y, mat& y)
{
mid_y = _W * x;
if(!_bzerob)mid_y = mid_y + _b;
if(_funcType == 2)y = mid_y % mid_y % mid_y;
else if(_funcType == 1) y = 1.0/(1.0+exp(-mid_y));
else y = tanh(mid_y);
}
void ComputeBackwardLoss(const mat& x, const mat& mid_y, const mat& y, const mat& ly, mat& lx)
{
//_gradW
static mat deri_yx, cly;
if(_funcType == 2)deri_yx = 3 * ( mid_y % mid_y);
else if(_funcType == 1) deri_yx = y - y % y;
else deri_yx = 1 - y%y;
cly = ly % deri_yx;
//_gradW
_gradW = _gradW + cly*x.t();
//_gradb
if(!_bzerob)_gradb = _gradb +cly;
//lx
lx = _W.t()*cly;
}
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(!_bzerob)
{
int idz = rand()%nOSize;
std::cout << "_b[" << idz << "]=" << _b(idz, 0) << " ";
}
count++;
}
std::cout << std::endl;
}
void updateAdaGrad(double regularizationWeight, double adaAlpha, double adaEps)
{
_gradW = _gradW + _W * regularizationWeight;
_eg2W = _eg2W + _gradW % _gradW;
_W = _W - _gradW * adaAlpha / sqrt(_eg2W + adaEps);
if(!_bzerob)
{
_gradb = _gradb + _b * regularizationWeight;
_eg2b = _eg2b + _gradb % _gradb;
_b = _b - _gradb * adaAlpha / sqrt(_eg2b + adaEps);
}
clearGrad();
}
void clearGrad()
{
_gradW.zeros();
if(!_bzerob)_gradb.zeros();
}
};
#endif /* SRC_TANHHIDDERLAYER_H_ */