forked from zhangmeishan/EGN3LDG
-
Notifications
You must be signed in to change notification settings - Fork 0
/
APParam.h
228 lines (202 loc) · 6.75 KB
/
APParam.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
220
221
222
223
224
225
226
227
228
/*
* APParam.h
*
* Created on: Jul 25, 2016
* Author: mason
*/
#ifndef AVGPARAM_H_
#define AVGPARAM_H_
#include "BaseParam.h"
#include "NRMat.h"
using namespace nr;
// Notice: aux is an auxiliary variable to help parameter updating
// The in-out dimension definiation is different with dense parameters.
struct APParam : BaseParam {
Tensor2D aux;
unordered_set<int> indexers;
int max_update;
NRVec<int> last_update;
// allow sparse and dense parameters have different parameter initialization methods
inline void initial(int outDim, int inDim, AlignedMemoryPool* mem = NULL) {
//not in the aligned memory pool
val.init(outDim, inDim);
grad.init(outDim, inDim);
aux.init(outDim, inDim);
indexers.clear();
max_update = 0;
last_update.resize(inDim);
last_update = 0;
}
inline void clearGrad() {
unordered_set<int>::iterator it;
for (it = indexers.begin(); it != indexers.end(); ++it) {
int index = *it;
for (int idx = 0; idx < val.row; idx++) {
grad[index][idx] = 0;
}
}
indexers.clear();
}
inline int outDim() {
return val.row;
}
inline int inDim() {
return val.col;
}
inline void updateAdagrad(dtype alpha, dtype reg, dtype eps) {
unordered_set<int>::iterator it;
max_update++;
for (it = indexers.begin(); it != indexers.end(); ++it) {
int index = *it;
for (int idx = 0; idx < val.row; idx++) {
aux[index][idx] += (max_update - last_update[index]) * val[index][idx] - grad[index][idx];
val[index][idx] = val[index][idx] - grad[index][idx];
}
last_update[index] = max_update;
}
}
inline void updateAdam(dtype belta1, dtype belta2, dtype alpha, dtype reg, dtype eps) {
unordered_set<int>::iterator it;
max_update++;
for (it = indexers.begin(); it != indexers.end(); ++it) {
int index = *it;
for (int idx = 0; idx < val.row; idx++) {
aux[index][idx] += (max_update - last_update[index]) * val[index][idx] - grad[index][idx];
val[index][idx] = val[index][idx] - grad[index][idx];
}
last_update[index] = max_update;
}
}
inline void randpoint(int& idx, int &idy) {
//select indexes randomly
std::vector<int> idRows, idCols;
idRows.clear();
idCols.clear();
unordered_set<int>::iterator it;
for (it = indexers.begin(); it != indexers.end(); ++it) {
idCols.push_back(*it);
}
for (int i = 0; i < val.row; i++) {
idRows.push_back(i);
}
random_shuffle(idRows.begin(), idRows.end());
random_shuffle(idCols.begin(), idCols.end());
idx = idCols[0];
idy = idRows[0];
}
inline dtype squareGradNorm() {
unordered_set<int>::iterator it;
dtype sumNorm = 0.0;
for (it = indexers.begin(); it != indexers.end(); ++it) {
int index = *it;
for (int idx = 0; idx < val.row; idx++) {
sumNorm += grad[index][idx] * grad[index][idx];
}
}
return sumNorm;
}
inline void rescaleGrad(dtype scale) {
unordered_set<int>::iterator it;
for (it = indexers.begin(); it != indexers.end(); ++it) {
int index = *it;
for (int idx = 0; idx < val.row; idx++) {
grad[index][idx] = grad[index][idx] * scale;
}
}
}
inline void sumWeight(int featId) {
if (last_update[featId] < max_update) {
int times = max_update - last_update[featId];
for (int idx = 0; idx < val.row; idx++) {
aux[featId][idx] += val[featId][idx] * times;
last_update[featId] = max_update;
}
}
}
inline void value(const int& featId, Tensor1D& out, const bool& bTrain) {
if (out.dim != val.row) {
std::cout << "warning: output dim not equal lookup param dim." << std::endl;
}
if (bTrain) {
for (int idx = 0; idx < val.row; idx++) {
out[idx] = val[featId][idx];
}
}
else {
sumWeight(featId);
for (int idx = 0; idx < val.row; idx++) {
out[idx] = aux[featId][idx];
}
}
}
inline void value(const vector<int>& featIds, Tensor1D& out, const bool& bTrain) {
if (out.dim != val.row) {
std::cout << "warning: output dim not equal lookup param dim." << std::endl;
}
int featNum = featIds.size();
int featId;
if (bTrain) {
for (int i = 0; i < featNum; i++) {
featId = featIds[i];
for (int idx = 0; idx < val.row; idx++) {
out[idx] += val[featId][idx];
}
}
}
else {
for (int i = 0; i < featNum; i++) {
featId = featIds[i];
sumWeight(featId);
for (int idx = 0; idx < val.row; idx++) {
out[idx] += aux[featId][idx];
}
}
}
}
inline void loss(const int& featId, const Tensor1D& loss) {
if (loss.dim != val.row) {
std::cout << "warning: loss dim not equal lookup param dim." << std::endl;
}
indexers.insert(featId);
for (int idx = 0; idx < val.row; idx++) {
grad[featId][idx] += loss[idx];
}
}
inline void loss(const vector<int>& featIds, const Tensor1D& loss) {
if (loss.dim != val.row) {
std::cout << "warning: loss dim not equal lookup param dim." << std::endl;
}
int featNum = featIds.size();
int featId;
for (int i = 0; i < featNum; i++) {
featId = featIds[i];
indexers.insert(featId);
for (int idx = 0; idx < val.row; idx++) {
grad[featId][idx] += loss[idx];
}
}
}
inline void save(std::ofstream &os)const {
val.save(os);
aux.save(os);
os << max_update << std::endl;
os << val.col << std::endl;
os << last_update[0];
for (int idx = 1; idx < val.col; idx++) {
os << " " << last_update[idx];
}
os << std::endl;
}
inline void load(std::ifstream &is, AlignedMemoryPool* mem = NULL) {
val.load(is);
aux.load(is);
is >> max_update;
int curInDim;
is >> curInDim;
last_update.resize(curInDim);
for (int idx = 0; idx < curInDim; idx++) {
is >> last_update[idx];
}
}
};
#endif /* AVGPARAM_H_ */