mljs / libsvm

LIBSVM for the browser and nodejs :fire:
https://mljs.github.io/libsvm/
BSD 3-Clause "New" or "Revised" License
82 stars 14 forks source link

FS is not defined inside src/loadSVM.js #10

Closed nopbyte closed 6 years ago

nopbyte commented 6 years ago

Hi,

first of all. Congrats! This tool seems to be really useful. I have a problem with many features and I tried another svm library before (https://github.com/karpathy/svmjs). libsvm is performing much better, as expected.

I am having an issue when I try to get the result of serlializeModel to port my model from the server to the browser since I only want to do training once. Here is a minimal example that reproduces the error:

var data = [
[1,2,3,4,5],
[11,21,31,41,51],
[21,22,23,24,25],
[31,32,33,34,35]
]
var labels = [1,2,2,1]
require('libsvm-js').then(SVM => {
    var options = {kernel: SVM.KERNEL_TYPES.RBF, // The type of kernel I want to use
          type: SVM.SVM_TYPES.C_SVC,    // The type of SVM I want to run
          cost: 1000,
          gamma: 0.001
    }
    const svm = new SVM(options); // ...
    svm.train(data, labels);
    console.log(JSON.stringify(data))
    console.log(JSON.stringify(svm.serializeModel()))
}).catch((e)=>{
  console.log(e);
});

The output I get from the algorithm is the following:

optimization finished, #iter = 2
nu = 1.000000
obj = -2.000000, rho = 0.000000
nSV = 4, nBSV = 4
Total nSV = 4
[[1,2,3,4,5],[11,21,31,41,51],[21,22,23,24,25],[31,32,33,34,35]]
ReferenceError: FS is not defined
    at ___syscall5 (/tmp/libsvm/node_modules/libsvm-js/dist/wasm/libsvm.js:1:38023)
    at  (<WASM>[192]+87)
    at  (<WASM>[101]+24)
    at  (<WASM>[36]+10)
    at Module._serialize_model (/tmp/libsvm/node_modules/libsvm-js/dist/wasm/libsvm.js:1:49301)
    at SVM.serializeModel (/tmp/libsvm/node_modules/libsvm-js/src/loadSVM.js:160:28)
    at require.then.SVM (/tmp/libsvm/index.js:20:36)
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:169:7)
    at Function.Module.runMain (module.js:607:11)
ReferenceError: FS is not defined
    at ___syscall5 (/tmp/libsvm/node_modules/libsvm-js/dist/wasm/libsvm.js:1:38023)
    at  (<WASM>[192]+87)
    at  (<WASM>[101]+24)
    at  (<WASM>[36]+10)
    at Module._serialize_model (/tmp/libsvm/node_modules/libsvm-js/dist/wasm/libsvm.js:1:49301)
    at SVM.serializeModel (/tmp/libsvm/node_modules/libsvm-js/src/loadSVM.js:160:28)
    at require.then.SVM (/tmp/libsvm/index.js:20:36)
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:169:7)
    at Function.Module.runMain (module.js:607:11)
abort({}) at Error
    at jsStackTrace (/tmp/libsvm/node_modules/libsvm-js/dist/wasm/libsvm.js:1:18351)
    at stackTrace (/tmp/libsvm/node_modules/libsvm-js/dist/wasm/libsvm.js:1:18522)
    at abort (/tmp/libsvm/node_modules/libsvm-js/dist/wasm/libsvm.js:1:57513)
    at ___syscall5 (/tmp/libsvm/node_modules/libsvm-js/dist/wasm/libsvm.js:1:38133)
    at  (<WASM>[192]+87)
    at  (<WASM>[101]+24)
    at  (<WASM>[36]+10)
    at Module._serialize_model (/tmp/libsvm/node_modules/libsvm-js/dist/wasm/libsvm.js:1:49301)
    at SVM.serializeModel (/tmp/libsvm/node_modules/libsvm-js/src/loadSVM.js:160:28)
    at require.then.SVM (/tmp/libsvm/index.js:20:36)
If this abort() is unexpected, build with -s ASSERTIONS=1 which can give more information.

There seems to be an issue with fs somewhere?

I am using Node v8.1.2 with NVM. Which is the same node version you have in your README file.

Thanks a lot for your help,

stropitek commented 6 years ago

Hello

Thanks for the feedback!

I probably did not build the wasm module with the right options, forgetting to include the virtual file system. I pretty sure this scenario works with the asm version though. Could you test that?

If you data is big you could run into some known issues with the FS (https://github.com/mljs/libsvm/issues/6) . Using a precomputed kernel will avoid that. Here is an example of using a precomputed kernel with libsvm-js: https://github.com/jajoe/tutorial-nodejs/blob/4931ad30b282416f174951f59971dae9e409c426/image-classification/index.js

I'll have a look at the FS issue later this week, probably during the week-end.

nopbyte commented 6 years ago

Hi @stropitek I tested with the asm version and it works. I get a string back without any problems. I will look into the precomputed kernel example and try this out. Thanks a lot for your pointers.

stropitek commented 6 years ago

@nopbyte I released v0.1.3 with the fix