Skip to content

Commit

Permalink
neural net perf tweaks - training time in half
Browse files Browse the repository at this point in the history
  • Loading branch information
harthur committed Oct 5, 2010
1 parent 6e77d32 commit c0a4a2e
Showing 1 changed file with 16 additions and 14 deletions.
30 changes: 16 additions & 14 deletions lib/neuralnetwork.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,8 @@ NeuralNetwork.prototype = {
},

train : function(data, iterations, errorThresh, callback, resolution) {
if(!iterations)
iterations = 20000;
if(!errorThresh)
errorThresh = 0.007;
iterations = iterations || 20000;
errorThresh = errorThresh || 0.007;

var error = 1;
for(var i = 0; i < iterations && error > errorThresh; i++) {
Expand Down Expand Up @@ -177,12 +175,14 @@ Layer.prototype = {
},

getError : function() {
var sum = this.reduce(function(sum, node) { return sum + Math.pow(node.error, 2);}, 0);
var sum = this.reduce(function(sum, node) {
return sum + Math.pow(node.error, 2);
}, 0);
return Math.sqrt(sum) / this.getSize(); // mean squared error
},

getSize : function() {
return this.reduce(function(count) { return ++count;}, 0);
return this.reduce(function(count) { return ++count; }, 0);
},

map : function(callback) {
Expand Down Expand Up @@ -225,15 +225,18 @@ Layer.prototype = {
},

calcOutputs : function() {
this.map(function(node) { node.calcOutput(); });
for(var id in this.nodes)
this.nodes[id].calcOutput();
},

calcErrors : function(targets) {
this.map(function(node) { node.calcError(targets); });
for(var id in this.nodes)
this.nodes[id].calcError(targets);
},

adjustWeights : function() {
this.map(function(node) { node.adjustWeights(); });
for(var id in this.nodes)
this.nodes[id].adjustWeights();
},

toJSON : function() {
Expand Down Expand Up @@ -268,8 +271,6 @@ function Node(layer, id, json) {
}

Node.prototype = {
getInputs : function() { return this.layer.prevLayer.getOutputs(); },

getIncoming : function() { return this.layer.prevLayer.nodes; },

getOutgoing : function() { return this.layer.nextLayer.nodes; },
Expand All @@ -293,8 +294,9 @@ Node.prototype = {

calcOutput : function() {
var sum = this.bias;
var inputs = this.getIncoming();
for(var id in this.weights)
sum += this.weights[id] * this.getInputs()[id];
sum += this.weights[id] * inputs[id].output;
this.output = this.sigmoid(sum);
},

Expand All @@ -316,9 +318,9 @@ Node.prototype = {
var rate = this.layer.network.learningRate;
var momentum = this.layer.network.momentum;

var inputs = this.getInputs();
var inputs = this.getIncoming();
for(var id in inputs) {
var change = rate * this.delta * inputs[id] + momentum * this.change[id];
var change = rate * this.delta * inputs[id].output + momentum * this.change[id];
this.change[id] = change;
this.weights[id] += change;
}
Expand Down

0 comments on commit c0a4a2e

Please sign in to comment.