Skip to content

Commit 9da27e7

Browse files
authored
Merge pull request #155 from BrainJS/154-testing-times
# If applied, this commit will make life better for everyone
2 parents f0097ee + 1724805 commit 9da27e7

File tree

4 files changed

+82
-217
lines changed

4 files changed

+82
-217
lines changed

test/base/bitwise.js

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -67,38 +67,4 @@ describe('bitwise functions sync training', () => {
6767
{input: [1, 1], output: [1]}];
6868
testBitwise(and, 'and');
6969
});
70-
});
71-
72-
describe('bitwise functions async training', () => {
73-
74-
it('NOT function', (done) => {
75-
let not = [{input: [0], output: [1]},
76-
{input: [1], output: [0]}];
77-
testBitwiseAsync(not, 'not', done);
78-
}).timeout(10000);
79-
80-
it('XOR function', (done) => {
81-
let xor = [{input: [0, 0], output: [0]},
82-
{input: [0, 1], output: [1]},
83-
{input: [1, 0], output: [1]},
84-
{input: [1, 1], output: [0]}];
85-
testBitwiseAsync(xor, 'xor', done);
86-
}).timeout(10000);
87-
88-
it('OR function', (done) => {
89-
let or = [{input: [0, 0], output: [0]},
90-
{input: [0, 1], output: [1]},
91-
{input: [1, 0], output: [1]},
92-
{input: [1, 1], output: [1]}];
93-
testBitwiseAsync(or, 'or', done);
94-
}).timeout(10000);
95-
96-
it('AND function', (done) => {
97-
let and = [{input: [0, 0], output: [0]},
98-
{input: [0, 1], output: [0]},
99-
{input: [1, 0], output: [0]},
100-
{input: [1, 1], output: [1]}];
101-
testBitwiseAsync(and, 'and', done);
102-
}).timeout(10000);
103-
});
104-
70+
});

test/base/hash.js

Lines changed: 1 addition & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -80,96 +80,4 @@ describe('hash input and output', () => {
8080
let output = net.run({x: 1, z: 1});
8181
assert.ok(output.answer > 0.9);
8282
});
83-
});
84-
85-
describe('async hash input and output', () => {
86-
it('runs correctly with array input and output', (done) => {
87-
let net = new brain.NeuralNetwork();
88-
net.trainAsync([
89-
{ input: [0, 0], output: [0] },
90-
{ input: [0, 1], output: [1] },
91-
{ input: [1, 0], output: [1] },
92-
{ input: [1, 1], output: [0] }])
93-
.then(_ => {
94-
let output = net.run([1, 0]);
95-
assert.ok(output[0] > 0.9, 'output: ' + output[0]);
96-
done();
97-
});
98-
}).timeout(15000);
99-
100-
it('runs correctly with hash input', (done) => {
101-
let net = new brain.NeuralNetwork();
102-
103-
net.trainAsync([
104-
{ input: { x: 0, y: 0 }, output: [0] },
105-
{ input: { x: 0, y: 1 }, output: [1] },
106-
{ input: { x: 1, y: 0 }, output: [1] },
107-
{ input: { x: 1, y: 1 }, output: [0] }
108-
])
109-
.then(() => {
110-
let output = net.run({x: 1, y: 0});
111-
assert.ok(output[0] > 0.9, 'output: ' + output[0]);
112-
done();
113-
});
114-
}).timeout(15000);
115-
116-
it('runs correctly with hash output', (done) => {
117-
let net = new brain.NeuralNetwork();
118-
net.trainAsync([
119-
{ input: [0, 0], output: { answer: 0 } },
120-
{ input: [0, 1], output: { answer: 1 } },
121-
{ input: [1, 0], output: { answer: 1 } },
122-
{ input: [1, 1], output: { answer: 0 } }
123-
])
124-
.then(() => {
125-
let output = net.run([1, 0]);
126-
assert.ok(output.answer > 0.9, 'output: ' + output.answer);
127-
done();
128-
});
129-
}).timeout(15000);
130-
131-
it('runs correctly with hash input and output', (done) => {
132-
let net = new brain.NeuralNetwork();
133-
net.trainAsync([
134-
{ input: { x: 0, y: 0 }, output: { answer: 0 } },
135-
{ input: { x: 0, y: 1 }, output: { answer: 1 } },
136-
{ input: { x: 1, y: 0 }, output: { answer: 1 } },
137-
{ input: { x: 1, y: 1 }, output: { answer: 0 } }
138-
])
139-
.then(() => {
140-
let output = net.run({x: 1, y: 0});
141-
assert.ok(output.answer > 0.9, 'output: ' + output.answer);
142-
done();
143-
});
144-
}).timeout(15000);
145-
146-
it('runs correctly with sparse hashes', (done) => {
147-
let net = new brain.NeuralNetwork();
148-
net.trainAsync ([
149-
{ input: {}, output: {} },
150-
{ input: { y: 1 }, output: { answer: 1 } },
151-
{ input: { x: 1 }, output: { answer: 1 } },
152-
{ input: { x: 1, y: 1 }, output: {} }
153-
])
154-
.then(() => {
155-
let output = net.run({x: 1});
156-
assert.ok(output.answer > 0.9);
157-
done();
158-
});
159-
}).timeout(15000);
160-
161-
it('runs correctly with unseen input', (done) => {
162-
let net = new brain.NeuralNetwork();
163-
net.trainAsync ([
164-
{ input: {}, output: {} },
165-
{ input: { y: 1 }, output: { answer: 1 } },
166-
{ input: { x: 1 }, output: { answer: 1 } },
167-
{ input: { x: 1, y: 1 }, output: {} }
168-
])
169-
.then(() => {
170-
let output = net.run({x: 1, z: 1});
171-
assert.ok(output.answer > 0.9);
172-
done();
173-
});
174-
}).timeout(15000);
175-
});
83+
});

test/base/log.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,4 @@ describe('log', () => {
3535

3636
it('should call log method', () => { trainWithLog(logFunction, true); });
3737
it('should not call log method', () => { trainWithLog(false, false); });
38-
39-
it('ASYNC should call log method', done => { trainWithLogAsync(logFunction, true, done); }).timeout(5000);
40-
it('ASYNC should not call log method', done => { trainWithLogAsync(false, false, done); }).timeout(5000);
4138
});

test/base/trainopts.js

Lines changed: 80 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import assert from 'assert';
22
import brain from '../../src';
3+
import sinon from 'sinon'
34

45
let data = [{input: [0, 0], output: [0]},
56
{input: [0, 1], output: [1]},
@@ -74,102 +75,95 @@ describe('train() options', () => {
7475
});
7576
});
7677

77-
78-
describe('async train() options', () => {
79-
it('train until error threshold reached', (done) => {
80-
let net = new brain.NeuralNetwork();
81-
let p1 = net
82-
.trainAsync(data, { errorThresh: 0.2 })
83-
.then (res => {
84-
assert.ok(res.error < 0.2, `[res.error, ${res.error}] should have been less then 0.2`);
78+
describe('train() and trainAsync() use the same private methods', () => {
79+
let trainingData = [{ input: [0, 0], output: [0] }];
80+
let opts = { iterations:1 };
81+
let net = new brain.NeuralNetwork();
82+
let methodsChecked = [
83+
'_prepTraining',
84+
'_updateTrainingOptions',
85+
'_formatData',
86+
'_verifyIsInitialized',
87+
'_trainingTick'
88+
];
89+
90+
beforeEach(() => { methodsChecked.forEach(m => sinon.spy(net, m)); })
91+
afterEach(() => { methodsChecked.forEach(m => net[m].restore()); })
92+
93+
it('_prepTraining()', (done) => {
94+
net.train(trainingData, opts);
95+
assert(net._prepTraining.calledOnce, `_prepTraining was expected to be called once but was called ${net._prepTraining.callCount}`);
96+
net
97+
.trainAsync(trainingData, opts)
98+
.then(() => {
99+
assert(net._prepTraining.calledTwice, `_prepTraining was expected to be called twice but was called ${net._prepTraining.callCount}`);
85100
done();
86101
})
87-
.catch(err => { assert.ok(false, err.toString()) });
88-
}).timeout(10000);
102+
.catch(e => {
103+
assert.ok(false, e.toString());
104+
done()
105+
});
106+
});
89107

90-
it('train until max iterations reached', (done) => {
91-
let net = new brain.NeuralNetwork();
92-
let res = net
93-
.trainAsync(data, { iterations: 25 })
94-
.then(res => {
95-
assert.equal(res.iterations, 25, `[res.iterations, ${res.iterations}] should have been less then 25`);
108+
it('_updateTrainingOptions()', (done) => {
109+
net.train(trainingData, opts);
110+
assert(net._updateTrainingOptions.calledOnce, `_updateTrainingOptions was expected to be called once but was called ${net._updateTrainingOptions.callCount}`);
111+
net
112+
.trainAsync(trainingData, opts)
113+
.then(() => {
114+
assert(net._updateTrainingOptions.calledTwice, `_updateTrainingOptions was expected to be called twice but was called ${net._prepTraining.callCount}`);
96115
done();
97116
})
98-
.catch(err => { assert.ok(false, err.toString()) });
99-
}).timeout(10000);
100-
101-
it('asyinc training callback called with training stats', (done) => {
102-
let iters = 100;
103-
let period = 20;
104-
let target = iters / period;
105-
106-
let calls = 0;
107-
108-
let net = new brain.NeuralNetwork();
109-
net.trainAsync(data, {
110-
iterations: iters,
111-
callbackPeriod: period,
112-
callback: (res) => {
113-
assert.ok(res.iterations % period == 0);
114-
calls++;
115-
}
116-
})
117-
.then (res => {
118-
assert.ok(target === calls, `[calls, ${calls}] should be the same as [target, ${target}]`);
119-
done();
120-
})
121-
.catch(err => { assert.ok(false, err.toString()) });
122-
}).timeout(10000);
123-
124-
it('learningRate ASYNC - higher learning rate should train faster', (done) => {
125-
let data = [
126-
{ input: [0, 0], output: [0] },
127-
{ input: [0, 1], output: [1] },
128-
{ input: [1, 0], output: [1] },
129-
{ input: [1, 1], output: [1] }
130-
];
131-
132-
let net = new brain.NeuralNetwork();
133-
let net2 = new brain.NeuralNetwork();
134-
135-
let p1 = net.trainAsync(data, { learningRate: 0.5 });
136-
let p2 = net2.trainAsync(data, { learningRate: 0.8 });
117+
.catch(e => {
118+
assert.ok(false, e.toString());
119+
done()
120+
});
121+
});
137122

138-
Promise
139-
.all([p1, p2])
140-
.then(values => {
141-
let res = values[0];
142-
let res2 = values[1];
143-
assert.ok(res.iterations > (res2.iterations * 1.1), `${res.iterations} !> ${res2.iterations * 1.1}`);
123+
it('_formatData()', (done) => {
124+
net.train(trainingData, opts);
125+
assert(net._formatData.calledOnce, `_formatData was expected to be called once but was called ${net._formatData.callCount}`);
126+
net
127+
.trainAsync(trainingData, opts)
128+
.then(() => {
129+
assert(net._formatData.calledTwice, `_formatData was expected to be called twice but was called ${net._prepTraining.callCount}`);
144130
done();
145131
})
146-
.catch(err => {
147-
assert.ok(false, err.toString())
132+
.catch(e => {
133+
assert.ok(false, e.toString());
134+
done()
148135
});
149-
}).timeout(10000);
150-
151-
it('momentum ASYNC - higher momentum should train faster', (done) => {
152-
let data = [
153-
{ input: [0, 0], output: [0] },
154-
{ input: [0, 1], output: [1] },
155-
{ input: [1, 0], output: [1] },
156-
{ input: [1, 1], output: [1] }
157-
];
158-
159-
let net = new brain.NeuralNetwork({ momentum: 0.1 });
160-
let net2 = new brain.NeuralNetwork({ momentum: 0.5 });
136+
});
161137

162-
let p1 = net.trainAsync(data);
163-
let p2 = net2.trainAsync(data);
138+
it('_verifyIsInitialized()', (done) => {
139+
net.train(trainingData, opts);
140+
assert(net._verifyIsInitialized.calledOnce, `_verifyIsInitialized was expected to be called once but was called ${net._verifyIsInitialized.callCount}`);
141+
net
142+
.trainAsync(trainingData, opts)
143+
.then(() => {
144+
assert(net._verifyIsInitialized.calledTwice, `_verifyIsInitialized was expected to be called twice but was called ${net._prepTraining.callCount}`);
145+
done();
146+
})
147+
.catch(e => {
148+
assert.ok(false, e.toString());
149+
done()
150+
});
151+
});
164152

165-
Promise.all([p1, p2])
166-
.then(values => {
167-
let res = values[0];
168-
let res2 = values[1];
169-
assert.ok(res.iterations > (res2.iterations * 1.1), `${res.iterations} !> ${res2.iterations * 1.1}`);
153+
it('_trainingTick()', (done) => {
154+
net.train(trainingData, opts);
155+
// The loop calls _trainingTick twice and returns imidiatly on second call
156+
assert(net._trainingTick.calledTwice, `_trainingTick was expected to be called twice but was called ${net._prepTraining.callCount}`);
157+
net
158+
.trainAsync(trainingData, opts)
159+
.then(() => {
160+
// trainAsync only calls _trainingTick once
161+
assert(net._trainingTick.calledThrice, `_trainingTick was expected to be called thrice but was called ${net._prepTraining.callCount}`);
170162
done();
171-
}).catch(err => {
172-
assert.ok(false, err.toString())
163+
})
164+
.catch(e => {
165+
assert.ok(false, e.toString());
166+
done()
173167
});
174-
}).timeout(10000);
175-
});
168+
});
169+
});

0 commit comments

Comments
 (0)