libsvm-wasm
Advanced tools
Comparing version 0.0.7 to 0.0.8
@@ -41,12 +41,13 @@ declare enum SVM_TYPE { | ||
private modelPointer; | ||
private ready; | ||
constructor(param?: SVMParam); | ||
init: () => Promise<void>; | ||
checkInitialization: () => void; | ||
train: () => Promise<void>; | ||
predict: (data: Array<number>) => Promise<number>; | ||
feedSamples: (data: Array<Array<number>>, labels: Array<number>) => Promise<void>; | ||
feedParam: () => Promise<void>; | ||
save: (name: string) => Promise<boolean>; | ||
load: (name: string) => Promise<boolean>; | ||
predict: (data: Array<number>) => number; | ||
feedSamples: (data: Array<Array<number>>, labels: Array<number>) => void; | ||
feedParam: () => void; | ||
save: (name: string) => boolean; | ||
load: (name: string) => boolean; | ||
evaluate: (label: Array<number>, pred: Array<number>) => Array<string>; | ||
} | ||
export { SVM, ISVMParam, SVM_TYPE, KERNEL_TYPE, SVMParam }; |
@@ -116,14 +116,28 @@ "use strict"; | ||
this.modelPointer = -1; | ||
this.init = function () { return __awaiter(_this, void 0, void 0, function () { | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, (0, libsvm_1.default)()]; | ||
case 1: | ||
libsvm = _a.sent(); | ||
return [2 /*return*/]; | ||
} | ||
}); | ||
}); }; | ||
this.checkInitialization = function () { | ||
if (!libsvm) { | ||
throw new Error("\n In order to use this SVM class, you'll need to initialize the svm (which grabs the WASM module and loads it asynchronously).\n Here's some example code.\n\n let svm = new SVM()\n await svm.init()\n svm.train(data)\n "); | ||
} | ||
}; | ||
this.train = function () { return __awaiter(_this, void 0, void 0, function () { | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, this.ready]; | ||
case 0: | ||
this.checkInitialization(); | ||
if (!(this.paramPointer == -1)) return [3 /*break*/, 2]; | ||
return [4 /*yield*/, this.feedParam()]; | ||
case 1: | ||
_a.sent(); | ||
if (!(this.paramPointer == -1)) return [3 /*break*/, 3]; | ||
return [4 /*yield*/, this.feedParam()]; | ||
_a.label = 2; | ||
case 2: | ||
_a.sent(); | ||
_a.label = 3; | ||
case 3: | ||
if (this.paramPointer == -1 || this.samplesPointer == -1) | ||
@@ -138,96 +152,59 @@ return [2 /*return*/]; | ||
}); }; | ||
this.predict = function (data) { return __awaiter(_this, void 0, void 0, function () { | ||
var dataPtr; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, this.ready]; | ||
case 1: | ||
_a.sent(); | ||
if (this.modelPointer == null) { | ||
console.error("Model should be trained first"); | ||
return [2 /*return*/, -1]; | ||
} | ||
dataPtr = libsvm._malloc(data.length * 8); | ||
libsvm.HEAPF64.set(data, dataPtr / 8); | ||
return [2 /*return*/, libsvm._predict_one(this.modelPointer, dataPtr, data.length)]; | ||
} | ||
}); | ||
}); }; | ||
this.feedSamples = function (data, labels) { return __awaiter(_this, void 0, void 0, function () { | ||
var encodeData, dataPtr, encodeLabelData, labelPtr; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, this.ready]; | ||
case 1: | ||
_a.sent(); | ||
if (this.samplesPointer == null) | ||
libsvm._free_sample(this.samplesPointer); | ||
encodeData = new Float64Array(data.reduce(function (prev, curr) { return prev.concat(curr); }, [])); | ||
dataPtr = libsvm._malloc(encodeData.length * 8); | ||
libsvm.HEAPF64.set(encodeData, dataPtr / 8); | ||
encodeLabelData = new Float64Array(labels); | ||
labelPtr = libsvm._malloc(encodeLabelData.length * 8); | ||
libsvm.HEAPF64.set(encodeLabelData, labelPtr / 8); | ||
this.samplesPointer = libsvm._make_samples(dataPtr, labelPtr, data.length, data[0].length); | ||
return [2 /*return*/]; | ||
} | ||
}); | ||
}); }; | ||
this.feedParam = function () { return __awaiter(_this, void 0, void 0, function () { | ||
var _a, svm_type, kernel_type, degree, gamma, coef0, nu, cache_size, C, p, shrinking, probability, nr_weight, weight_label, weight, eps, weightLabelPtr, weightlPtr; | ||
return __generator(this, function (_b) { | ||
switch (_b.label) { | ||
case 0: return [4 /*yield*/, this.ready]; | ||
case 1: | ||
_b.sent(); | ||
_a = this.param.param, svm_type = _a.svm_type, kernel_type = _a.kernel_type, degree = _a.degree, gamma = _a.gamma, coef0 = _a.coef0, nu = _a.nu, cache_size = _a.cache_size, C = _a.C, p = _a.p, shrinking = _a.shrinking, probability = _a.probability, nr_weight = _a.nr_weight, weight_label = _a.weight_label, weight = _a.weight; | ||
eps = this.param.eps; | ||
if (this.paramPointer == null) | ||
libsvm._free(this.paramPointer); | ||
weightLabelPtr = libsvm._malloc(nr_weight * 4); | ||
libsvm.HEAP32.set(new Int32Array(weight_label), weightLabelPtr / 4); | ||
weightlPtr = libsvm._malloc(nr_weight * 8); | ||
libsvm.HEAPF64.set(new Float64Array(weight), weightlPtr / 8); | ||
this.paramPointer = libsvm._make_param(svm_type, kernel_type, degree, gamma, coef0, nu, cache_size, C, eps, p, shrinking, probability, nr_weight, weightLabelPtr, weightlPtr); | ||
return [2 /*return*/]; | ||
} | ||
}); | ||
}); }; | ||
this.save = function (name) { return __awaiter(_this, void 0, void 0, function () { | ||
var buffer; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, this.ready]; | ||
case 1: | ||
_a.sent(); | ||
if (this.modelPointer == null) { | ||
console.error("Model should be trained first"); | ||
return [2 /*return*/, false]; | ||
} | ||
buffer = libsvm._malloc(name.length + 1); | ||
// Write the string to memory | ||
libsvm.stringToUTF8(name, buffer, name.length + 1); | ||
return [2 /*return*/, libsvm._save_model(this.modelPointer, buffer)]; | ||
} | ||
}); | ||
}); }; | ||
this.load = function (name) { return __awaiter(_this, void 0, void 0, function () { | ||
var buffer; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, this.ready]; | ||
case 1: | ||
_a.sent(); | ||
if (this.modelPointer == null) | ||
libsvm._free_model(this.modelPointer); | ||
buffer = libsvm._malloc(name.length + 1); | ||
libsvm.stringToUTF8(name, buffer, name.length + 1); | ||
this.modelPointer = libsvm._load_model(buffer); | ||
if (this.modelPointer != null || this.modelPointer > 0) { | ||
return [2 /*return*/, true]; | ||
} | ||
return [2 /*return*/, false]; | ||
} | ||
}); | ||
}); }; | ||
this.predict = function (data) { | ||
_this.checkInitialization(); | ||
if (_this.modelPointer == null) { | ||
console.error("Model should be trained first"); | ||
return -1; | ||
} | ||
var dataPtr = libsvm._malloc(data.length * 8); | ||
libsvm.HEAPF64.set(data, dataPtr / 8); | ||
return libsvm._predict_one(_this.modelPointer, dataPtr, data.length); | ||
}; | ||
this.feedSamples = function (data, labels) { | ||
_this.checkInitialization(); | ||
if (_this.samplesPointer == null) | ||
libsvm._free_sample(_this.samplesPointer); | ||
var encodeData = new Float64Array(data.reduce(function (prev, curr) { return prev.concat(curr); }, [])); | ||
var dataPtr = libsvm._malloc(encodeData.length * 8); | ||
libsvm.HEAPF64.set(encodeData, dataPtr / 8); | ||
var encodeLabelData = new Float64Array(labels); | ||
var labelPtr = libsvm._malloc(encodeLabelData.length * 8); | ||
libsvm.HEAPF64.set(encodeLabelData, labelPtr / 8); | ||
_this.samplesPointer = libsvm._make_samples(dataPtr, labelPtr, data.length, data[0].length); | ||
}; | ||
this.feedParam = function () { | ||
_this.checkInitialization(); | ||
var _a = _this.param.param, svm_type = _a.svm_type, kernel_type = _a.kernel_type, degree = _a.degree, gamma = _a.gamma, coef0 = _a.coef0, nu = _a.nu, cache_size = _a.cache_size, C = _a.C, p = _a.p, shrinking = _a.shrinking, probability = _a.probability, nr_weight = _a.nr_weight, weight_label = _a.weight_label, weight = _a.weight; | ||
var eps = _this.param.eps; | ||
if (_this.paramPointer == null) | ||
libsvm._free(_this.paramPointer); | ||
var weightLabelPtr = libsvm._malloc(nr_weight * 4); | ||
libsvm.HEAP32.set(new Int32Array(weight_label), weightLabelPtr / 4); | ||
var weightlPtr = libsvm._malloc(nr_weight * 8); | ||
libsvm.HEAPF64.set(new Float64Array(weight), weightlPtr / 8); | ||
_this.paramPointer = libsvm._make_param(svm_type, kernel_type, degree, gamma, coef0, nu, cache_size, C, eps, p, shrinking, probability, nr_weight, weightLabelPtr, weightlPtr); | ||
}; | ||
this.save = function (name) { | ||
_this.checkInitialization(); | ||
if (_this.modelPointer == null) { | ||
console.error("Model should be trained first"); | ||
return false; | ||
} | ||
var buffer = libsvm._malloc(name.length + 1); | ||
// Write the string to memory | ||
libsvm.stringToUTF8(name, buffer, name.length + 1); | ||
return libsvm._save_model(_this.modelPointer, buffer); | ||
}; | ||
this.load = function (name) { | ||
_this.checkInitialization(); | ||
if (_this.modelPointer == null) | ||
libsvm._free_model(_this.modelPointer); | ||
var buffer = libsvm._malloc(name.length + 1); | ||
libsvm.stringToUTF8(name, buffer, name.length + 1); | ||
_this.modelPointer = libsvm._load_model(buffer); | ||
if (_this.modelPointer != null || _this.modelPointer > 0) { | ||
return true; | ||
} | ||
return false; | ||
}; | ||
this.evaluate = function (label, pred) { | ||
@@ -270,8 +247,2 @@ //from the python implementation libsvm/python/commonutil.py | ||
}; | ||
this.ready = new Promise(function (resolve, reject) { | ||
(0, libsvm_1.default)().then(function (lib) { | ||
libsvm = lib; | ||
resolve(); | ||
}); | ||
}); | ||
if (!param) | ||
@@ -278,0 +249,0 @@ this.param = new SVMParam({}); |
@@ -1,2 +0,2 @@ | ||
import { SVM, ISVMParam, SVM_TYPE, KERNEL_TYPE, SVMParam } from '../src/'; | ||
import { SVM } from '../src/'; | ||
import { readFileSync } from 'fs'; | ||
@@ -13,2 +13,3 @@ import { join } from 'path'; | ||
const label = rawData.map(it => parseInt(it[0])); | ||
@@ -18,36 +19,19 @@ const data = rawData.map(it => it.slice(1).map(it => parseFloat(it))); | ||
async function ml(data: any, label: any){ | ||
const param: ISVMParam = { | ||
svm_type: SVM_TYPE.EPSILON_SVR, | ||
kernel_type: KERNEL_TYPE.RBF, | ||
degree: 3, | ||
gamma: 1/(2*6.75), | ||
coef0: 0, | ||
cache_size: 100, | ||
C: 1, | ||
nr_weight: 0, | ||
weight_label: [], | ||
weight: [], | ||
nu: 0.5, | ||
p: 0.1, | ||
shrinking: 1, | ||
probability: 0 | ||
}; | ||
const aParam: SVMParam = new SVMParam(param, 1e-3); | ||
const svm = new SVM(aParam); | ||
await svm.feedSamples([[1,1,1], [-2,-2,2]], [0, 1]); | ||
const svm = new SVM(); | ||
await svm.init() | ||
svm.feedSamples(data, label); | ||
await svm.train(); | ||
let pred_data: Array<number> = []; | ||
const pred = [[1,1,1], [-2,-2,-2]]; | ||
for(let i in pred){ | ||
let p = await svm.predict(data[i]) | ||
pred_data.push(p); | ||
for(let i in data){ | ||
let pred = svm.predict(data[i]) | ||
pred_data.push(pred); | ||
} | ||
console.log(pred_data); | ||
// const evaluate = svm.evaluate(label, pred_data); | ||
// console.log(evaluate) | ||
const evaluate = svm.evaluate(label, pred_data); | ||
console.log(evaluate) | ||
} | ||
ml(data,label) |
{ | ||
"name": "libsvm-wasm", | ||
"version": "0.0.7", | ||
"version": "0.0.8", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -29,9 +29,9 @@ # libsvm-wasm | ||
const svm = new SVM(); | ||
await svm.init() | ||
const data = [[-1, -1], [1, 1], [2, 2], [-2, -2]] | ||
const label = [-1, 1, 1, -1]; | ||
await svm.feedSamples(data, label); | ||
svm.feedSamples(data, label); | ||
await svm.train(); | ||
await svm.predict([3, 3]); | ||
svm.predict([3, 3]); | ||
``` | ||
@@ -38,0 +38,0 @@ |
@@ -81,11 +81,4 @@ import svmFactory, { LibsvmModlue } from '../dist/libsvm'; | ||
private modelPointer: number = -1; | ||
private ready: Promise<void>; | ||
constructor(param?: SVMParam) { | ||
this.ready = new Promise((resolve, reject) => { | ||
svmFactory().then((lib) => { | ||
libsvm = lib; | ||
resolve(); | ||
}); | ||
}); | ||
if (!param) this.param = new SVMParam({} as ISVMParam); | ||
@@ -95,4 +88,21 @@ else this.param = param; | ||
public init = async () => { | ||
libsvm = await svmFactory() | ||
} | ||
public checkInitialization = () => { | ||
if(!libsvm){ | ||
throw new Error(` | ||
In order to use this SVM class, you'll need to initialize the svm (which grabs the WASM module and loads it asynchronously). | ||
Here's some example code. | ||
let svm = new SVM() | ||
await svm.init() | ||
svm.train(data) | ||
`) | ||
} | ||
} | ||
public train = async () => { | ||
await this.ready; | ||
this.checkInitialization() | ||
if (this.paramPointer == -1) { | ||
@@ -106,4 +116,4 @@ await this.feedParam(); | ||
public predict = async (data: Array<number>): Promise<number> => { | ||
await this.ready; | ||
public predict = (data: Array<number>): number => { | ||
this.checkInitialization() | ||
if (this.modelPointer == null) { | ||
@@ -120,4 +130,4 @@ console.error("Model should be trained first"); | ||
public feedSamples = async (data: Array<Array<number>>, labels: Array<number>) => { | ||
await this.ready; | ||
public feedSamples = (data: Array<Array<number>>, labels: Array<number>) => { | ||
this.checkInitialization() | ||
if (this.samplesPointer == null) libsvm._free_sample(this.samplesPointer); | ||
@@ -137,4 +147,4 @@ | ||
public feedParam = async () => { | ||
await this.ready; | ||
public feedParam = () => { | ||
this.checkInitialization() | ||
const { | ||
@@ -174,4 +184,4 @@ svm_type, | ||
public save = async (name: string): Promise<boolean> => { | ||
await this.ready; | ||
public save = (name: string): boolean => { | ||
this.checkInitialization() | ||
if (this.modelPointer == null) { | ||
@@ -190,4 +200,4 @@ console.error("Model should be trained first"); | ||
public load = async (name: string): Promise<boolean> => { | ||
await this.ready; | ||
public load = (name: string): boolean => { | ||
this.checkInitialization() | ||
if (this.modelPointer == null) libsvm._free_model(this.modelPointer); | ||
@@ -194,0 +204,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
331047
1236