-
Notifications
You must be signed in to change notification settings - Fork 29
/
SparseUniLayer1O.h
218 lines (158 loc) · 4.77 KB
/
SparseUniLayer1O.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
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
/*
* SparseUniLayer1O.h
*
* Created on: Oct 22, 2015
* Author: mszhang
*/
#ifndef SPARSEUNILAYER1O_H_
#define SPARSEUNILAYER1O_H_
#include "tensor.h"
#include "Utiltensor.h"
#include "MyLib.h"
using namespace mshadow;
using namespace mshadow::expr;
using namespace mshadow::utils;
// Weight updating process implemented without theory support,
// but recently find an EMNLP 2015 paper "An Empirical Analysis of Optimization for Max-Margin NLP"
// In all my papers that use adagrad for sparse features, I use it for parameter updating.
template<typename xpu>
class SparseUniLayer1O {
public:
hash_set<int> _indexers;
Tensor<xpu, 1, dtype> _W;
Tensor<xpu, 1, dtype> _gradW;
Tensor<xpu, 1, dtype> _eg2W;
Tensor<xpu, 1, dtype> _ftW;
int _max_update;
NRVec<int> _last_update;
public:
SparseUniLayer1O() {
_indexers.clear();
}
inline void initial(int nISize, int seed = 0) {
dtype bound = sqrt(6.0 / (nISize + 1));
//dtype bound = 0.01;
_W = NewTensor<xpu>(Shape1(nISize), d_zero);
_gradW = NewTensor<xpu>(Shape1(nISize), d_zero);
_eg2W = NewTensor<xpu>(Shape1(nISize), d_zero);
_ftW = NewTensor<xpu>(Shape1(nISize), d_one);
random(_W, -1.0 * bound, 1.0 * bound, seed);
_max_update = 0;
_last_update.resize(nISize);
_last_update = 0;
}
inline void initial(Tensor<xpu, 1, dtype> W) {
static int nOSize, nISize;
nISize = W.size(0);
_W = NewTensor<xpu>(Shape1(nISize), d_zero);
_gradW = NewTensor<xpu>(Shape1(nISize), d_zero);
_eg2W = NewTensor<xpu>(Shape1(nISize), d_zero);
_ftW = NewTensor<xpu>(Shape1(nISize), d_one);
Copy(_W, W);
_max_update = 0;
_last_update.resize(nISize);
_last_update = 0;
}
inline void release() {
FreeSpace(&_W);
FreeSpace(&_gradW);
FreeSpace(&_eg2W);
FreeSpace(&_ftW);
_indexers.clear();
}
virtual ~SparseUniLayer1O() {
// TODO Auto-generated destructor stub
}
inline dtype squarenormAll() {
dtype result = squarenorm(_gradW);
return result;
}
inline void scaleGrad(dtype scale) {
_gradW = _gradW * scale;
}
public:
void ComputeForwardScore(const std::vector<int>& x, dtype& y) {
static long long featNum, featId;
featNum = x.size();
y = 0.0;
for (int idx = 0; idx < featNum; idx++) {
featId = x[idx];
if(featId >= _W.size(0))continue;
updateSparseWeight(featId);
y += _W[featId];
}
}
// loss is stopped at this layer, since the input is one-hold alike
void ComputeBackwardLoss(const std::vector<int>& x, dtype ly) {
//_gradW
static long long featNum, featId;
featNum = x.size();
for (int idx = 0; idx < featNum; idx++) {
featId = x[idx];
if(featId >= _W.size(0))continue;
_indexers.insert(featId);
_gradW[featId] += ly;
}
}
void randomprint(int num) {
static int nISize;
nISize = _W.size(0);
int count = 0;
while (count < num) {
int idx = rand() % nISize;
std::cout << "_W[" << idx << "]=" << _W[idx] << " ";
count++;
}
std::cout << std::endl;
}
void updateAdaGrad(dtype regularizationWeight, dtype adaAlpha, dtype adaEps) {
static int startPos;
static hash_set<int>::iterator it;
_max_update++;
dtype sqrt_eg2W = d_zero;
for (it = _indexers.begin(); it != _indexers.end(); ++it) {
int index = *it;
_eg2W[index] = _eg2W[index] + _gradW[index] * _gradW[index];
sqrt_eg2W = sqrt(_eg2W[index] + adaEps);
_W[index] = (_W[index] * sqrt_eg2W - _gradW[index] * adaAlpha) / (adaAlpha * regularizationWeight + sqrt_eg2W);
_ftW[index] = sqrt_eg2W / (adaAlpha * regularizationWeight + sqrt_eg2W);
}
//for (it = _indexers.begin(); it != _indexers.end(); ++it) {
// int index = *it;
// _W[index] = _W[index] - _gradW[index];
//}
clearGrad();
}
void clearGrad() {
static hash_set<int>::iterator it;
for (it = _indexers.begin(); it != _indexers.end(); ++it) {
int index = *it;
_gradW[index] = 0.0;
}
_indexers.clear();
}
void updateSparseWeight(long long featId) {
if (_last_update[featId] < _max_update) {
int times = _max_update - _last_update[featId];
_W[featId] = _W[featId] * exp(times * log(_ftW[featId]));
_last_update[featId] = _max_update;
}
}
void writeModel(LStream &outf) {
SaveBinary(outf, _W);
SaveBinary(outf, _gradW);
SaveBinary(outf, _eg2W);
SaveBinary(outf, _ftW);
WriteBinary(outf, _max_update);
WriteVector(outf, _last_update);
}
void loadModel(LStream &inf) {
LoadBinary(inf, &_W, false);
LoadBinary(inf, &_gradW, false);
LoadBinary(inf, &_eg2W, false);
LoadBinary(inf, &_ftW, false);
ReadBinary(inf, _max_update);
ReadVector(inf, _last_update);
}
};
#endif /* SPARSEUNILAYER1O_H_ */