Skip to content

Commit 50f3641

Browse files
authored
Accept object for kernel argument (#815)
* Remove kernel argument and accept object for kernel argument * Accept object for kernel argument
1 parent 2cf9994 commit 50f3641

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+516
-387
lines changed

js/view/gplvm.js

Lines changed: 14 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,32 @@
11
import GPLVM from '../../lib/model/gplvm.js'
22
import Controller from '../controller.js'
33

4-
var dispGPLVM = function (elm, platform) {
4+
export default function (platform) {
5+
platform.setting.ml.usage = 'Click and add data point. Next, click "Fit" button.'
56
const controller = new Controller(platform)
67
let model = null
78
const fitModel = () => {
89
if (!model) {
910
const dim = platform.dimension
10-
const alpha = +elm.select('[name=alpha]').property('value')
11-
const sigma = +elm.select('[name=sigma]').property('value')
12-
const ez = +elm.select('[name=ez]').property('value')
13-
const ea = +elm.select('[name=ea]').property('value')
14-
const ep = +elm.select('[name=ep]').property('value')
15-
model = new GPLVM(dim, alpha, ez, ea, ep, 'gaussian', [1.0, sigma])
11+
model = new GPLVM(dim, alpha.value, ez.value, ea.value, ep.value, {
12+
name: kernel.value,
13+
a: 1.0,
14+
b: gauss_sigma.avlue,
15+
})
1616
model.init(platform.trainInput)
1717
}
1818
model.fit()
1919
const y = model.predict(platform.trainInput)
2020
platform.trainResult = y
2121
}
2222

23-
const kernelElm = elm.append('span')
24-
kernelElm
25-
.append('select')
26-
.attr('name', 'kernel')
27-
.selectAll('option')
28-
.data(['gaussian'])
29-
.enter()
30-
.append('option')
31-
.attr('value', d => d)
32-
.text(d => d)
33-
const gauss_sigma = kernelElm.append('span')
34-
gauss_sigma
35-
.append('span')
36-
.text(' sigma = ')
37-
.append('input')
38-
.attr('type', 'number')
39-
.attr('name', 'sigma')
40-
.attr('value', 1)
41-
.attr('min', 0)
42-
.attr('max', 10)
43-
.attr('step', 0.1)
44-
elm.append('span')
45-
.text(' alpha = ')
46-
.append('input')
47-
.attr('type', 'number')
48-
.attr('name', 'alpha')
49-
.attr('value', 0.05)
50-
.attr('min', 0)
51-
.attr('max', 10)
52-
.attr('step', 0.01)
53-
elm.append('span')
54-
.text(' ez = ')
55-
.append('input')
56-
.attr('type', 'number')
57-
.attr('name', 'ez')
58-
.attr('value', 1)
59-
.attr('min', 0)
60-
.attr('max', 10)
61-
.attr('step', 0.1)
62-
elm.append('span')
63-
.text(' ea = ')
64-
.append('input')
65-
.attr('type', 'number')
66-
.attr('name', 'ea')
67-
.attr('value', 0.005)
68-
.attr('min', 0)
69-
.attr('max', 10)
70-
.attr('step', 0.001)
71-
elm.append('span')
72-
.text(' ep = ')
73-
.append('input')
74-
.attr('type', 'number')
75-
.attr('name', 'ep')
76-
.attr('value', 0.02)
77-
.attr('min', 0)
78-
.attr('max', 10)
79-
.attr('step', 0.001)
23+
const kernelElm = controller.span()
24+
const kernel = kernelElm.select(['gaussian'])
25+
const gauss_sigma = kernelElm.input.number({ label: ' sigma = ', min: 0, max: 10, step: 0.1, value: 1 })
26+
const alpha = controller.input.number({ label: ' alpha = ', min: 0, max: 10, step: 0.01, value: 0.05 })
27+
const ez = controller.input.number({ label: ' ez = ', min: 0, max: 10, step: 0.1, value: 1 })
28+
const ea = controller.input.number({ label: ' ea = ', min: 0, max: 10, step: 0.001, value: 0.005 })
29+
const ep = controller.input.number({ label: ' ep = ', min: 0, max: 10, step: 0.001, value: 0.02 })
8030
controller
8131
.stepLoopButtons()
8232
.init(() => {
@@ -86,8 +36,3 @@ var dispGPLVM = function (elm, platform) {
8636
.step(fitModel)
8737
.epoch()
8838
}
89-
90-
export default function (platform) {
91-
platform.setting.ml.usage = 'Click and add data point. Next, click "Fit" button.'
92-
dispGPLVM(platform.setting.ml.configElement, platform)
93-
}

js/view/ocsvm.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,7 @@ export default function (platform) {
5050
step: 0.01,
5151
})
5252
const slbConf = controller.stepLoopButtons().init(() => {
53-
const args = []
54-
if (kernel.value === 'gaussian') {
55-
args.push(gamma.value)
56-
}
57-
model = new OCSVM(nu.value, kernel.value, args)
53+
model = new OCSVM(nu.value, { name: kernel.value, d: gamma.value })
5854
model.init(platform.trainInput, platform.trainOutput)
5955
learn_epoch = 0
6056
platform.init()

js/view/pca.js

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,7 @@ export default function (platform) {
1313
} else if (type.value === 'dual') {
1414
model = new DualPCA()
1515
} else {
16-
const args = []
17-
if (kernel.value === 'polynomial') {
18-
args.push(poly_d.value)
19-
} else if (kernel.value === 'gaussian') {
20-
args.push(sigma.value)
21-
}
22-
model = new KernelPCA(kernel.value, args)
16+
model = new KernelPCA({ name: kernel.value, sigma: sigma.value, n: poly_d.value })
2317
}
2418
model.fit(platform.trainInput)
2519
const y = model.predict(platform.trainInput, dim)

js/view/s3vm.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,7 @@ export default function (platform) {
3434
step: 0.01,
3535
})
3636
const slbConf = controller.stepLoopButtons().init(() => {
37-
const kernel_args = []
38-
if (kernel.value === 'gaussian') {
39-
kernel_args.push(gamma.value)
40-
}
41-
model = new S3VM(kernel.value, kernel_args)
37+
model = new S3VM({ name: kernel.value, d: gamma.value })
4238
model.init(
4339
platform.trainInput,
4440
platform.trainOutput.map(v => (v[0] == null ? null : v[0] === 1 ? -1 : 1))

js/view/svc.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,7 @@ export default function (platform) {
3535
controller
3636
.stepLoopButtons()
3737
.init(() => {
38-
const kernel_args = []
39-
if (kernel.value === 'gaussian') {
40-
kernel_args.push(gamma.value)
41-
}
42-
model = new SVC(kernel.value, kernel_args)
38+
model = new SVC({ name: kernel.value, d: gamma.value })
4339
model.init(platform.trainInput)
4440
platform.init()
4541
})

js/view/svm.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,8 @@ export default function (platform) {
4141
step: 0.01,
4242
})
4343
const slbConf = controller.stepLoopButtons().init(() => {
44-
const kernel_args = []
45-
if (kernel.value === 'gaussian') {
46-
kernel_args.push(gamma.value)
47-
}
4844
model = new EnsembleBinaryModel(function () {
49-
return new SVM(kernel.value, kernel_args)
45+
return new SVM({ name: kernel.value, d: gamma.value })
5046
}, method.value)
5147
model.init(
5248
platform.trainInput,

js/view/svr.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,7 @@ export default function (platform) {
3131
step: 0.1,
3232
})
3333
const slbConf = controller.stepLoopButtons().init(() => {
34-
const args = []
35-
if (kernel.value === 'gaussian') {
36-
args.push(gamma.value)
37-
}
38-
model = new SVR(kernel.value, args)
34+
model = new SVR({ name: kernel.value, d: gamma.value })
3935
model.init(platform.trainInput, platform.trainOutput)
4036
learn_epoch = 0
4137
platform.init()

lib/model/bogd.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export default class BOGD {
1010
* @param {number} [lambda=0.1] Regularization parameter
1111
* @param {number} [gamma=0.1] Maximum coefficient
1212
* @param {'uniform' | 'nonuniform'} [sampling=nonuniform] Sampling approach
13-
* @param {'gaussian' | 'polynomial' | function (number[], number[]): number} [kernel=gaussian] Kernel name
13+
* @param {'gaussian' | 'polynomial' | { name: 'gaussian', s?: number } | { name: 'polynomial', d?: number } | function (number[], number[]): number} [kernel=gaussian] Kernel name
1414
* @param {'zero_one' | 'hinge'} [loss=hinge] Loss type name
1515
*/
1616
constructor(
@@ -31,14 +31,17 @@ export default class BOGD {
3131
if (typeof kernel === 'function') {
3232
this._kernel = kernel
3333
} else {
34-
switch (kernel) {
34+
if (typeof kernel === 'string') {
35+
kernel = { name: kernel }
36+
}
37+
switch (kernel.name) {
3538
case 'gaussian':
36-
this._s = 1
39+
this._s = kernel.s ?? 1
3740
this._kernel = (a, b) =>
3841
Math.exp(-(a.reduce((s, v, i) => s + (v - b[i]) ** 2, 0) ** 2) / this._s ** 2)
3942
break
4043
case 'polynomial':
41-
this._d = 2
44+
this._d = kernel.d ?? 2
4245
this._kernel = (a, b) => (1 + a.reduce((s, v, i) => s + v * b[i])) ** this._d
4346
break
4447
}

lib/model/bpa.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export default class BPA {
1010
* @param {number} [c=1] Regularization parameter
1111
* @param {number} [b=10] Budget size
1212
* @param {'simple' | 'projecting' | 'nn'} [version=simple] Version
13-
* @param {'gaussian' | 'polynomial' | function (number[], number[]): number} [kernel=gaussian] Kernel name
13+
* @param {'gaussian' | 'polynomial' | { name: 'gaussian', s?: number } | { name: 'polynomial', d?: number } | function (number[], number[]): number} [kernel=gaussian] Kernel name
1414
*/
1515
constructor(c = 1, b = 10, version = 'simple', kernel = 'gaussian') {
1616
this._c = c
@@ -19,14 +19,17 @@ export default class BPA {
1919
if (typeof kernel === 'function') {
2020
this._kernel = kernel
2121
} else {
22-
switch (kernel) {
22+
if (typeof kernel === 'string') {
23+
kernel = { name: kernel }
24+
}
25+
switch (kernel.name) {
2326
case 'gaussian':
24-
this._s = 1
27+
this._s = kernel.s ?? 1
2528
this._kernel = (a, b) =>
2629
Math.exp(-(a.reduce((s, v, i) => s + (v - b[i]) ** 2, 0) ** 2) / this._s ** 2)
2730
break
2831
case 'polynomial':
29-
this._d = 2
32+
this._d = kernel.d ?? 2
3033
this._kernel = (a, b) => (1 + a.reduce((s, v, i) => s + v * b[i])) ** this._d
3134
break
3235
}

lib/model/bsgd.js

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export default class BSGD {
1111
* @param {number} [eta=1] Learning rate
1212
* @param {number} [lambda=1] Regularization parameter
1313
* @param {'removal' | 'projection' | 'merging'} [maintenance=removal] Maintenance type
14-
* @param {'gaussian' | 'polynomial' | function (number[], number[]): number} [kernel=gaussian] Kernel name
14+
* @param {'gaussian' | 'polynomial' | { name: 'gaussian', s?: number } | { name: 'polynomial', d?: number } | function (number[], number[]): number} [kernel=gaussian] Kernel name
1515
*/
1616
constructor(b = 10, eta = 1, lambda = 1, maintenance = 'removal', kernel = 'gaussian') {
1717
this._b = b
@@ -21,14 +21,17 @@ export default class BSGD {
2121
if (typeof kernel === 'function') {
2222
this._kernel = kernel
2323
} else {
24-
switch (kernel) {
24+
if (typeof kernel === 'string') {
25+
kernel = { name: kernel }
26+
}
27+
switch (kernel.name) {
2528
case 'gaussian':
26-
this._s = 1
29+
this._s = kernel.s ?? 1
2730
this._kernel = (a, b) =>
2831
Math.exp(-(a.reduce((s, v, i) => s + (v - b[i]) ** 2, 0) ** 2) / this._s ** 2)
2932
break
3033
case 'polynomial':
31-
this._d = 2
34+
this._d = kernel.d ?? 2
3235
this._kernel = (a, b) => (1 + a.reduce((s, v, i) => s + v * b[i])) ** this._d
3336
break
3437
}
@@ -195,7 +198,7 @@ export class MulticlassBSGD {
195198
* @param {number} [eta=1] Learning rate
196199
* @param {number} [lambda=1] Regularization parameter
197200
* @param {'removal' | 'projection' | 'merging'} [maintenance=removal] Maintenance type
198-
* @param {'gaussian' | 'polynomial' | function (number[], number[]): number} [kernel=gaussian] Kernel name
201+
* @param {'gaussian' | 'polynomial' | { name: 'gaussian', s?: number } | { name: 'polynomial', d?: number } | function (number[], number[]): number} [kernel=gaussian] Kernel name
199202
*/
200203
constructor(b = 10, eta = 1, lambda = 1, maintenance = 'removal', kernel = 'gaussian') {
201204
this._b = b
@@ -205,14 +208,17 @@ export class MulticlassBSGD {
205208
if (typeof kernel === 'function') {
206209
this._kernel = kernel
207210
} else {
208-
switch (kernel) {
211+
if (typeof kernel === 'string') {
212+
kernel = { name: kernel }
213+
}
214+
switch (kernel.name) {
209215
case 'gaussian':
210-
this._s = 1
216+
this._s = kernel.s ?? 1
211217
this._kernel = (a, b) =>
212218
Math.exp(-(a.reduce((s, v, i) => s + (v - b[i]) ** 2, 0) ** 2) / this._s ** 2)
213219
break
214220
case 'polynomial':
215-
this._d = 2
221+
this._d = kernel.d ?? 2
216222
this._kernel = (a, b) => (1 + a.reduce((s, v, i) => s + v * b[i])) ** this._d
217223
break
218224
}

0 commit comments

Comments
 (0)