/
bsgd.js
405 lines (397 loc) · 11.8 KB
/
bsgd.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
import Matrix from '../util/matrix.js'
/**
* Budgeted Stochastic Gradient Descent
*/
export default class BSGD {
// Breaking the Curse of Kernelization: Budgeted Stochastic Gradient Descent for Large-Scale SVM Training
// https://www.jmlr.org/papers/volume13/wang12b/wang12b.pdf
/**
* @param {number} [b] Budget size
* @param {number} [eta] Learning rate
* @param {number} [lambda] Regularization parameter
* @param {'removal' | 'projection' | 'merging'} [maintenance] Maintenance type
* @param {'gaussian' | 'polynomial' | { name: 'gaussian', s?: number } | { name: 'polynomial', d?: number } | function (number[], number[]): number} [kernel] Kernel name
*/
constructor(b = 10, eta = 1, lambda = 1, maintenance = 'removal', kernel = 'gaussian') {
this._b = b
this._eta = eta
this._lambda = lambda
this._maintenance = maintenance
if (typeof kernel === 'function') {
this._kernel = kernel
} else {
if (typeof kernel === 'string') {
kernel = { name: kernel }
}
switch (kernel.name) {
case 'gaussian':
this._s = kernel.s ?? 1
this._kernel = (a, b) =>
Math.exp(-(a.reduce((s, v, i) => s + (v - b[i]) ** 2, 0) ** 2) / this._s ** 2)
break
case 'polynomial':
this._d = kernel.d ?? 2
this._kernel = (a, b) => (1 + a.reduce((s, v, i) => s + v * b[i])) ** this._d
break
}
}
this._sv = []
this._alpha = []
}
/**
* Fit model.
*
* @param {Array<Array<number>>} x Training data
* @param {Array<1 | -1>} y Target values
*/
fit(x, y) {
const phi = (1 + Math.sqrt(5)) / 2
for (let t = 0; t < x.length; t++) {
let s = 0
for (let k = 0; k < this._sv.length; k++) {
s += this._alpha[k] * this._kernel(x[t], this._sv[k])
}
this._alpha = this._alpha.map(a => a * (1 - this._eta * this._lambda))
if (s * y[t] < 1) {
this._alpha.push(this._eta * y[t])
this._sv.push(x[t])
if (this._sv.length > this._b) {
let minv = Infinity
let p = -1
for (let k = 0; k < this._sv.length; k++) {
const v = this._alpha[k] ** 2 * this._kernel(this._sv[k], this._sv[k])
if (v < minv) {
minv = v
p = k
}
}
if (this._maintenance === 'projection') {
const svp = this._sv.splice(p, 1)[0]
const ap = this._alpha.splice(p, 1)[0]
const K = new Matrix(this._b, this._b)
const k = new Matrix(this._b, 1)
for (let i = 0; i < this._b; i++) {
K.set(i, i, this._kernel(this._sv[i], this._sv[i]))
for (let j = i; j < this._b; j++) {
const kij = this._kernel(this._sv[i], this._sv[j])
K.set(i, j, kij)
K.set(j, i, kij)
}
k.set(i, 0, this._kernel(svp, this._sv[i]))
}
const norm = K.norm() / 1000
for (let i = 0; i < this._b; i++) {
K.addAt(i, i, norm)
}
const da = K.solve(k)
for (let k = 0; k < this._b; k++) {
this._alpha[k] += ap * da.at(k, 0)
}
} else if (this._maintenance === 'merging') {
let minwd = Infinity
let minq = 0
let minaz = 0
let minz = null
for (let q = 0; q < this._sv.length; q++) {
if (q === p) {
continue
}
const m = this._alpha[p] / (this._alpha[p] + this._alpha[q])
const fh = h =>
m *
this._kernel(
this._sv[p].map(v => (1 - h) * v),
this._sv[q].map(v => (1 - h) * v)
) +
(1 - m) *
this._kernel(
this._sv[p].map(v => h * v),
this._sv[q].map(v => h * v)
)
let low = [0, fh(0)]
let high = [1, fh(1)]
let mid = [1 / (1 + phi), fh(1 / (1 + phi))]
while (high[0] - low[0] > 1.0e-4) {
const mid2 = low[0] + (high[0] - mid[0])
const mid2h = fh(mid2)
if (mid2 < mid[0]) {
if (mid2h < mid[1]) {
low = [mid2, mid2h]
} else {
high = mid
mid = [mid2, mid2h]
}
} else {
if (mid2h < mid[1]) {
low = mid
mid = [mid2, mid2h]
} else {
high = [mid2, mid2h]
}
}
}
const h = (low[0] + high[0]) / 2
const z = this._sv[p].map((v, d) => h * v + (1 - h) * this._sv[q][d])
const az =
this._alpha[p] * this._kernel(this._sv[p], z) +
this._alpha[q] * this._kernel(this._sv[q], z)
const wd =
this._alpha[p] ** 2 * this._kernel(this._sv[p], this._sv[p]) +
this._alpha[q] ** 2 * this._kernel(this._sv[q], this._sv[q]) +
az ** 2 +
this._kernel(z, z) +
this._alpha[p] * this._alpha[q] * this._kernel(this._sv[p], this._sv[q]) -
this._alpha[p] * az * this._kernel(this._sv[p], z) -
this._alpha[q] * az * this._kernel(this._sv[q], z)
if (wd < minwd) {
minwd = wd
minq = q
minaz = az
minz = z
}
}
this._sv.splice(Math.max(p, minq), 1)
this._alpha.splice(Math.max(p, minq), 1)
this._sv.splice(Math.min(p, minq), 1)
this._alpha.splice(Math.min(p, minq), 1)
this._sv.push(minz)
this._alpha.push(minaz)
} else {
this._sv.splice(p, 1)
this._alpha.splice(p, 1)
}
}
}
}
}
/**
* Returns predicted values.
*
* @param {Array<Array<number>>} data Sample data
* @returns {(1 | -1)[]} Predicted values
*/
predict(data) {
const pred = []
for (let i = 0; i < data.length; i++) {
let s = 0
for (let k = 0; k < this._sv.length; k++) {
s += this._alpha[k] * this._kernel(data[i], this._sv[k])
}
pred[i] = s < 0 ? -1 : 1
}
return pred
}
}
/**
* Multiclass Budgeted Stochastic Gradient Descent
*/
export class MulticlassBSGD {
// Breaking the Curse of Kernelization: Budgeted Stochastic Gradient Descent for Large-Scale SVM Training
// https://www.jmlr.org/papers/volume13/wang12b/wang12b.pdf
/**
* @param {number} [b] Budget size
* @param {number} [eta] Learning rate
* @param {number} [lambda] Regularization parameter
* @param {'removal' | 'projection' | 'merging'} [maintenance] Maintenance type
* @param {'gaussian' | 'polynomial' | { name: 'gaussian', s?: number } | { name: 'polynomial', d?: number } | function (number[], number[]): number} [kernel] Kernel name
*/
constructor(b = 10, eta = 1, lambda = 1, maintenance = 'removal', kernel = 'gaussian') {
this._b = b
this._eta = eta
this._lambda = lambda
this._maintenance = maintenance
if (typeof kernel === 'function') {
this._kernel = kernel
} else {
if (typeof kernel === 'string') {
kernel = { name: kernel }
}
switch (kernel.name) {
case 'gaussian':
this._s = kernel.s ?? 1
this._kernel = (a, b) =>
Math.exp(-(a.reduce((s, v, i) => s + (v - b[i]) ** 2, 0) ** 2) / this._s ** 2)
break
case 'polynomial':
this._d = kernel.d ?? 2
this._kernel = (a, b) => (1 + a.reduce((s, v, i) => s + v * b[i])) ** this._d
break
}
}
this._classes = []
this._sv = []
this._alpha = []
}
/**
* Fit model.
*
* @param {Array<Array<number>>} x Training data
* @param {*[]} y Target values
*/
fit(x, y) {
const phi = (1 + Math.sqrt(5)) / 2
for (let t = 0; t < x.length; t++) {
let cidx = this._classes.indexOf(y[t])
if (cidx < 0) {
cidx = this._classes.length
this._classes.push(y[t])
this._sv.push([])
this._alpha.push([])
}
let max_s = -Infinity
let max_c = -1
for (let c = 0; c < this._classes.length; c++) {
let s = 0
for (let k = 0; k < this._sv[c].length; k++) {
s += this._alpha[c][k] * this._kernel(x[t], this._sv[c][k])
}
if (max_s < s) {
max_s = s
max_c = c
}
}
for (let c = 0; c < this._classes.length; c++) {
this._alpha[c] = this._alpha[c].map(a => a * (1 - this._eta * this._lambda))
}
if (cidx !== max_c) {
this._alpha[cidx].push(this._eta)
this._sv[cidx].push(x[t])
this._alpha[max_c].push(-this._eta)
this._sv[max_c].push(x[t])
for (const c of [cidx, max_c]) {
if (this._sv[c].length <= this._b) {
continue
}
let minv = Infinity
let p = -1
for (let k = 0; k < this._sv[c].length; k++) {
const v = this._alpha[c][k] ** 2 * this._kernel(this._sv[c][k], this._sv[c][k])
if (v < minv) {
minv = v
p = k
}
}
if (this._maintenance === 'projection') {
const svp = this._sv[c].splice(p, 1)[0]
const ap = this._alpha[c].splice(p, 1)[0]
const K = new Matrix(this._b, this._b)
const k = new Matrix(this._b, 1)
for (let i = 0; i < this._b; i++) {
K.set(i, i, this._kernel(this._sv[c][i], this._sv[c][i]))
for (let j = i; j < this._b; j++) {
const kij = this._kernel(this._sv[c][i], this._sv[c][j])
K.set(i, j, kij)
K.set(j, i, kij)
}
k.set(i, 0, this._kernel(svp, this._sv[c][i]))
}
const norm = K.norm() / 1000
for (let i = 0; i < this._b; i++) {
K.addAt(i, i, norm)
}
const da = K.solve(k)
for (let k = 0; k < this._b; k++) {
this._alpha[c][k] += ap * da.at(k, 0)
}
} else if (this._maintenance === 'merging') {
let minwd = Infinity
let minq = 0
let minaz = 0
let minz = null
for (let q = 0; q < this._sv[c].length; q++) {
if (q === p) {
continue
}
const m = this._alpha[c][p] / (this._alpha[c][p] + this._alpha[c][q])
const fh = h =>
m *
this._kernel(
this._sv[c][p].map(v => (1 - h) * v),
this._sv[c][q].map(v => (1 - h) * v)
) +
(1 - m) *
this._kernel(
this._sv[c][p].map(v => h * v),
this._sv[c][q].map(v => h * v)
)
let low = [0, fh(0)]
let high = [1, fh(1)]
let mid = [1 / (1 + phi), fh(1 / (1 + phi))]
while (high[0] - low[0] > 1.0e-4) {
const mid2 = low[0] + (high[0] - mid[0])
const mid2h = fh(mid2)
if (mid2 < mid[0]) {
if (mid2h < mid[1]) {
low = [mid2, mid2h]
} else {
high = mid
mid = [mid2, mid2h]
}
} else {
if (mid2h < mid[1]) {
low = mid
mid = [mid2, mid2h]
} else {
high = [mid2, mid2h]
}
}
}
const h = (low[0] + high[0]) / 2
const z = this._sv[c][p].map((v, d) => h * v + (1 - h) * this._sv[c][q][d])
const az =
this._alpha[c][p] * this._kernel(this._sv[c][p], z) +
this._alpha[c][q] * this._kernel(this._sv[c][q], z)
const wd =
this._alpha[c][p] ** 2 * this._kernel(this._sv[c][p], this._sv[c][p]) +
this._alpha[c][q] ** 2 * this._kernel(this._sv[c][q], this._sv[c][q]) +
az ** 2 +
this._kernel(z, z) +
this._alpha[c][p] * this._alpha[c][q] * this._kernel(this._sv[c][p], this._sv[c][q]) -
this._alpha[c][p] * az * this._kernel(this._sv[c][p], z) -
this._alpha[c][q] * az * this._kernel(this._sv[c][q], z)
if (wd < minwd) {
minwd = wd
minq = q
minaz = az
minz = z
}
}
this._sv[c].splice(Math.max(p, minq), 1)
this._alpha[c].splice(Math.max(p, minq), 1)
this._sv[c].splice(Math.min(p, minq), 1)
this._alpha[c].splice(Math.min(p, minq), 1)
this._sv[c].push(minz)
this._alpha[c].push(minaz)
} else {
this._sv[c].splice(p, 1)
this._alpha[c].splice(p, 1)
}
}
}
}
}
/**
* Returns predicted values.
*
* @param {Array<Array<number>>} data Sample data
* @returns {*[]} Predicted values
*/
predict(data) {
const pred = []
for (let i = 0; i < data.length; i++) {
let max_s = -Infinity
pred[i] = null
for (let c = 0; c < this._classes.length; c++) {
let s = 0
for (let k = 0; k < this._sv[c].length; k++) {
s += this._alpha[c][k] * this._kernel(data[i], this._sv[c][k])
}
if (max_s < s) {
max_s = s
pred[i] = this._classes[c]
}
}
}
return pred
}
}