Skip to content

Commit

Permalink
Fixed rate callback and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bobalazek committed Jul 17, 2016
1 parent 8ff4f28 commit c233227
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 3 deletions.
2 changes: 1 addition & 1 deletion dist/synaptic.js
Original file line number Diff line number Diff line change
Expand Up @@ -1983,6 +1983,7 @@

var currentSetSize = set.length;
error = 0;
iterations++;

if(bucketSize > 0) {
var currentBucket = Math.floor(iterations / bucketSize);
Expand All @@ -2003,7 +2004,6 @@
}

// check error
iterations++;
error /= currentSetSize;

if (options) {
Expand Down
2 changes: 1 addition & 1 deletion src/trainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ Trainer.prototype = {

var currentSetSize = set.length;
error = 0;
iterations++;

if(bucketSize > 0) {
var currentBucket = Math.floor(iterations / bucketSize);
Expand All @@ -101,7 +102,6 @@ Trainer.prototype = {
}

// check error
iterations++;
error /= currentSetSize;

if (options) {
Expand Down
84 changes: 83 additions & 1 deletion test/synaptic.js
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@ describe("Scheduled Tasks", function() {
assert.equal( final_stats.iterations, 2000 )
});

it('should work even if shedule.do() returns no value', function(){
it('should work even if schedule.do() returns no value', function(){
var final_stats = perceptron.trainer.XOR({
iterations: 3000,
rate: 0.000001,
Expand All @@ -576,3 +576,85 @@ describe("Scheduled Tasks", function() {
});

});

describe("Rate Callback Check", function() {
var perceptron = new Perceptron(2, 3, 1);

it('should switch rate from 0.01 to 0.005 after 1000 iterations', function(){
var final_stats = perceptron.trainer.XOR({
iterations: 2000,
rate: function(iterations, error) {
return iterations < 1000 ? 0.01 : 0.005
},
error: 0.000001,
schedule: {
every: 1,
do: function(data) {
if(data.iterations == 1){
assert.equal( data.rate, 0.01 )
}

if(data.iterations == 500){
assert.equal( data.rate, 0.01 )
}

if(data.iterations == 999){
assert.equal( data.rate, 0.01 )
}

if(data.iterations == 1000){
assert.equal( data.rate, 0.005 )
}

if(data.iterations == 1500){
assert.equal( data.rate, 0.005 )
}

if(data.iterations == 2000){
assert.equal( data.rate, 0.005 )
}
}
}
});
});
});

describe("Rate Array Check", function() {
var perceptron = new Perceptron(2, 3, 1);

it('should switch rate from 0.01 to 0.005 after 1000 iterations', function(){
var final_stats = perceptron.trainer.XOR({
iterations: 2000,
rate: [0.01, 0.005],
error: 0.000001,
schedule: {
every: 1,
do: function(data) {
if(data.iterations == 1){
assert.equal( data.rate, 0.01 )
}

if(data.iterations == 500){
assert.equal( data.rate, 0.01 )
}

if(data.iterations == 999){
assert.equal( data.rate, 0.01 )
}

if(data.iterations == 1000){
assert.equal( data.rate, 0.005 )
}

if(data.iterations == 1500){
assert.equal( data.rate, 0.005 )
}

if(data.iterations == 2000){
assert.equal( data.rate, 0.005 )
}
}
}
});
});
});

0 comments on commit c233227

Please sign in to comment.