ml-fnn
Advanced tools
Comparing version 0.0.3 to 1.0.0
{ | ||
"name": "ml-fnn", | ||
"version": "0.0.3", | ||
"version": "1.0.0", | ||
"description": "feedforward neural networks library", | ||
@@ -35,15 +35,6 @@ "main": "src/index.js", | ||
"devDependencies": { | ||
"browserify": "^8.1.3", | ||
"del": "^1.1.1", | ||
"gulp": "^3.8.11", | ||
"gulp-header": "^1.2.2", | ||
"gulp-rename": "^1.2.0", | ||
"gulp-sourcemaps": "^1.3.0", | ||
"gulp-uglify": "^1.1.0", | ||
"mocha": "latest", | ||
"mocha-better-spec-reporter": "latest", | ||
"should": "latest", | ||
"vinyl-buffer": "^1.0.0", | ||
"vinyl-source-stream": "^1.0.0" | ||
"should": "latest" | ||
} | ||
} |
# Feedforward Neural Network | ||
[![NPM version][npm-image]][npm-url] | ||
[![build status][travis-image]][travis-url] | ||
[![David deps][david-image]][david-url] | ||
[![npm download][download-image]][download-url] | ||
A implementation of feedforward neural networks in javascript based on the mrbo answer | ||
@@ -19,3 +24,3 @@ found here: | ||
```js | ||
var FNN = new FeedforwardNeuralNetwork([2, 4, 1]); | ||
var fnn = new FeedforwardNeuralNetwork(); | ||
``` | ||
@@ -32,2 +37,7 @@ | ||
* `predictions` - A matrix of predictions with the same size of rows of the trainingSet. | ||
* `options` - A Javascript object with the configuration of the FNN. | ||
__Options__ | ||
* `hiddenLayers` - Array with the size of each hidden layer in the FNN. | ||
* `iterations` - Maximum number of iterations of the algorithm. | ||
@@ -42,4 +52,10 @@ * `learningRate` - The learning rate (number). | ||
var predictions = [[0], [0], [0], [1]]; | ||
var options = { | ||
hiddenLayers: [4], | ||
iterations: 100, | ||
learningRate: 0.3, | ||
momentum: 0.3 | ||
}; | ||
FNN.train(trainingSet, predictions, 0.3, 0.3); | ||
fnn.train(trainingSet, predictions, options); | ||
``` | ||
@@ -60,3 +76,3 @@ | ||
var ans = FNN.predict(dataset); | ||
var ans = fnn.predict(dataset); | ||
``` | ||
@@ -82,2 +98,11 @@ | ||
[MIT](./LICENSE) | ||
[MIT](./LICENSE) | ||
[npm-image]: https://img.shields.io/npm/v/ml-fnn.svg?style=flat-square | ||
[npm-url]: https://npmjs.org/package/ml-fnn | ||
[travis-image]: https://img.shields.io/travis/mljs/feedforward-neural-networks/master.svg?style=flat-square | ||
[travis-url]: https://travis-ci.org/mljs/feedforward-neural-networks | ||
[david-image]: https://img.shields.io/david/mljs/feedforward-neural-networks.svg?style=flat-square | ||
[david-url]: https://david-dm.org/mljs/feedforward-neural-networks | ||
[download-image]: https://img.shields.io/npm/dm/ml-fnn.svg?style=flat-square | ||
[download-url]: https://npmjs.org/package/ml-fnn |
@@ -23,3 +23,2 @@ "use strict"; | ||
* | ||
* @param {Array} layersSize - Array of sizes of each layer. | ||
* @param reload - for load purposes. | ||
@@ -29,3 +28,3 @@ * @param model - for load purposes. | ||
*/ | ||
function FeedforwardNeuralNetwork(layersSize, reload, model) { | ||
function FeedforwardNeuralNetwork(reload, model) { | ||
if(reload) { | ||
@@ -35,18 +34,23 @@ this.layers = model.layers; | ||
this.outputSize = model.outputSize; | ||
} else { | ||
this.inputSize = layersSize[0]; | ||
this.outputSize = layersSize[layersSize.length - 1]; | ||
layersSize.shift(); | ||
} | ||
} | ||
this.layers = new Array(layersSize.length); | ||
/** | ||
* Build the Neural Network with an array that represent each hidden layer size. | ||
* | ||
* @param {Array} layersSize - Array of sizes of each layer. | ||
*/ | ||
FeedforwardNeuralNetwork.prototype.buildNetwork = function (layersSize) { | ||
layersSize.push(this.outputSize); | ||
for (var i = 0; i < layersSize.length; ++i) { | ||
var inSize = (i == 0) ? this.inputSize : layersSize[i - 1]; | ||
this.layers[i] = new Layer(inSize, layersSize[i]); | ||
} | ||
this.layers = new Array(layersSize.length); | ||
this.layers[this.layers.length - 1].isSigmoid = false; | ||
for (var i = 0; i < layersSize.length; ++i) { | ||
var inSize = (i == 0) ? this.inputSize : layersSize[i - 1]; | ||
this.layers[i] = new Layer(inSize, layersSize[i]); | ||
} | ||
} | ||
this.layers[this.layers.length - 1].isSigmoid = false; | ||
}; | ||
/** | ||
@@ -98,21 +102,32 @@ * Function that applies a forward propagation over the Neural Network | ||
* Method that train the neural network with a given training set with corresponding | ||
* predictions, the number of iterations that we want to perform, the learning rate | ||
* and the momentum that is the regularization term for the parameters of each | ||
* perceptron in the Neural Network. | ||
* predictions. The options argument has an array of the number of perceptrons that we want in each hidden layer, the | ||
* number of iterations (default 50) that we want to perform, the learning rate and the momentum that is the | ||
* regularization term (default 0.1 for both) for the parameters of each perceptron in the Neural Network. | ||
* | ||
* options: | ||
* * hiddenLayers - Array of number with each hidden layer size. | ||
* * iterations - Number | ||
* * learningRate - Number | ||
* * momentum - Number | ||
* | ||
* @param {Matrix} trainingSet | ||
* @param {Matrix} predictions | ||
* @param {Number} iterations | ||
* @param {Number} learningRate | ||
* @param {Number} momentum | ||
* @param {Number} options | ||
*/ | ||
FeedforwardNeuralNetwork.prototype.train = function (trainingSet, predictions, iterations, learningRate, momentum) { | ||
FeedforwardNeuralNetwork.prototype.train = function (trainingSet, predictions, options) { | ||
if(options === undefined) options = {}; | ||
if(trainingSet.length !== predictions.length) | ||
throw new RangeError("the training and prediction set must have the same size."); | ||
if(trainingSet[0].length !== this.inputSize) | ||
throw new RangeError("The training set columns must have the same size of the " + | ||
"input layer"); | ||
if(predictions[0].length !== this.outputSize) | ||
throw new RangeError("The prediction set columns must have the same size of the " + | ||
"output layer"); | ||
this.inputSize = trainingSet[0].length; | ||
this.outputSize = predictions[0].length; | ||
var hiddenLayers = options.hiddenLayers === undefined ? [10] : options.hiddenLayers; | ||
var iterations = options.iterations === undefined ? 50 : options.iterations; | ||
var learningRate = options.learningRate === undefined ? 0.1 : options.learningRate; | ||
var momentum = options.momentum === undefined ? 0.1 : options.momentum; | ||
this.buildNetwork(options.hiddenLayers); | ||
for(var i = 0; i < iterations; ++i) { | ||
@@ -153,3 +168,3 @@ for(var j = 0; j < predictions.length; ++j) { | ||
return new FeedforwardNeuralNetwork(null, true, model); | ||
return new FeedforwardNeuralNetwork(true, model); | ||
}; | ||
@@ -156,0 +171,0 @@ |
@@ -8,8 +8,18 @@ "use strict"; | ||
/** | ||
* Function that create a random array of numbers between -2 to 2. | ||
* Function that create a random array of numbers between value depending | ||
* on the input and output size given the following formula: | ||
* | ||
* sqrt(6) / sqrt(l_in + l_out); | ||
* | ||
* Taken from the coursera course of machine learning from Andrew Ng, | ||
* Exercise 4, Page 7 of the exercise PDF. | ||
* | ||
* @param numberOfWeights - size of the array. | ||
* @param inputSize - number of input of the current layer | ||
* @param outputSize - number of output of the current layer | ||
* @returns {Array} random array of numbers. | ||
*/ | ||
function randomInitialzeWeights(numberOfWeights) { | ||
return Matrix.rand(1, numberOfWeights).sub(0.5).mul(4).getRow(0); | ||
function randomInitialzeWeights(numberOfWeights, inputSize, outputSize) { | ||
var epsilon = 2.449489742783 / Math.sqrt(inputSize + outputSize); | ||
return Matrix.rand(1, numberOfWeights).mul(2 * epsilon).sub(epsilon).getRow(0); | ||
} | ||
@@ -46,3 +56,3 @@ | ||
this.deltaWeights = Matrix.zeros(1, (1 + inputSize) * outputSize).getRow(0); | ||
this.weights = randomInitialzeWeights(this.deltaWeights.length); | ||
this.weights = randomInitialzeWeights(this.deltaWeights.length, inputSize, outputSize); | ||
this.isSigmoid = true; | ||
@@ -49,0 +59,0 @@ } |
@@ -11,4 +11,11 @@ "use strict"; | ||
var xorNN = new FeedforwardNeuralNetwork([2, 4, 1]); | ||
xorNN.train(trainingSet, predictions, 500, 0.3, 0.3); | ||
var xorNN = new FeedforwardNeuralNetwork(); | ||
var options = { | ||
hiddenLayers: [4], | ||
iterations: 500, | ||
learningRate : 0.3, | ||
momentum: 0.3 | ||
}; | ||
xorNN.train(trainingSet, predictions, options); | ||
var results = xorNN.predict(trainingSet); | ||
@@ -26,4 +33,10 @@ | ||
var andNN = new FeedforwardNeuralNetwork([2, 3, 2]); | ||
andNN.train(trainingSet, predictions, 500, 0.3, 0.3); | ||
var andNN = new FeedforwardNeuralNetwork(); | ||
var options = { | ||
hiddenLayers: [3], | ||
iterations: 500, | ||
learningRate : 0.3, | ||
momentum: 0.3 | ||
}; | ||
andNN.train(trainingSet, predictions, options); | ||
@@ -42,4 +55,10 @@ var results = andNN.predict(trainingSet); | ||
var orNN = new FeedforwardNeuralNetwork([2, 4, 1]); | ||
orNN.train(trainingSet, predictions, 500, 0.3, 0.3); | ||
var orNN = new FeedforwardNeuralNetwork(); | ||
var options = { | ||
hiddenLayers: [4], | ||
iterations: 500, | ||
learningRate : 0.3, | ||
momentum: 0.3 | ||
}; | ||
orNN.train(trainingSet, predictions, options); | ||
@@ -61,4 +80,10 @@ var model = orNN.export(); | ||
var nn = new FeedforwardNeuralNetwork([2, 4, 1]); | ||
nn.train(trainingSet, predictions, 300, 0.5, 0.1); | ||
var nn = new FeedforwardNeuralNetwork(); | ||
var options = { | ||
hiddenLayers: [4], | ||
iterations: 300, | ||
learningRate : 0.5, | ||
momentum: 0.1 | ||
}; | ||
nn.train(trainingSet, predictions, options); | ||
@@ -79,4 +104,10 @@ var result = nn.predict(trainingSet); | ||
var nn = new FeedforwardNeuralNetwork([2, 10, 2]); | ||
nn.train(trainingSet, predictions, 200, 0.1, 0.1); | ||
var nn = new FeedforwardNeuralNetwork(); | ||
var options = { | ||
hiddenLayers: [10], | ||
iterations: 200, | ||
learningRate : 0.1, | ||
momentum: 0.1 | ||
}; | ||
nn.train(trainingSet, predictions, options); | ||
@@ -83,0 +114,0 @@ var result = nn.predict([[5, 4]]); |
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
18990
3
363
0
103