Skip to content

Commit

Permalink
added new test case
Browse files Browse the repository at this point in the history
added new test case: compare if optimized and unoptimized networks
output the same results when activating/propagating, for 5 consecutive
iterations.
  • Loading branch information
cazala committed Oct 24, 2014
1 parent 0094d12 commit 919b1e8
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 12 deletions.
53 changes: 41 additions & 12 deletions lib/synaptic.js
Original file line number Diff line number Diff line change
Expand Up @@ -1080,19 +1080,40 @@ Network.prototype = {
// feed-forward activation of all the layers to produce an ouput
activate: function(input) {

if (!this.optimized)
this.optimize();

return this.optimized.activate(input);
if (this.optimized === false)
{
this.layers.input.activate(input);
for (var layer in this.layers.hidden)
this.layers.hidden[layer].activate();
return this.layers.output.activate();
}
else
{
if (this.optimized == null)
this.optimize();
return this.optimized.activate(input);
}
},

// back-propagate the error thru the network
propagate: function(rate, target) {

if (!this.optimized)
this.optimize();

return this.optimized.propagate(rate, target);
if (this.optimized === false)
{
this.layers.output.propagate(rate, target);
var reverse = [];
for (var layer in this.layers.hidden)
reverse.push(this.layers.hidden[layer]);
reverse.reverse();
for (var layer in reverse)
reverse[layer].propagate(rate);
}
else
{
if (this.optimized == null)
this.optimize();
this.optimized.propagate(rate, target);
}
},

// project a connection to another unit (either a network or a layer)
Expand Down Expand Up @@ -1158,6 +1179,7 @@ Network.prototype = {
// hardcodes the behaviour of the whole network into a single optimized function
optimize: function() {

var that = this;
var optimized = {};
var neurons = this.neurons();

Expand Down Expand Up @@ -1213,10 +1235,10 @@ Network.prototype = {
check_propagation: this.propagate
}
network.reset = function() {
if (this.optimized) {
this.optimized = null;
this.activate = network.data.check_activation;
this.propagate = network.data.check_propagation;
if (that.optimized) {
that.optimized = null;
that.activate = network.data.check_activation;
that.propagate = network.data.check_propagation;
}
}

Expand Down Expand Up @@ -1338,6 +1360,13 @@ Network.prototype = {
this.optimized.reset();
},

setOptimize: function(bool){
this.restore();
if (this.optimized)
this.optimized.reset();
this.optimized = bool? null : false;
},

// returns a json that represents all the neurons and connections of the network
toJSON: function(ignoreTraces) {

Expand Down
64 changes: 64 additions & 0 deletions test/synaptic.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ var assert = require('assert'),

var Perceptron = synaptic.Architect.Perceptron,
LSTM = synaptic.Architect.LSTM;
Trainer = synaptic.Trainer;

describe("Perceptron - XOR", function() {

Expand Down Expand Up @@ -146,3 +147,66 @@ describe("LSTM - Discrete Sequence Recall", function() {
});
}
});

describe("Optimized and Unoptimized Networks Equivalency", function() {
var optimized = new Perceptron(2,3,1);

var unoptimized = optimized.clone();
unoptimized.setOptimize(false);

// activate networks
var output1 = optimized.activate([0,0]);
var output2 = unoptimized.activate([0,0]);

it('1) Same output for both networks', function(){
assert((output1 - output2) == 0);
});

// propagate networks
optimized.propagate(.1, [0]);
unoptimized.propagate(.1, [0]);

// activate networks
var output1 = optimized.activate([1,0]);
var output2 = unoptimized.activate([1,0]);

it('2) Same output for both networks', function(){
assert((output1 - output2) == 0);
});

// propagate networks
optimized.propagate(.4, [1]);
unoptimized.propagate(.4, [1]);

// activate networks
var output1 = optimized.activate([0,1]);
var output2 = unoptimized.activate([0,1]);

it('3) Same output for both networks', function(){
assert((output1 - output2) == 0);
});

// propagate networks
optimized.propagate(.2, [1]);
unoptimized.propagate(.2, [1]);

// activate networks
var output1 = optimized.activate([1,1]);
var output2 = unoptimized.activate([1,1]);

it('4) Same output for both networks', function(){
assert((output1 - output2) == 0);
});

// propagate networks
optimized.propagate(.3, [0]);
unoptimized.propagate(.3, [0]);

// activate networks
var output1 = optimized.activate([1,0]);
var output2 = unoptimized.activate([1,0]);

it('5) Same output for both networks', function(){
assert((output1 - output2) == 0);
});
});

0 comments on commit 919b1e8

Please sign in to comment.