From 2dbad0191415a58e8bbed41c6a218cbbc747e0fc Mon Sep 17 00:00:00 2001 From: Will Newmarch Date: Sun, 17 Mar 2019 14:35:54 +0000 Subject: [PATCH 1/7] Added chaining ability to Network methods --- src/Network.js | 6 ++++++ xor-problem.test.js | 12 +++++------- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/Network.js b/src/Network.js index e6aa347..3200a38 100644 --- a/src/Network.js +++ b/src/Network.js @@ -98,6 +98,7 @@ class Network { /** * Fire the input layer's Neurons with supplied array of floats * @param {array} signals + * @returns Network (for chaining purposes) */ fire(signals) { for (var i = 0; i < this.layers[0].neurons.length; i++) { @@ -114,6 +115,7 @@ class Network { /** * Initialise back propagation through network with supplied array of floats * @param {array} errors + * @returns Network (for chaining purposes) */ backPropagate(errors) { for (var i = 0; i < errors.length; i++) { @@ -125,6 +127,7 @@ class Network { /** * Trigger each synapse to apply its error to its weight * @param {float} learningRate + * @returns Network (for chaining purposes) */ applyError(learningRate) { this.layers.map(l => { @@ -136,6 +139,7 @@ class Network { } }); }); + return this; } /** @@ -153,6 +157,7 @@ class Network { /** * Reset all the Neurons and Synapses back to their initial state + * @returns Network (for chaining purposes) */ reset() { this.layers.map(l => { @@ -165,6 +170,7 @@ class Network { } }); }); + return this; } /** diff --git a/xor-problem.test.js b/xor-problem.test.js index 223f764..f24c2b7 100644 --- a/xor-problem.test.js +++ b/xor-problem.test.js @@ -30,13 +30,11 @@ test('library solves XOR problem', () => { let index = Math.floor(Math.random() * data.length); - network.fire(data[index].x); - - network.backPropagate(data[index].y); - - network.applyError(learningRate); - - network.reset(); + network + .fire(data[index].x) + .backPropagate(data[index].y) + .applyError(learningRate) + .reset(); } } From 51c8a97f52ee5156079482fcc20592de17fa51c3 Mon Sep 17 00:00:00 2001 From: Will Newmarch Date: Sun, 17 Mar 2019 18:52:47 +0000 Subject: [PATCH 2/7] Added .idea to gitignore --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitignore b/.gitignore index a8ff1e7..a453c27 100644 --- a/.gitignore +++ b/.gitignore @@ -61,3 +61,5 @@ typings/ # next.js build output .next + +.idea \ No newline at end of file From 52d0d2ab6c5f1d7d11cc602af6c50afd4951b67b Mon Sep 17 00:00:00 2001 From: Will Newmarch Date: Sun, 17 Mar 2019 18:53:08 +0000 Subject: [PATCH 3/7] Updated readme with example and information on usage. --- README.md | 123 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 122 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index a7fcc2c..0db6084 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ # Intuitive Neural Network -### When would like to understand what's going on without the mathematics! +### When you would like to understand what's going on without the mathematics! A JavaScript based, object-orientated approach to a Neural Network library. The two aims behind creating this library are: @@ -9,3 +9,124 @@ The two aims behind creating this library are: --- Run the XOR example with `npm test` + +--- + +## A Simple Implementation (XOR Problem) + + // Build the network... + var network = new Network({ + layers: [2,2,1], + bias: false + }); + + // Training data (x in, y out) + var data = [ + {x: [0,0], y: [0]}, + {x: [0,1], y: [1]}, + {x: [1,0], y: [1]}, + {x: [1,1], y: [0]} + ]; + + // Training the network... + var epochs = 10000; + var learningRate = 0.01; + + for (var h = 0; h < epochs; h++) { + + for (var i = 0; i < data.length; i++) { + + let index = Math.floor(Math.random() * data.length); + + network + .fire(data[index].x) + .backPropagate(data[index].y) + .applyError(learningRate) + .reset(); + + } + } + // Done. + + // Testing the trained network... + for(var i = 0; i < data.length; i++) { + + network.fire(data[i].x); + + var activation = network.layers[network.layers.length-1].neurons[0].activation; + + expect(Math.round(activation)).toBe(data[i].y[0]); + + network.reset(); + + } + // Done. + +--- + + + + +## Network +**Kind**: global class + +* [Network](#Network) + * [new Network(settings)](#new_Network_new) + * [.fire(signals)](#Network+fire) ⇒ + * [.backPropagate(errors)](#Network+backPropagate) ⇒ + * [.applyError(learningRate)](#Network+applyError) ⇒ + * [.reset()](#Network+reset) ⇒ + + + +### new Network(settings) +Constructor for Network + + +| Param | Type | +| --- | --- | +| settings | object | + + + +### network.fire(signals) ⇒ +Fire the input layer's Neurons with supplied array of floats + +**Kind**: instance method of [Network](#Network) +**Returns**: Network (for chaining purposes) + +| Param | Type | +| --- | --- | +| signals | array | + + + +### network.backPropagate(errors) ⇒ +Initialise back propagation through network with supplied array of floats + +**Kind**: instance method of [Network](#Network) +**Returns**: Network (for chaining purposes) + +| Param | Type | +| --- | --- | +| errors | array | + + + +### network.applyError(learningRate) ⇒ +Trigger each synapse to apply its error to its weight + +**Kind**: instance method of [Network](#Network) +**Returns**: Network (for chaining purposes) + +| Param | Type | +| --- | --- | +| learningRate | float | + + + +### network.reset() ⇒ +Reset all the Neurons and Synapses back to their initial state + +**Kind**: instance method of [Network](#Network) +**Returns**: Network (for chaining purposes) \ No newline at end of file From 4a72d48de8fb88c132a4c0f904987904923aee5c Mon Sep 17 00:00:00 2001 From: Will Newmarch Date: Sun, 17 Mar 2019 18:53:23 +0000 Subject: [PATCH 4/7] Minor changes to XOR test. --- xor-problem.test.js | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/xor-problem.test.js b/xor-problem.test.js index f24c2b7..7bf1534 100644 --- a/xor-problem.test.js +++ b/xor-problem.test.js @@ -2,16 +2,9 @@ const Network = require('./src/Network.js'); test('library solves XOR problem', () => { - // Various settings... - var learningRate = 0.01; - var epochs = 100000; - var activation = 'sigmoid'; - // Build the network... var network = new Network({ layers: [2,2,1], - hiddenActivationType: activation, - outputActivationType: 'identity', bias: false }); @@ -22,7 +15,9 @@ test('library solves XOR problem', () => { {x: [1,1], y: [0]} ]; - // Training... + // Training the network... + var epochs = 10000; + var learningRate = 0.01; for (var h = 0; h < epochs; h++) { @@ -38,11 +33,9 @@ test('library solves XOR problem', () => { } } - // Done. - // Testing... - + // Testing the trained network... for(var i = 0; i < data.length; i++) { network.fire(data[i].x); @@ -54,7 +47,6 @@ test('library solves XOR problem', () => { network.reset(); } - // Done. }); From bf1d3a71cd21a911a64581ac94bde2f8f8695323 Mon Sep 17 00:00:00 2001 From: Will Newmarch Date: Sun, 17 Mar 2019 18:54:48 +0000 Subject: [PATCH 5/7] Minor amend to readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 0db6084..59872a8 100644 --- a/README.md +++ b/README.md @@ -55,7 +55,7 @@ Run the XOR example with `npm test` var activation = network.layers[network.layers.length-1].neurons[0].activation; - expect(Math.round(activation)).toBe(data[i].y[0]); + // expect Math.round(activation) to equal data[i].y[0] network.reset(); From 4182f66c3b5f9077b592d9d626d3d75d6f2a159a Mon Sep 17 00:00:00 2001 From: Will Newmarch Date: Sun, 17 Mar 2019 19:21:47 +0000 Subject: [PATCH 6/7] Initial commit of travis ci config file --- .travis.yml | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 .travis.yml diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..efb0983 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,3 @@ +language: node_js +node_js: + - "8" From 5329a51e818da47e20d3ec36c0f1a110046d2803 Mon Sep 17 00:00:00 2001 From: Will Newmarch Date: Sun, 17 Mar 2019 19:24:41 +0000 Subject: [PATCH 7/7] Test suite status badge added to readme --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 59872a8..9920b33 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,4 @@ +[![Build Status](https://travis-ci.org/will-newmarch/intuitive-neural-network.svg?branch=master)](https://travis-ci.org/will-newmarch/intuitive-neural-network) # Intuitive Neural Network ### When you would like to understand what's going on without the mathematics! A JavaScript based, object-orientated approach to a Neural Network library.