-
Notifications
You must be signed in to change notification settings - Fork 0
/
objective.js
189 lines (164 loc) · 4.17 KB
/
objective.js
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
import { zeros } from 'util/array.js';
const eps = 1e-8;
/**
* mean squared error
* @param {x} - output layer tensor
* @param {y} - label tensor
*/
function meanSquaredError(x, y) {
let N = x.size;
let loss = 0.;
let xw = x.w, yw = y.w, xdw = x.dw;
for (let i = 0; i < N; i++) {
let dx = xw[i] - yw[i];
xdw[i] += dx;
loss += 0.5 * dx * dx;
}
return loss;
}
/**
* mean absolute error
* @param {x} - output layer tensor
* @param {y} - label tensor
*/
function meanAbsoluteError(x, y) {
let N = x.size;
let loss = 0.;
let xw = x.w, yw = y.w, xdw = x.dw;
for (let i = 0; i < N; i++) {
let dx = xw[i] - yw[i];
xdw[i] += Math.sign(dx);
loss += Math.abs(dx);
}
return loss;
}
/**
* KL divergence
* @param {x} - output tensor
* @param {y} - label tensor
*/
function KLD(x, y) {
let N = x.size;
let loss = 0.;
let xw = x.w, yw = y.w, xdw = x.dw;
for (let i = 0; i < N; i++) {
let Pi = xw[i], Qi = yw[i];
let logpq = Math.log2(Pi / Qi + eps);
xdw[i] += 1.0 + logpq;
loss += Pi * logpq;
}
return loss;
}
/**
* Jensen–Shannon divergence
* M = 0.5(P + Q)
* JS(P, Q) = 0.5 * KL( P || M ) + 0.5 * KL( Q || M )
* @param {x} - output tensor
* @param {y} - label tensor
*/
function JSD(x, y) {
let N = x.size;
let loss = 0.;
let mw = zeros(N);
let xw = x.w, yw = y.w, xdw = x.dw;
for (let i = 0; i < N; i++) {
mw[i] = 0.5 * (xw[i] + yw[i]);
}
for (let i = 0; i < N; i++) {
let Pi = xw[i], Qi = yw[i], Mi = mw[i];
let logpm = Math.log2(Pi / Mi + eps);
let logqm = Math.log2(Qi / Mi + eps);
xdw[i] += 0.5 * logpm;
loss += 0.5 * (Pi * logpm + Qi * logqm);
}
return loss;
}
/**
* cosine distance
* @param {x} - output tensor
* @param {y} - label tensor
*/
function cosine(x, y) {
let N = x.size;
let loss = 0.;
let xw = x.w, yw = y.w, xdw = x.dw;
let modx = 0., mody = 0.;
for (let i = 0; i < N; i++) {
modx += xw[i] * xw[i];
mody += yw[i] * yw[i];
}
modx = Math.sqrt(modx);
mody = Math.sqrt(mody);
for (let i = 0; i < N; i++) {
let Xi = xw[i], Yi = yw[i];
let normx = Xi / modx;
let normy = Yi / mody;
xdw[i] += normy * (normx * normx - 1) / modx;
loss += normx * normy;
}
return 1 - loss;
}
/**
* SVM loss / Hinge loss
* @param {x} - output tensor
* @param {y} - case id
*/
function hingeLoss(x, y) {
let N = x.size, xw = x.w, xdw = x.dw;
// xdw.fill(0.); // zero out the gradient of input Vol
// we're using structured loss here, which means that the score
// of the ground truth should be higher than the score of any other
// class, by a margin
let yscore = xw[y]; // score of ground truth
let margin = 1.0;
let loss = 0.0;
for (let i = 0; i < N; i++) {
if (y === i) continue;
let ydiff = -yscore + xw[i] + margin;
if (ydiff > 0) {
// violating dimension, apply loss
xdw[i] += 1;
xdw[y] -= 1;
loss += ydiff;
}
}
return loss;
}
/**
* Multi-class logarithmic loss
* @param {x} - output tensor
* @param {y} - case id
*/
function sparseMulticlassLogarithmicLoss(x, y) {
let N = x.size, xw = x.w, xdx = x.dw;
for (let i = 0; i < N; i++) {
xdw[i] = 1.0 / ((i === y ? 0.0 : 1.0) - xw[i]);
}
// loss is the class negative log likelihood
return -Math.log(xw[i]);
}
/**
* Sparse softmax entropy
* @param {x} - output tensor
* @param {y} - case id
*/
function sparseSoftmaxLoss(x, y) {
x.softmax_a(x.dw);
let v = x.dw[y];
x.dw[y] -= 1.0;
// loss is the class negative log likelihood
return -Math.log(v);
}
export { meanSquaredError, hingeLoss, sparseMulticlassLogarithmicLoss, sparseSoftmaxLoss, KLD, JSD };
export default function(name='mse') {
let dict = {
'mse' : meanSquaredError,
'mae': meanAbsoluteError,
'hinge': hingeLoss,
'mclog': sparseMulticlassLogarithmicLoss,
'softmax': sparseSoftmaxLoss,
'kld': KLD,
'jsd': JSD
}
return dict[name];
};