Skip to content

Commit

Permalink
Merge pull request #11 from will-newmarch/release-0.6.0
Browse files Browse the repository at this point in the history
Release 0.6.0
  • Loading branch information
will-newmarch authored Aug 20, 2019
2 parents 7dfe9ea + 23f877d commit 714bdf7
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 1 deletion.
Binary file added .swp
Binary file not shown.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "intuitive-neural-network",
"version": "0.5.5",
"version": "0.6.0",
"description": "An intuitive, object-orientated approach to a Neural Network library.",
"main": "src/Network.js",
"scripts": {
Expand Down
4 changes: 4 additions & 0 deletions src/Synapse.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ class Synapse {
this.weight -= learningRate * this.error;
}

mapActivation(signal,count) {
this.input.mapActivation(signal * this.weight,count);
}

toObject() {
return {
type: this.constructor.name,
Expand Down
30 changes: 30 additions & 0 deletions src/neuron/Neuron.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,36 @@ class Neuron {
throw 'backPropagate method must be overidden!';
}

/**
* Uses activation maximisation to discover the path of activation
* needed to activate this neuron to the supplied signal
* @param {float} signal
* @param {integer} count
*/
mapActivation(signal,count = null) {
if(!this.hasOwnProperty('mappedSignals')) this.mappedSignals = [];
this.mappedSignals.push(signal);
if(count === null || this.mappedSignals.length === count) {
this.activation = this.mappedSignals.reduce((a,s) => a+s,0);
const expectedCount = count === null ? 1 : this.outputs[0].output.inputs.length;
for(let input of this.inputs) {
input.mapActivation(this.activation,expectedCount);
}
delete this.mappedSignals;
}
}

backPropagate(backSignal) {
this.outputSignals.push(backSignal);
if(this.outputSignals.length == this.outputs.length) {
const signal = this.outputSignals.reduce((a,s) => a+s,0);
this.error = signal + this.derivativeFunc(this.activation);
for (var i = 0; i < this.inputs.length; i++) {
this.inputs[i].backPropagate(this.error);
}
}
}

toObject() {
return {
type: this.constructor.name,
Expand Down
46 changes: 46 additions & 0 deletions tests/activation-mapping.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
'use strict';

const Network = require('../src/Network.js');

test('library maps activation', () => {

// Build the network...
var network = new Network({
layers: [2,1],
bias: false
});

var data = [
{x: [1,0], y: [1]},
{x: [1,1], y: [3]}
];

// 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...
network.layers[network.layers.length-1].neurons[0].mapActivation([1]);

expect(Math.round(network.layers[0].neurons[0].activation)).toBe(1);
expect(Math.round(network.layers[0].neurons[1].activation)).toBe(2);
// Done.

});

0 comments on commit 714bdf7

Please sign in to comment.