-
Notifications
You must be signed in to change notification settings - Fork 29
/
LookupTable.h
219 lines (168 loc) · 4.94 KB
/
LookupTable.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
219
/*
* LookupTable.h
*
* Created on: Mar 18, 2015
* Author: mszhang
*/
#ifndef SRC_LookupTable_H_
#define SRC_LookupTable_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 LookupTable {
public:
hash_set<int> _indexers;
Tensor<xpu, 2, dtype> _E;
Tensor<xpu, 2, dtype> _gradE;
Tensor<xpu, 2, dtype> _eg2E;
Tensor<xpu, 2, dtype> _ftE;
bool _bFineTune;
int _nDim;
int _nVSize;
int _max_update;
NRVec<int> _last_update;
public:
LookupTable() {
_indexers.clear();
}
inline void initial(const NRMat<dtype>& wordEmb) {
_nVSize = wordEmb.nrows();
_nDim = wordEmb.ncols();
_E = NewTensor<xpu>(Shape2(_nVSize, _nDim), d_zero);
_gradE = NewTensor<xpu>(Shape2(_nVSize, _nDim), d_zero);
_eg2E = NewTensor<xpu>(Shape2(_nVSize, _nDim), d_zero);
_ftE = NewTensor<xpu>(Shape2(_nVSize, _nDim), d_one);
assign(_E, wordEmb);
for (int idx = 0; idx < _nVSize; idx++) {
norm2one(_E, idx);
}
_bFineTune = true;
_max_update = 0;
_last_update.resize(_nVSize);
_last_update = 0;
}
inline void setEmbFineTune(bool bFineTune) {
_bFineTune = bFineTune;
}
inline void release() {
FreeSpace(&_E);
FreeSpace(&_gradE);
FreeSpace(&_eg2E);
FreeSpace(&_ftE);
_indexers.clear();
}
virtual ~LookupTable() {
// TODO Auto-generated destructor stub
}
inline dtype squarenormAll() {
dtype result = 0;
static hash_set<int>::iterator it;
for (int idx = 0; idx < _nDim; idx++) {
for (it = _indexers.begin(); it != _indexers.end(); ++it) {
result += _gradE[*it][idx] * _gradE[*it][idx];
}
}
return result;
}
inline void scaleGrad(dtype scale) {
static hash_set<int>::iterator it;
for (int idx = 0; idx < _nDim; idx++) {
for (it = _indexers.begin(); it != _indexers.end(); ++it) {
_gradE[*it][idx] = _gradE[*it][idx] * scale;
}
}
}
inline bool bEmbFineTune()
{
return _bFineTune;
}
public:
void GetEmb(int id, Tensor<xpu, 2, dtype> y) {
updateSparseWeight(id);
assert(y.size(0) == 1);
y = 0.0;
y[0] += _E[id];
}
// loss is stopped at this layer, since the input is one-hold alike
void EmbLoss(int id, Tensor<xpu, 2, dtype> ly) {
if(!_bFineTune) return;
//_gradE
assert(ly.size(0) == 1);
_gradE[id] += ly[0];
_indexers.insert(id);
}
void randomprint(int num) {
static int _nVSize, _nDim;
_nVSize = _E.size(0);
_nDim = _E.size(1);
int count = 0;
while (count < num) {
int idx = rand() % _nVSize;
int idy = rand() % _nDim;
std::cout << "_E[" << idx << "," << idy << "]=" << _E[idx][idy] << " ";
count++;
}
std::cout << std::endl;
}
void updateAdaGrad(dtype regularizationWeight, dtype adaAlpha, dtype adaEps) {
if(!_bFineTune) return;
static hash_set<int>::iterator it;
_max_update++;
Tensor<xpu, 1, dtype> sqrt_eg2E = NewTensor<xpu>(Shape1(_E.size(1)), d_zero);
for (it = _indexers.begin(); it != _indexers.end(); ++it) {
int index = *it;
_eg2E[index] = _eg2E[index] + _gradE[index] * _gradE[index];
sqrt_eg2E = F<nl_sqrt>(_eg2E[index] + adaEps);
_E[index] = (_E[index] * sqrt_eg2E - _gradE[index] * adaAlpha) / (adaAlpha * regularizationWeight + sqrt_eg2E);
_ftE[index] = sqrt_eg2E / (adaAlpha * regularizationWeight + sqrt_eg2E);
}
FreeSpace(&sqrt_eg2E);
clearGrad();
}
void clearGrad() {
static hash_set<int>::iterator it;
for (it = _indexers.begin(); it != _indexers.end(); ++it) {
int index = *it;
_gradE[index] = 0.0;
}
_indexers.clear();
}
void updateSparseWeight(int wordId) {
if(!_bFineTune) return;
if (_last_update[wordId] < _max_update) {
int times = _max_update - _last_update[wordId];
_E[wordId] = _E[wordId] * F<nl_exp>(times * F<nl_log>(_ftE[wordId]));
_last_update[wordId] = _max_update;
}
}
void writeModel(LStream &outf) {
SaveBinary(outf, _E);
SaveBinary(outf, _gradE);
SaveBinary(outf, _eg2E);
SaveBinary(outf, _ftE);
WriteBinary(outf, _bFineTune);
WriteBinary(outf, _nDim);
WriteBinary(outf, _nVSize);
WriteBinary(outf, _max_update);
WriteVector(outf, _last_update);
}
void loadModel(LStream &inf) {
LoadBinary(inf, &_E, false);
LoadBinary(inf, &_gradE, false);
LoadBinary(inf, &_eg2E, false);
LoadBinary(inf, &_ftE, false);
ReadBinary(inf, _bFineTune);
ReadBinary(inf, _nDim);
ReadBinary(inf, _nVSize);
ReadBinary(inf, _max_update);
ReadVector(inf, _last_update);
}
};
#endif /* SRC_LookupTable_H_ */