forked from karpathy/convnetjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
convnet_layers_loss.js
234 lines (209 loc) · 6.74 KB
/
convnet_layers_loss.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
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
229
230
231
232
233
(function(global) {
"use strict";
var Vol = global.Vol; // convenience
// Layers that implement a loss. Currently these are the layers that
// can initiate a backward() pass. In future we probably want a more
// flexible system that can accomodate multiple losses to do multi-task
// learning, and stuff like that. But for now, one of the layers in this
// file must be the final layer in a Net.
// This is a classifier, with N discrete classes from 0 to N-1
// it gets a stream of N incoming numbers and computes the softmax
// function (exponentiate and normalize to sum to 1 as probabilities should)
var SoftmaxLayer = function(opt) {
var opt = opt || {};
// computed
this.num_inputs = opt.in_sx * opt.in_sy * opt.in_depth;
this.out_depth = this.num_inputs;
this.out_sx = 1;
this.out_sy = 1;
this.layer_type = 'softmax';
}
SoftmaxLayer.prototype = {
forward: function(V, is_training) {
this.in_act = V;
var A = new Vol(1, 1, this.out_depth, 0.0);
// compute max activation
var as = V.w;
var amax = V.w[0];
for(var i=1;i<this.out_depth;i++) {
if(as[i] > amax) amax = as[i];
}
// compute exponentials (carefully to not blow up)
var es = global.zeros(this.out_depth);
var esum = 0.0;
for(var i=0;i<this.out_depth;i++) {
var e = Math.exp(as[i] - amax);
esum += e;
es[i] = e;
}
// normalize and output to sum to one
for(var i=0;i<this.out_depth;i++) {
es[i] /= esum;
A.w[i] = es[i];
}
this.es = es; // save these for backprop
this.out_act = A;
return this.out_act;
},
backward: function(y) {
// compute and accumulate gradient wrt weights and bias of this layer
var x = this.in_act;
x.dw = global.zeros(x.w.length); // zero out the gradient of input Vol
for(var i=0;i<this.out_depth;i++) {
var indicator = i === y ? 1.0 : 0.0;
var mul = -(indicator - this.es[i]);
x.dw[i] = mul;
}
// loss is the class negative log likelihood
return -Math.log(this.es[y]);
},
getParamsAndGrads: function() {
return [];
},
toJSON: function() {
var json = {};
json.out_depth = this.out_depth;
json.out_sx = this.out_sx;
json.out_sy = this.out_sy;
json.layer_type = this.layer_type;
json.num_inputs = this.num_inputs;
return json;
},
fromJSON: function(json) {
this.out_depth = json.out_depth;
this.out_sx = json.out_sx;
this.out_sy = json.out_sy;
this.layer_type = json.layer_type;
this.num_inputs = json.num_inputs;
}
}
// implements an L2 regression cost layer,
// so penalizes \sum_i(||x_i - y_i||^2), where x is its input
// and y is the user-provided array of "correct" values.
var RegressionLayer = function(opt) {
var opt = opt || {};
// computed
this.num_inputs = opt.in_sx * opt.in_sy * opt.in_depth;
this.out_depth = this.num_inputs;
this.out_sx = 1;
this.out_sy = 1;
this.layer_type = 'regression';
}
RegressionLayer.prototype = {
forward: function(V, is_training) {
this.in_act = V;
this.out_act = V;
return V; // identity function
},
// y is a list here of size num_inputs
// or it can be a number if only one value is regressed
// or it can be a struct {dim: i, val: x} where we only want to
// regress on dimension i and asking it to have value x
backward: function(y) {
// compute and accumulate gradient wrt weights and bias of this layer
var x = this.in_act;
x.dw = global.zeros(x.w.length); // zero out the gradient of input Vol
var loss = 0.0;
if(y instanceof Array || y instanceof Float64Array) {
for(var i=0;i<this.out_depth;i++) {
var dy = x.w[i] - y[i];
x.dw[i] = dy;
loss += 0.5*dy*dy;
}
} else if(typeof y === 'number') {
// lets hope that only one number is being regressed
var dy = x.w[0] - y;
x.dw[0] = dy;
loss += 0.5*dy*dy;
} else {
// assume it is a struct with entries .dim and .val
// and we pass gradient only along dimension dim to be equal to val
var i = y.dim;
var yi = y.val;
var dy = x.w[i] - yi;
x.dw[i] = dy;
loss += 0.5*dy*dy;
}
return loss;
},
getParamsAndGrads: function() {
return [];
},
toJSON: function() {
var json = {};
json.out_depth = this.out_depth;
json.out_sx = this.out_sx;
json.out_sy = this.out_sy;
json.layer_type = this.layer_type;
json.num_inputs = this.num_inputs;
return json;
},
fromJSON: function(json) {
this.out_depth = json.out_depth;
this.out_sx = json.out_sx;
this.out_sy = json.out_sy;
this.layer_type = json.layer_type;
this.num_inputs = json.num_inputs;
}
}
var SVMLayer = function(opt) {
var opt = opt || {};
// computed
this.num_inputs = opt.in_sx * opt.in_sy * opt.in_depth;
this.out_depth = this.num_inputs;
this.out_sx = 1;
this.out_sy = 1;
this.layer_type = 'svm';
}
SVMLayer.prototype = {
forward: function(V, is_training) {
this.in_act = V;
this.out_act = V; // nothing to do, output raw scores
return V;
},
backward: function(y) {
// compute and accumulate gradient wrt weights and bias of this layer
var x = this.in_act;
x.dw = global.zeros(x.w.length); // 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
var yscore = x.w[y]; // score of ground truth
var margin = 1.0;
var loss = 0.0;
for(var i=0;i<this.out_depth;i++) {
if(y === i) { continue; }
var ydiff = -yscore + x.w[i] + margin;
if(ydiff > 0) {
// violating dimension, apply loss
x.dw[i] += 1;
x.dw[y] -= 1;
loss += ydiff;
}
}
return loss;
},
getParamsAndGrads: function() {
return [];
},
toJSON: function() {
var json = {};
json.out_depth = this.out_depth;
json.out_sx = this.out_sx;
json.out_sy = this.out_sy;
json.layer_type = this.layer_type;
json.num_inputs = this.num_inputs;
return json;
},
fromJSON: function(json) {
this.out_depth = json.out_depth;
this.out_sx = json.out_sx;
this.out_sy = json.out_sy;
this.layer_type = json.layer_type;
this.num_inputs = json.num_inputs;
}
}
global.RegressionLayer = RegressionLayer;
global.SoftmaxLayer = SoftmaxLayer;
global.SVMLayer = SVMLayer;
})(convnetjs);