brain.js on Titanium Mobile

brain.js is a Node JS module library serves neural network. It is normally used for pattern recognition, machine learning, forecasting and business intelligence.

Titanium mobile is a native mobile development platform using JavaScript.

Titanium mobile allows to include modules that conform to CommonJS Specification. Therefore, I just wonder if it is possible Titanium mobile run a neural network by including brain.js which is a Node JS module. And then, I managed to include it and have made some test training inputs and get the test outputs.

The exciting part is the training data sets can be shared among server (Node JS server side), and client (Titanium mobile, with iOS, Android), because they both use same CommonJS module brain.js.

Short Notes about including brain.js on Titanium mobile:

1) you use require to include the brain.js module by must provide full absolute path to the main entry js, for example: require('/path/to/module'), with corresponds to /path/to/module.js. you should change all the require portion among all the library codes to use absolute path instead of relative path.

2) try to separate out the callback function after exports.xxx.
For example, exports.xxx = function() {}; doesn’t work, you should write this as:

var callback = function() {};
exports.xxx = callback;

3) sample code to use brain.js:


var brain = require("/path/to/your/brain");

var trainInputs = [];

trainInputs.push({input: [1,0,1,1,1,1,1,0,1], output: [0.08]});
trainInputs.push({input: [1,1,1,0,1,0,1,1,1], output: [0.09]});
trainInputs.push({input: [1,1,1,0,1,0,1,1,0], output: [0.1 ]});
trainInputs.push({input: [1,0,1,1,1,0,1,0,1], output: [0.11]});
trainInputs.push({input: [1,0,0,1,0,0,1,1,1], output: [0.12]});
trainInputs.push({input: [1,0,1,1,1,1,1,0,1], output: [0.13]});
trainInputs.push({input: [1,0,1,1,1,1,1,0,1], output: [0.14]});
trainInputs.push({input: [1,1,1,1,0,1,1,1,1], output: [0.15]});
trainInputs.push({input: [1,1,1,1,1,1,1,0,0], output: [0.16]});

var net = new brain.NeuralNetwork();
net.train(trainInputs);
var input = [1,0,1,1,1,1,1,0,1];
var output = net.run(input);

As you can see the training data sets trainInputs is a JavaScript Array and it can be easily shared to Node JS server side using JSON-serialized format and in Node JS server side you can do the same code which Titanium mobile used also to manipulate the training data sets.

Author: fyhao

Jebsen & Jessen Comms Singapore INTI University College Bsc (Hon) of Computer Science, Coventry University

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.