A flexible neural network library for Node.js and the browser. Check out a live demo of a movie recommendation engine built with Mind.
- Vectorized - uses a matrix implementation to process training data
- Configurable - allows you to customize the network topology
- Pluggable - download/upload minds that have already learned
$ npm install node-mind
You can use Mind in the browser by requiring it with Duo or Browserify. Or you can simply use the prebuilt root index.js
file directly, which will expose Mind
on the window
object.
var Mind = require('node-mind');
/**
* Letters.
*
* - Imagine these # and . represent black and white pixels.
*/
var a = character(
'.#####.' +
'#.....#' +
'#.....#' +
'#######' +
'#.....#' +
'#.....#' +
'#.....#'
);
var b = character(
'######.' +
'#.....#' +
'#.....#' +
'######.' +
'#.....#' +
'#.....#' +
'######.'
);
var c = character(
'#######' +
'#......' +
'#......' +
'#......' +
'#......' +
'#......' +
'#######'
);
/**
* Learn the letters A through C.
*/
var mind = Mind()
.learn([
{ input: a, output: map('a') },
{ input: b, output: map('b') },
{ input: c, output: map('c') }
]);
/**
* Predict the letter C, even with a pixel off.
*/
var result = mind.predict(character(
'#######' +
'#......' +
'#......' +
'#......' +
'#......' +
'##.....' +
'#######'
));
console.log(result); // ~ 0.5
/**
* Turn the # into 1s and . into 0s.
*/
function character(string) {
return string
.trim()
.split('')
.map(integer);
function integer(symbol) {
if ('#' === symbol) return 1;
if ('.' === symbol) return 0;
}
}
/**
* Map letter to a number.
*/
function map(letter) {
if (letter === 'a') return [ 0.1 ];
if (letter === 'b') return [ 0.3 ];
if (letter === 'c') return [ 0.5 ];
return 0;
}
Use plugins created by the Mind community to configure pre-trained networks that can go straight to making predictions.
Here's a cool example of the way you could use a hypothetical mind-ocr
plugin:
var Mind = require('node-mind');
var ocr = require('mind-ocr');
var mind = Mind()
.upload(ocr)
.predict(
'.#####.' +
'#.....#' +
'#.....#' +
'#######' +
'#.....#' +
'#.....#' +
'#.....#'
);
To create a plugin, simply call download
on your trained mind:
var Mind = require('node-mind');
var mind = Mind()
.learn([
{ input: [0, 0], output: [ 0 ] },
{ input: [0, 1], output: [ 1 ] },
{ input: [1, 0], output: [ 1 ] },
{ input: [1, 1], output: [ 0 ] }
]);
var xor = mind.download();
Here's a list of available plugins:
Create a new instance of Mind that can learn to make predictions.
The available options are:
learningRate
: how quickly the network should learn.hiddenUnits
: how many neurons are in the hidden layer.activator
: which activation function to use,sigmoid
orhtan
.iterations
: the number of iterations to run.
Learn from training data:
mind.learn([
{ input: [0, 0], output: [ 0 ] },
{ input: [0, 1], output: [ 1 ] },
{ input: [1, 0], output: [ 1 ] },
{ input: [1, 1], output: [ 0 ] }
]);
Make a prediction:
mind.predict([0, 1]);
Download a mind:
var xor = mind.download();
Upload a mind:
mind.upload(xor);
Listen for the 'data' event, which is fired with each iteration:
mind.on('data', function(iteration, errors, results) {
// ...
});
If you're interested in learning more about neural networks, you'll definitely want to check out these fantastic libraries: