-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathxor.js
47 lines (42 loc) · 967 Bytes
/
xor.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
'use strict';
function xor(SVM) {
let svm = new SVM({
kernel: SVM.KERNEL_TYPES.RBF,
type: SVM.SVM_TYPES.C_SVC,
gamma: 1,
cost: 1
});
const features = [[0, 0], [1, 1], [1, 0], [0, 1]];
const labels = [0, 0, 1, 1];
svm.train(features, labels);
for (let i = 0; i < features.length; i++) {
const pred = svm.predictOne(features[i]);
console.log(`actual: ${labels[i]}, predicted: ${pred}`);
}
console.log('sv indices', svm.getSVIndices());
console.log('labels', svm.getLabels());
console.log('save model', svm.serializeModel());
}
function execAsm() {
console.log('asm');
const SVM = require('../asm');
xor(SVM);
}
async function execWasm() {
console.log('wasm');
let SVM;
try {
SVM = await require('../wasm');
} catch (e) {
console.log(e);
}
console.log(SVM);
xor(SVM);
}
try {
execAsm(); // Synchronous
execWasm(); // Asynchronous
} catch (e) {
console.log('failed');
console.log(e);
}