-
Notifications
You must be signed in to change notification settings - Fork 29
/
CheckGrad.h
130 lines (106 loc) · 3.63 KB
/
CheckGrad.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
/*
* CheckGrad.h
*
* Created on: Dec 4, 2015
* Author: mason
*/
#ifndef BASIC_CHECKGRAD_H_
#define BASIC_CHECKGRAD_H_
#include <iostream>
#include "tensor.h"
#include "MyLib.h"
using namespace nr;
using namespace std;
using namespace mshadow;
using namespace mshadow::expr;
using namespace mshadow::utils;
template<typename xpu, typename Example, typename Classifier>
void checkgrad(Classifier* classifier, const vector<Example>& examples, Tensor<xpu, 2, double>& Wd,
const Tensor<xpu, 2, double>& gradWd, const string& mark, int iter) {
int charseed = mark.length();
for (int i = 0; i < mark.length(); i++) {
charseed = (int) (mark[i]) * 5 + charseed;
}
srand(iter + charseed);
std::vector<int> idRows, idCols;
idRows.clear();
idCols.clear();
for (int i = 0; i < Wd.size(0); ++i)
idRows.push_back(i);
for (int idx = 0; idx < Wd.size(1); idx++)
idCols.push_back(idx);
random_shuffle(idRows.begin(), idRows.end());
random_shuffle(idCols.begin(), idCols.end());
int check_i = idRows[0], check_j = idCols[0];
dtype orginValue = Wd[check_i][check_j];
Wd[check_i][check_j] = orginValue + 0.001;
dtype lossAdd = 0.0;
for (int i = 0; i < examples.size(); i++) {
Example oneExam = examples[i];
lossAdd += classifier->computeScore(oneExam);
}
Wd[check_i][check_j] = orginValue - 0.001;
dtype lossPlus = 0.0;
for (int i = 0; i < examples.size(); i++) {
Example oneExam = examples[i];
lossPlus += classifier->computeScore(oneExam);
}
dtype mockGrad = (lossAdd - lossPlus) / 0.002;
mockGrad = mockGrad / examples.size();
dtype computeGrad = gradWd[check_i][check_j];
printf("Iteration %d, Checking gradient for %s[%d][%d]:\t", iter,
mark.c_str(), check_i, check_j);
printf("mock grad = %.18f, computed grad = %.18f\n", mockGrad, computeGrad);
Wd[check_i][check_j] = orginValue;
}
template<typename xpu, typename Example, typename Classifier>
void checkgrad(Classifier* classifier, const vector<Example>& examples, Tensor<xpu, 2, double>& Wd,
const Tensor<xpu, 2, double>& gradWd, const string& mark, int iter,
const hash_set<int>& indexes, bool bRow = true) {
if (indexes.size() == 0)
return;
int charseed = mark.length();
for (int i = 0; i < mark.length(); i++) {
charseed = (int) (mark[i]) * 5 + charseed;
}
srand(iter + charseed);
std::vector<int> idRows, idCols;
idRows.clear();
idCols.clear();
static hash_set<int>::iterator it;
if (bRow) {
for (it = indexes.begin(); it != indexes.end(); ++it)
idRows.push_back(*it);
for (int idx = 0; idx < Wd.size(1); idx++)
idCols.push_back(idx);
} else {
for (it = indexes.begin(); it != indexes.end(); ++it)
idCols.push_back(*it);
for (int idx = 0; idx < Wd.size(0); idx++)
idRows.push_back(idx);
}
random_shuffle(idRows.begin(), idRows.end());
random_shuffle(idCols.begin(), idCols.end());
int check_i = idRows[0], check_j = idCols[0];
dtype orginValue = Wd[check_i][check_j];
Wd[check_i][check_j] = orginValue + 0.001;
dtype lossAdd = 0.0;
for (int i = 0; i < examples.size(); i++) {
Example oneExam = examples[i];
lossAdd += classifier->computeScore(oneExam);
}
Wd[check_i][check_j] = orginValue - 0.001;
dtype lossPlus = 0.0;
for (int i = 0; i < examples.size(); i++) {
Example oneExam = examples[i];
lossPlus += classifier->computeScore(oneExam);
}
dtype mockGrad = (lossAdd - lossPlus) / 0.002;
mockGrad = mockGrad / examples.size();
dtype computeGrad = gradWd[check_i][check_j];
printf("Iteration %d, Checking gradient for %s[%d][%d]:\t", iter,
mark.c_str(), check_i, check_j);
printf("mock grad = %.18f, computed grad = %.18f\n", mockGrad, computeGrad);
Wd[check_i][check_j] = orginValue;
}
#endif /* BASIC_CHECKGRAD_H_ */