Merge pull request #115 from Jabher/master

removing useless variables && declaring missed ones
Esse commit está contido em:
Juan Cazala
2016-07-25 15:11:33 -03:00
commit de GitHub
8 arquivos alterados com 266 adições e 329 exclusões
+6 -1
Ver Arquivo
@@ -1,3 +1,8 @@
language: node_js
node_js:
- "0.10"
# always latest release
- "node"
# previous releases
- "6"
- "5"
- "4"
+41 -47
Ver Arquivo
@@ -207,7 +207,6 @@
var influences = [];
for (var id in this.trace.extended) {
// extended elegibility trace
var xtrace = this.trace.extended[id];
var neuron = this.neighboors[id];
// if gated neuron's selfconnection is gated by this unit, the influence keeps track of the neuron's old state
@@ -445,7 +444,6 @@
optimize: function(optimized, layer) {
optimized = optimized || {};
var that = this;
var store_activation = [];
var store_trace = [];
var store_propagation = [];
@@ -468,7 +466,7 @@
layers.__count = store.push([]) - 1;
layers[layer] = layers.__count;
}
}
};
allocate(activation_sentences);
allocate(trace_sentences);
allocate(propagation_sentences);
@@ -527,7 +525,7 @@
sentence += 'F[' + args[i].id + ']';
store.push(sentence + ';');
}
};
// helper to check if an object is empty
var isEmpty = function(obj) {
@@ -615,7 +613,6 @@
for (var id in this.trace.extended) {
// calculate extended elegibility traces in advance
var xtrace = this.trace.extended[id];
var neuron = this.neighboors[id];
var influence = getVar('influences[' + neuron.ID + ']');
var neuron_old = getVar(neuron, 'old');
@@ -673,10 +670,8 @@
}
for (var id in this.trace.extended) {
// extended elegibility trace
var xtrace = this.trace.extended[id];
var neuron = this.neighboors[id];
var influence = getVar('influences[' + neuron.ID + ']');
var neuron_old = getVar(neuron, 'old');
var trace = getVar(this, 'trace', 'elegibility', input.ID, this.trace
.elegibility[input.ID]);
@@ -1669,7 +1664,6 @@
code += " " + layerID + " -> " + layerToID + " [label = " + size + "]\n";
for (var from in connection.gatedfrom) { // gatings
var layerfrom = connection.gatedfrom[from].layer;
var type = connection.gatedfrom[from].type;
var layerfromID = layers.indexOf(layerfrom);
code += " " + layerfromID + " -> " + fakeNode + " [color = blue]\n";
}
@@ -1677,7 +1671,6 @@
code += " " + layerID + " -> " + layerToID + " [label = " + size + "]\n";
for (var from in connection.gatedfrom) { // gatings
var layerfrom = connection.gatedfrom[from].layer;
var type = connection.gatedfrom[from].type;
var layerfromID = layers.indexOf(layerfrom);
code += " " + layerfromID + " -> " + layerToID + " [color = blue]\n";
}
@@ -1749,7 +1742,7 @@
// Copy the options and set defaults (options might be different for each worker)
var workerOptions = {};
if(options) workerOptions = options
if(options) workerOptions = options;
workerOptions.rate = options.rate || .2;
workerOptions.iterations = options.iterations || 100000;
workerOptions.error = options.error || .005;
@@ -1809,7 +1802,7 @@
clone: function() {
return Network.fromJSON(this.toJSON());
}
}
};
/**
* Creates a static String to store the source code of the functions
@@ -1853,7 +1846,7 @@
input: new Layer(),
hidden: [],
output: new Layer()
}
};
for (var i in json.neurons) {
var config = json.neurons[i];
@@ -1883,7 +1876,7 @@
var config = json.connections[i];
var from = neurons[config.from];
var to = neurons[config.to];
var weight = config.weight
var weight = config.weight;
var gater = neurons[config.gater];
var connection = from.project(to, weight);
@@ -1892,7 +1885,7 @@
}
return new Network(layers);
}
};
/* WEBPACK VAR INJECTION */}.call(exports, __webpack_require__(3)(module)))
@@ -1925,7 +1918,7 @@
var error = 1;
var iterations = bucketSize = 0;
var abort = false;
var input, output, target, currentRate;
var currentRate;
var cost = options && options.cost || this.cost || Trainer.cost.MSE;
var crossValidate = false, testSet, trainSet;
@@ -1967,7 +1960,7 @@
currentRate = this.rate;
if(Array.isArray(this.rate)) {
bucketSize = Math.floor(this.iterations / this.rate.length);
var bucketSize = Math.floor(this.iterations / this.rate.length);
}
if(crossValidate) {
@@ -2022,14 +2015,14 @@
error: error,
iterations: iterations,
time: Date.now() - start
}
};
return results;
},
// trains any given set to a network, using a WebWorker (only for the browser). Returns a Promise of the results.
trainAsync: function(set, options) {
var train = this.workerTrain.bind(this)
var train = this.workerTrain.bind(this);
return new Promise(function(resolve, reject) {
try {
train(set, resolve, options, true)
@@ -2043,10 +2036,10 @@
_trainSet: function(set, currentRate, costFunction) {
var errorSum = 0;
for (var train in set) {
input = set[train].input;
target = set[train].output;
var input = set[train].input;
var target = set[train].output;
output = this.network.activate(input);
var output = this.network.activate(input);
this.network.propagate(currentRate, target);
errorSum += costFunction(target, output);
@@ -2058,7 +2051,6 @@
test: function(set, options) {
var error = 0;
var abort = false;
var input, output, target;
var cost = options && options.cost || this.cost || Trainer.cost.MSE;
@@ -2076,7 +2068,7 @@
var results = {
error: error,
time: Date.now() - start
}
};
return results;
},
@@ -2126,7 +2118,7 @@
}
break;
}
}
};
// Start the worker
worker.postMessage({action: 'startTraining'});
@@ -2143,7 +2135,7 @@
log: false,
shuffle: true,
cost: Trainer.cost.MSE
}
};
if (options)
for (var i in options)
@@ -2179,8 +2171,9 @@
var schedule = options.schedule || {};
var cost = options.cost || this.cost || Trainer.cost.CROSS_ENTROPY;
var trial = correct = i = j = success = 0,
error = 1,
var trial, correct, i, j, success;
trial = correct = i = j = success = 0;
var error = 1,
symbols = targets.length + distractors.length + prompts.length;
var noRepeat = function(range, avoid) {
@@ -2190,14 +2183,14 @@
if (number == avoid[i])
used = true;
return used ? noRepeat(range, avoid) : number;
}
};
var equal = function(prediction, output) {
for (var i in prediction)
if (Math.round(prediction[i]) != output[i])
return false;
return true;
}
};
var start = Date.now();
@@ -2222,6 +2215,7 @@
}
//train sequence
var distractorsCorrect;
var targetsCorrect = distractorsCorrect = 0;
error = 0;
for (i = 0; i < length; i++) {
@@ -2303,7 +2297,7 @@
// gramar node
var Node = function() {
this.paths = [];
}
};
Node.prototype = {
connect: function(node, value) {
this.paths.push({
@@ -2324,7 +2318,7 @@
return this.paths[i];
return false;
}
}
};
var reberGrammar = function() {
@@ -2333,19 +2327,19 @@
var n1 = (new Node()).connect(output, "E");
var n2 = (new Node()).connect(n1, "S");
var n3 = (new Node()).connect(n1, "V").connect(n2, "P");
var n4 = (new Node()).connect(n2, "X")
var n4 = (new Node()).connect(n2, "X");
n4.connect(n4, "S");
var n5 = (new Node()).connect(n3, "V")
var n5 = (new Node()).connect(n3, "V");
n5.connect(n5, "T");
n2.connect(n5, "X")
n2.connect(n5, "X");
var n6 = (new Node()).connect(n4, "T").connect(n5, "P");
var input = (new Node()).connect(n6, "B")
var input = (new Node()).connect(n6, "B");
return {
input: input,
output: output
}
}
};
// build an embeded reber grammar
var embededReberGrammar = function() {
@@ -2365,7 +2359,7 @@
output: output
}
}
};
// generate an ERG sequence
var generate = function() {
@@ -2377,7 +2371,7 @@
next = next.node.any();
}
return str;
}
};
// test if a string matches an embeded reber grammar
var test = function(str) {
@@ -2392,7 +2386,7 @@
ch = str.charAt(++i);
}
return true;
}
};
// helper to check if the output and the target vectors match
var different = function(array1, array2) {
@@ -2412,7 +2406,7 @@
}
return i1 != i2;
}
};
var iteration = 0;
var error = 1;
@@ -2423,7 +2417,7 @@
"X": 3,
"S": 4,
"E": 5
}
};
var start = Date.now();
while (iteration < iterations && error > criterion) {
@@ -2482,7 +2476,7 @@
throw new Error("Invalid Network: must have 2 inputs and one output");
if (typeof options == 'undefined')
var options = {};
options = {};
// helper
function getSamples (trainingSize, testSize){
@@ -2492,7 +2486,7 @@
// generate samples
var t = 0;
var set = [];
var set = [];
for (var i = 0; i < size; i++) {
set.push({ input: [0,0], output: [0] });
}
@@ -2610,7 +2604,7 @@
var previous = input;
// generate hidden layers
for (level in layers) {
for (var level in layers) {
var size = layers[level];
var layer = new Layer(size);
hidden.push(layer);
@@ -2799,8 +2793,8 @@
this.trainer = new Trainer(this);
},
Hopfield: function Hopfield(size)
{
Hopfield: function Hopfield(size) {
var inputLayer = new Layer(size);
var outputLayer = new Layer(size);
@@ -2830,7 +2824,7 @@
error: .00005,
rate: 1
});
}
};
proto.feed = proto.feed || function(pattern)
{
+4 -1
Ver Arquivo
@@ -12,8 +12,11 @@
"build"
],
"devDependencies": {
"chai": "^3.5.0",
"mocha": "^2.2.4",
"webpack": "^1.13.1"
"webpack": "^1.13.1",
"random-js": "^1.0.8",
"chai-stats": "^0.3.0"
},
"repository": {
"type": "git",
+4 -4
Ver Arquivo
@@ -28,7 +28,7 @@ var Architect = {
var previous = input;
// generate hidden layers
for (level in layers) {
for (var level in layers) {
var size = layers[level];
var layer = new Layer(size);
hidden.push(layer);
@@ -217,8 +217,8 @@ var Architect = {
this.trainer = new Trainer(this);
},
Hopfield: function Hopfield(size)
{
Hopfield: function Hopfield(size) {
var inputLayer = new Layer(size);
var outputLayer = new Layer(size);
@@ -248,7 +248,7 @@ var Architect = {
error: .00005,
rate: 1
});
}
};
proto.feed = proto.feed || function(pattern)
{
+5 -7
Ver Arquivo
@@ -421,7 +421,6 @@ Network.prototype = {
code += " " + layerID + " -> " + layerToID + " [label = " + size + "]\n";
for (var from in connection.gatedfrom) { // gatings
var layerfrom = connection.gatedfrom[from].layer;
var type = connection.gatedfrom[from].type;
var layerfromID = layers.indexOf(layerfrom);
code += " " + layerfromID + " -> " + fakeNode + " [color = blue]\n";
}
@@ -429,7 +428,6 @@ Network.prototype = {
code += " " + layerID + " -> " + layerToID + " [label = " + size + "]\n";
for (var from in connection.gatedfrom) { // gatings
var layerfrom = connection.gatedfrom[from].layer;
var type = connection.gatedfrom[from].type;
var layerfromID = layers.indexOf(layerfrom);
code += " " + layerfromID + " -> " + layerToID + " [color = blue]\n";
}
@@ -501,7 +499,7 @@ Network.prototype = {
// Copy the options and set defaults (options might be different for each worker)
var workerOptions = {};
if(options) workerOptions = options
if(options) workerOptions = options;
workerOptions.rate = options.rate || .2;
workerOptions.iterations = options.iterations || 100000;
workerOptions.error = options.error || .005;
@@ -561,7 +559,7 @@ Network.prototype = {
clone: function() {
return Network.fromJSON(this.toJSON());
}
}
};
/**
* Creates a static String to store the source code of the functions
@@ -605,7 +603,7 @@ Network.fromJSON = function(json) {
input: new Layer(),
hidden: [],
output: new Layer()
}
};
for (var i in json.neurons) {
var config = json.neurons[i];
@@ -635,7 +633,7 @@ Network.fromJSON = function(json) {
var config = json.connections[i];
var from = neurons[config.from];
var to = neurons[config.to];
var weight = config.weight
var weight = config.weight;
var gater = neurons[config.gater];
var connection = from.project(to, weight);
@@ -644,4 +642,4 @@ Network.fromJSON = function(json) {
}
return new Network(layers);
}
};
+2 -7
Ver Arquivo
@@ -66,7 +66,6 @@ Neuron.prototype = {
var influences = [];
for (var id in this.trace.extended) {
// extended elegibility trace
var xtrace = this.trace.extended[id];
var neuron = this.neighboors[id];
// if gated neuron's selfconnection is gated by this unit, the influence keeps track of the neuron's old state
@@ -304,7 +303,6 @@ Neuron.prototype = {
optimize: function(optimized, layer) {
optimized = optimized || {};
var that = this;
var store_activation = [];
var store_trace = [];
var store_propagation = [];
@@ -327,7 +325,7 @@ Neuron.prototype = {
layers.__count = store.push([]) - 1;
layers[layer] = layers.__count;
}
}
};
allocate(activation_sentences);
allocate(trace_sentences);
allocate(propagation_sentences);
@@ -386,7 +384,7 @@ Neuron.prototype = {
sentence += 'F[' + args[i].id + ']';
store.push(sentence + ';');
}
};
// helper to check if an object is empty
var isEmpty = function(obj) {
@@ -474,7 +472,6 @@ Neuron.prototype = {
for (var id in this.trace.extended) {
// calculate extended elegibility traces in advance
var xtrace = this.trace.extended[id];
var neuron = this.neighboors[id];
var influence = getVar('influences[' + neuron.ID + ']');
var neuron_old = getVar(neuron, 'old');
@@ -532,10 +529,8 @@ Neuron.prototype = {
}
for (var id in this.trace.extended) {
// extended elegibility trace
var xtrace = this.trace.extended[id];
var neuron = this.neighboors[id];
var influence = getVar('influences[' + neuron.ID + ']');
var neuron_old = getVar(neuron, 'old');
var trace = getVar(this, 'trace', 'elegibility', input.ID, this.trace
.elegibility[input.ID]);
+30 -29
Ver Arquivo
@@ -23,7 +23,7 @@ Trainer.prototype = {
var error = 1;
var iterations = bucketSize = 0;
var abort = false;
var input, output, target, currentRate;
var currentRate;
var cost = options && options.cost || this.cost || Trainer.cost.MSE;
var crossValidate = false, testSet, trainSet;
@@ -65,7 +65,7 @@ Trainer.prototype = {
currentRate = this.rate;
if(Array.isArray(this.rate)) {
bucketSize = Math.floor(this.iterations / this.rate.length);
var bucketSize = Math.floor(this.iterations / this.rate.length);
}
if(crossValidate) {
@@ -120,14 +120,14 @@ Trainer.prototype = {
error: error,
iterations: iterations,
time: Date.now() - start
}
};
return results;
},
// trains any given set to a network, using a WebWorker (only for the browser). Returns a Promise of the results.
trainAsync: function(set, options) {
var train = this.workerTrain.bind(this)
var train = this.workerTrain.bind(this);
return new Promise(function(resolve, reject) {
try {
train(set, resolve, options, true)
@@ -141,10 +141,10 @@ Trainer.prototype = {
_trainSet: function(set, currentRate, costFunction) {
var errorSum = 0;
for (var train in set) {
input = set[train].input;
target = set[train].output;
var input = set[train].input;
var target = set[train].output;
output = this.network.activate(input);
var output = this.network.activate(input);
this.network.propagate(currentRate, target);
errorSum += costFunction(target, output);
@@ -156,7 +156,6 @@ Trainer.prototype = {
test: function(set, options) {
var error = 0;
var abort = false;
var input, output, target;
var cost = options && options.cost || this.cost || Trainer.cost.MSE;
@@ -174,7 +173,7 @@ Trainer.prototype = {
var results = {
error: error,
time: Date.now() - start
}
};
return results;
},
@@ -224,7 +223,7 @@ Trainer.prototype = {
}
break;
}
}
};
// Start the worker
worker.postMessage({action: 'startTraining'});
@@ -241,7 +240,7 @@ Trainer.prototype = {
log: false,
shuffle: true,
cost: Trainer.cost.MSE
}
};
if (options)
for (var i in options)
@@ -277,8 +276,9 @@ Trainer.prototype = {
var schedule = options.schedule || {};
var cost = options.cost || this.cost || Trainer.cost.CROSS_ENTROPY;
var trial = correct = i = j = success = 0,
error = 1,
var trial, correct, i, j, success;
trial = correct = i = j = success = 0;
var error = 1,
symbols = targets.length + distractors.length + prompts.length;
var noRepeat = function(range, avoid) {
@@ -288,14 +288,14 @@ Trainer.prototype = {
if (number == avoid[i])
used = true;
return used ? noRepeat(range, avoid) : number;
}
};
var equal = function(prediction, output) {
for (var i in prediction)
if (Math.round(prediction[i]) != output[i])
return false;
return true;
}
};
var start = Date.now();
@@ -320,6 +320,7 @@ Trainer.prototype = {
}
//train sequence
var distractorsCorrect;
var targetsCorrect = distractorsCorrect = 0;
error = 0;
for (i = 0; i < length; i++) {
@@ -401,7 +402,7 @@ Trainer.prototype = {
// gramar node
var Node = function() {
this.paths = [];
}
};
Node.prototype = {
connect: function(node, value) {
this.paths.push({
@@ -422,7 +423,7 @@ Trainer.prototype = {
return this.paths[i];
return false;
}
}
};
var reberGrammar = function() {
@@ -431,19 +432,19 @@ Trainer.prototype = {
var n1 = (new Node()).connect(output, "E");
var n2 = (new Node()).connect(n1, "S");
var n3 = (new Node()).connect(n1, "V").connect(n2, "P");
var n4 = (new Node()).connect(n2, "X")
var n4 = (new Node()).connect(n2, "X");
n4.connect(n4, "S");
var n5 = (new Node()).connect(n3, "V")
var n5 = (new Node()).connect(n3, "V");
n5.connect(n5, "T");
n2.connect(n5, "X")
n2.connect(n5, "X");
var n6 = (new Node()).connect(n4, "T").connect(n5, "P");
var input = (new Node()).connect(n6, "B")
var input = (new Node()).connect(n6, "B");
return {
input: input,
output: output
}
}
};
// build an embeded reber grammar
var embededReberGrammar = function() {
@@ -463,7 +464,7 @@ Trainer.prototype = {
output: output
}
}
};
// generate an ERG sequence
var generate = function() {
@@ -475,7 +476,7 @@ Trainer.prototype = {
next = next.node.any();
}
return str;
}
};
// test if a string matches an embeded reber grammar
var test = function(str) {
@@ -490,7 +491,7 @@ Trainer.prototype = {
ch = str.charAt(++i);
}
return true;
}
};
// helper to check if the output and the target vectors match
var different = function(array1, array2) {
@@ -510,7 +511,7 @@ Trainer.prototype = {
}
return i1 != i2;
}
};
var iteration = 0;
var error = 1;
@@ -521,7 +522,7 @@ Trainer.prototype = {
"X": 3,
"S": 4,
"E": 5
}
};
var start = Date.now();
while (iteration < iterations && error > criterion) {
@@ -580,7 +581,7 @@ Trainer.prototype = {
throw new Error("Invalid Network: must have 2 inputs and one output");
if (typeof options == 'undefined')
var options = {};
options = {};
// helper
function getSamples (trainingSize, testSize){
@@ -590,7 +591,7 @@ Trainer.prototype = {
// generate samples
var t = 0;
var set = [];
var set = [];
for (var i = 0; i < size; i++) {
set.push({ input: [0,0], output: [0] });
}
+174 -233
Ver Arquivo
@@ -1,62 +1,68 @@
// import
var chai = require('chai');
chai.use(require('chai-stats'));
var assert = chai.assert;
var assert = require('assert'),
synaptic = require('../src/synaptic');
var synaptic = require('../src/synaptic');
var Perceptron = synaptic.Architect.Perceptron,
LSTM = synaptic.Architect.LSTM,
Layer = synaptic.Layer,
Network = synaptic.Network,
Trainer = synaptic.Trainer;
var Perceptron = synaptic.Architect.Perceptron;
var LSTM = synaptic.Architect.LSTM;
var Layer = synaptic.Layer;
var Network = synaptic.Network;
var Trainer = synaptic.Trainer;
var learningRate = .5;
// utils
function noRepeat (range, avoid) {
function noRepeat(range, avoid) {
var number = Math.random() * range | 0;
for (var i in avoid){
if (number == avoid[i]){
return noRepeat(range,avoid);
for (var i in avoid) {
if (number == avoid[i]) {
return noRepeat(range, avoid);
}
}
return number;
}
function equal (prediction, output) {
function equal(prediction, output) {
for (var i in prediction)
if (Math.round(prediction[i]) != output[i])
return false;
return true;
}
function generateRandomArray (size){
var array = [];
for (var j = 0; j < size; j++)
array.push(Math.random() + .5 | 0);
return array;
function generateRandomArray(size) {
var array = [];
for (var j = 0; j < size; j++)
array.push(Math.random() + .5 | 0);
return array;
}
function compare (a, b) {
function calculateMse(a, b) {
var mse = 0;
for (var k in a)
mse += Math.pow(a[k] - b[k], 2);
mse /= a.length;
return mse < 1e-10;
return mse;
}
function equalWithError (output, expected, error) {
function equalWithError(output, expected, error) {
return Math.abs(output - expected) <= error;
}
// specs
describe('Basic Neural Network', function() {
describe('Basic Neural Network', function () {
it("trains an AND gate", function() {
it("trains an AND gate", function () {
var inputLayer = new Layer(2),
outputLayer = new Layer(1);
outputLayer = new Layer(1);
inputLayer.project(outputLayer);
@@ -99,7 +105,7 @@ describe('Basic Neural Network', function() {
assert.equal(test11, 1, "[1,1] did not output 1");
});
it("trains an OR gate", function() {
it("trains an OR gate", function () {
var inputLayer = new Layer(2),
outputLayer = new Layer(1);
@@ -145,11 +151,10 @@ describe('Basic Neural Network', function() {
assert.equal(test11, 1, "[1,1] did not output 1");
});
it("trains a NOT gate", function() {
it("trains a NOT gate", function () {
var inputLayer = new Layer(1),
outputLayer = new Layer(1),
network;
outputLayer = new Layer(1);
inputLayer.project(outputLayer);
@@ -180,50 +185,43 @@ describe('Basic Neural Network', function() {
});
});
describe("Perceptron - XOR", function() {
describe("Perceptron - XOR", function () {
var perceptron = new Perceptron(2, 3, 1);
perceptron.trainer.XOR();
var test00 = Math.round(perceptron.activate([0, 0]));
it("input: [0,0] output: " + test00, function() {
assert.equal(test00, 0, "[0,0] did not output 0");
it("should return near-0 value on [0,0]", function () {
assert.isAtMost(perceptron.activate([0, 0]), .49, "[0,0] did not output 0");
});
var test01 = Math.round(perceptron.activate([0, 1]));
it("input: [0,1] output: " + test01, function() {
assert.equal(test01, 1, "[0,1] did not output 1");
it("should return near-1 value on [0,1]", function () {
assert.isAtLeast(perceptron.activate([0, 1]), .51, "[0,1] did not output 1");
});
var test10 = Math.round(perceptron.activate([1, 0]));
it("input: [1,0] output: " + test10, function() {
assert.equal(test10, 1, "[1,0] did not output 1");
it("should return near-1 value on [1,0]", function () {
assert.isAtLeast(perceptron.activate([1, 0]), .51, "[1,0] did not output 1");
});
var test11 = Math.round(perceptron.activate([1, 1]));
it("input: [1,1] output: " + test11, function() {
assert.equal(test11, 0, "[1,1] did not output 0");
it("should return near-0 value on [1,1]", function () {
assert.isAtMost(perceptron.activate([1, 1]), .49, "[1,1] did not output 0");
});
});
describe("Perceptron - SIN", function() {
var mySin = function(x) {
return (Math.sin(x)+1)/2;
describe("Perceptron - SIN", function () {
var mySin = function (x) {
return (Math.sin(x) + 1) / 2;
};
var sinNetwork = new Perceptron(1, 12, 1);
var trainingSet = [];
var trainingSet = Array.apply(null, Array(800)).map(function () {
while (trainingSet.length < 800) {
var inputValue = Math.random() * Math.PI * 2;
return {
trainingSet.push({
input: [inputValue],
output: [mySin(inputValue)]
};
});
});
}
var results = sinNetwork.trainer.train(trainingSet, {
iterations: 2000,
@@ -232,38 +230,26 @@ describe("Perceptron - SIN", function() {
cost: Trainer.cost.MSE,
});
var test0 = sinNetwork.activate([0])[0];
var expected0 = mySin(0);
it("input: [0] output: " + test0 + ", expected: " + expected0, function() {
var eq = equalWithError(test0, expected0, .035);
assert.equal(eq, true, "[0] did not output " + expected0);
});
var test05PI = sinNetwork.activate([.5*Math.PI])[0];
var expected05PI = mySin(.5*Math.PI);
it("input: [0.5*Math.PI] output: " + test05PI + ", expected: " + expected05PI, function() {
var eq = equalWithError(test05PI, expected05PI, .035);
assert.equal(eq, true, "[0.5*Math.PI] did not output " + expected05PI);
});
var test2 = sinNetwork.activate([2])[0];
var expected2 = mySin(2);
it("input: [2] output: " + test2 + ", expected: " + expected2, function() {
var eq = equalWithError(test2, expected2, .035);
assert.equal(eq, true, "[2] did not output " + expected2);
});
[0, .5 * Math.PI, 2]
.forEach(function (x) {
var y = mySin(x);
it("should return value around " + y + " when [" + x + "] is on input", function () {
// near scalability: abs(expected-actual) < 0.5 * 10**(-decimal)
// 0.5 * Math.pow(10, -.15) => 0.35397289219206896
assert.almostEqual(sinNetwork.activate([x])[0], y, .15);
});
});
var errorResult = results.error;
it("Sin error: " + errorResult, function() {
var lessThanOrEqualError = errorResult <= .001;
assert.equal(lessThanOrEqualError, true, "Sin error not less than or equal to desired error.");
it("Sin error: " + errorResult, function () {
assert.isAtMost(errorResult, .001, "Sin error not less than or equal to desired error.");
});
});
describe("Perceptron - SIN - CrossValidate", function() {
describe("Perceptron - SIN - CrossValidate", function () {
var mySin = function(x) {
return (Math.sin(x)+1)/2;
var mySin = function (x) {
return (Math.sin(x) + 1) / 2;
};
var sinNetwork = new Perceptron(1, 12, 1);
@@ -289,33 +275,31 @@ describe("Perceptron - SIN - CrossValidate", function() {
var test0 = sinNetwork.activate([0])[0];
var expected0 = mySin(0);
it("input: [0] output: " + test0 + ", expected: " + expected0, function() {
var eq = equalWithError(test0, expected0, .035);
assert.equal(eq, true, "[0] did not output " + expected0);
it("input: [0] output: " + test0 + ", expected: " + expected0, function () {
assert.isAtMost(Math.abs(test0 - expected0), .035, "[0] did not output " + expected0);
});
var test05PI = sinNetwork.activate([.5*Math.PI])[0];
var expected05PI = mySin(.5*Math.PI);
it("input: [0.5*Math.PI] output: " + test05PI + ", expected: " + expected05PI, function() {
var eq = equalWithError(test05PI, expected05PI, .035);
assert.equal(eq, true, "[0.5*Math.PI] did not output " + expected05PI);
var test05PI = sinNetwork.activate([.5 * Math.PI])[0];
var expected05PI = mySin(.5 * Math.PI);
it("input: [0.5*Math.PI] output: " + test05PI + ", expected: " + expected05PI, function () {
assert.isAtMost(Math.abs(test05PI - expected05PI), .035, "[0.5*Math.PI] did not output " + expected05PI);
});
var test2 = sinNetwork.activate([2])[0];
var expected2 = mySin(2);
it("input: [2] output: " + test2 + ", expected: " + expected2, function() {
it("input: [2] output: " + test2 + ", expected: " + expected2, function () {
var eq = equalWithError(test2, expected2, .035);
assert.equal(eq, true, "[2] did not output " + expected2);
});
var errorResult = results.error;
it("CrossValidation error: " + errorResult, function() {
it("CrossValidation error: " + errorResult, function () {
var lessThanOrEqualError = errorResult <= .001;
assert.equal(lessThanOrEqualError, true, "CrossValidation error not less than or equal to desired error.");
});
});
describe("LSTM - Discrete Sequence Recall", function() {
describe("LSTM - Discrete Sequence Recall", function () {
var targets = [2, 4];
var distractors = [3, 5];
@@ -353,7 +337,7 @@ describe("LSTM - Discrete Sequence Recall", function() {
sequence.push(prompts[i]);
}
var check = function(which) {
var check = function (which) {
// generate input from sequence
var input = [];
for (j = 0; j < symbols; j++)
@@ -378,7 +362,7 @@ describe("LSTM - Discrete Sequence Recall", function() {
};
};
var value = function(array) {
var value = function (array) {
var max = .5;
var res = -1;
for (var i in array)
@@ -389,229 +373,196 @@ describe("LSTM - Discrete Sequence Recall", function() {
return res == -1 ? '-' : targets[res];
};
it("targets: " + targets, function() {
it("targets: " + targets, function () {
assert(true);
});
it("distractors: " + distractors, function() {
it("distractors: " + distractors, function () {
assert(true);
});
it("prompts: " + prompts, function() {
it("prompts: " + prompts, function () {
assert(true);
});
it("length: " + length + "\n", function() {
it("length: " + length + "\n", function () {
assert(true);
});
for (var i = 0; i < length; i++) {
var test = check(i);
it((i + 1) + ") input: " + sequence[i] + " output: " + value(test.prediction),
function() {
function () {
var ok = equal(test.prediction, test.output);
assert(ok);
});
}
});
describe("LSTM - Timing Task", function() {
var network = new synaptic.Architect.LSTM(2,7,1);
describe("LSTM - Timing Task", function () {
var network = new LSTM(2, 7, 1);
var result = network.trainer.timingTask({
log: false,
trainSamples: 4000,
testSamples: 500
});
it("should complete the training in less than 200 iterations", function() {
it("should complete the training in less than 200 iterations", function () {
assert(result.train.iterations <= 200);
});
it("should pass the test with an error smaller than 0.05", function() {
it("should pass the test with an error smaller than 0.05", function () {
assert(result.test.error < .05);
});
});
describe("Optimized and Unoptimized Networks Equivalency", function() {
var optimized = new LSTM(2,1,1)
describe("Optimized and Unoptimized Networks Equivalency", function () {
var unoptimized = optimized.clone();
unoptimized.setOptimize(false);
var optimized;
var unoptimized;
beforeEach(function () {
optimized = new LSTM(2, 1, 1);
unoptimized = optimized.clone();
unoptimized.setOptimize(false);
});
var learningRate = .5;
var iterations = 1000;
for (var i = 1; i <= iterations; i++)
{
//random input
it('should produce the same output for both networks', function () {
for (var i = 0; i < 1000; i++) {
var input = generateRandomArray(2);
// activate networks
var output1 = optimized.activate(input);
var output2 = unoptimized.activate(input);
if (i % 100 == 0)
it('should produce the same output for both networks after ' + i + ' iterations', function(){
assert(compare(output1, output2));
});
// random target
var target = generateRandomArray(1);
// propagate networks
optimized.activate(input);
unoptimized.activate(input);
optimized.propagate(learningRate, target);
unoptimized.propagate(learningRate, target);
}
}
var mse = calculateMse(optimized.activate(input), unoptimized.activate(input));
assert.isAtMost(mse, 1e-10, 'output should be same for both networks after ' + i + ' iterations');
});
});
describe("toJSON/fromJSON Networks Equivalency", function() {
var original = new LSTM(10,5,5);
describe("toJSON/fromJSON Networks Equivalency", function () {
var original;
var imported;
beforeEach(function () {
original = new LSTM(10, 5, 5);
imported = Network.fromJSON(original.toJSON());
});
var exported = original.toJSON();
var imported = Network.fromJSON(exported);
var learningRate = .5;
var iterations = 1000;
for (var i = 1; i <= iterations; i++)
{
//random input
it('should produce the same output for both networks', function () {
for (var i = 0; i < 1000; i++) {
var input = generateRandomArray(10);
// activate networks
var output1 = original.activate(input);
var output2 = imported.activate(input);
if (i % 100 == 0)
it('should produce the same output for both networks after ' + i + ' iterations', function(){
assert(compare(output1, output2));
});
// random target
var target = generateRandomArray(5);
// propagate networks
original.propagate(learningRate, target);
imported.propagate(learningRate, target);
}
assert.isAtMost(calculateMse(output1, output2), 1e-10,
'output should be same for both networks after ' + i + ' iterations');
}
});
});
describe("Cloned Networks Equivalency", function() {
describe("Cloned Networks Equivalency", function () {
var original;
var cloned;
beforeEach(function () {
original = new LSTM(10, 5, 5);
cloned = Network.fromJSON(original.toJSON());
});
var original = new LSTM(10,5,5);
var cloned = original.clone();
var learningRate = .5;
var iterations = 1000;
for (var i = 1; i <= iterations; i++)
{
//random input
it('should produce the same output for both networks', function () {
for (var i = 0; i < 1000; i++) {
var input = generateRandomArray(10);
// activate networks
var output1 = original.activate(input);
var output2 = cloned.activate(input);
if (i % 100 == 0)
it('should produce the same output for both networks after ' + i + ' iterations', function(){
assert(compare(output1, output2));
});
// random target
var target = generateRandomArray(5);
// propagate networks
original.propagate(learningRate, target);
cloned.propagate(learningRate, target);
}
assert.isAtMost(calculateMse(output1, output2), 1e-10,
'output should be same for both networks after ' + i + ' iterations');
}
});
});
describe("Scheduled Tasks", function() {
describe("Scheduled Tasks", function () {
var perceptron = new Perceptron(2, 3, 1);
it('should stop training at 3000 iterations', function(){
it('should stop training at 3000 iterations', function () {
var final_stats = perceptron.trainer.XOR({
iterations: 3000,
rate: 0.000001,
error: 0.000001,
schedule: {
every: 1000,
do: function(data) {
if( data.iterations == 20000){
return true
}
}
every: 1000,
do: function (data) {
return data.iterations == 20000;
}
}
});
assert.equal( final_stats.iterations, 3000 )
assert.equal(final_stats.iterations, 3000)
});
it('should abort the training at 2000 iterations', function(){
it('should abort the training at 2000 iterations', function () {
var final_stats = perceptron.trainer.XOR({
iterations: 3000,
rate: 0.000001,
error: 0.000001,
schedule: {
every: 1000,
do: function(data) {
if( data.iterations == 2000){
return true
}
}
every: 1000,
do: function (data) {
return data.iterations == 2000;
}
}
});
assert.equal( final_stats.iterations, 2000 )
assert.equal(final_stats.iterations, 2000)
});
it('should work even if schedule.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,
error: 0.000001,
schedule: {
every: 1000,
do: function(data) {}
}
every: 1000,
do: function (data) {}
}
});
assert.equal( final_stats.iterations, 3000 )
assert.equal(final_stats.iterations, 3000)
});
});
describe("Rate Callback Check", 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(){
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) {
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 )
}
do: function (data) {
switch (data.iterations) {
case 1:
case 500:
case 999:
assert.equal(data.rate, 0.01);
break;
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 )
case 1000:
case 1500:
case 2000:
assert.equal(data.rate, 0.005);
break;
}
}
}
@@ -619,39 +570,29 @@ describe("Rate Callback Check", function() {
});
});
describe("Rate Array Check", function() {
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(){
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 )
}
do: function (data) {
switch (data.iterations) {
case 1:
case 500:
case 999:
assert.equal(data.rate, 0.01);
break;
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 )
case 1000:
case 1500:
case 2000:
assert.equal(data.rate, 0.005);
break;
}
}
}