Skip to content

Commit 6854435

Browse files
committed
Loggity
Simplified log test, and added in the ability to pass in custom log method. I thought this was outside of the scope of my pr but just kinda did it because the test output bugged me lol.
1 parent a781ba8 commit 6854435

File tree

2 files changed

+28
-17
lines changed

2 files changed

+28
-17
lines changed

src/neural-network.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,21 @@ export default class NeuralNetwork {
246246
return sizes;
247247
}
248248

249+
250+
/**
251+
*
252+
* @param log
253+
* if true passed in console.log is used
254+
* if a method is passed in method is used
255+
* if false passed in nothing is logged
256+
* @returns error
257+
*/
258+
_setLogMethod (log) {
259+
if (typeof log === 'function') return log;
260+
if (log) return console.log;
261+
return false;
262+
}
263+
249264
/**
250265
*
251266
* @param data
@@ -270,7 +285,7 @@ export default class NeuralNetwork {
270285
status.error = this._calculateTrainingError(data, options.learningRate);
271286

272287
if (options.log && (status.iterations % options.logPeriod === 0)) {
273-
console.log (`iterations: ${status.iterations}, training error: ${status.error}`);
288+
options.log (`iterations: ${status.iterations}, training error: ${status.error}`);
274289
}
275290

276291
if (options.callback && (status.iterations % options.callbackPeriod === 0)) {
@@ -287,6 +302,7 @@ export default class NeuralNetwork {
287302
train (data, _options = {}) {
288303
let options = Object.assign({}, this.constructor.trainDefaults, _options);
289304
data = this.formatData(data);
305+
options.log = this._setLogMethod(options.log);
290306
options.learningRate = _options.learningRate || this.learningRate || options.learningRate;
291307
let endTime = Date.now() + options.trainTimeMs;
292308
var status = {
@@ -315,6 +331,7 @@ export default class NeuralNetwork {
315331
return new Promise ((resolve, reject) => {
316332
let options = Object.assign({}, this.constructor.trainDefaults, _options);
317333
data = this.formatData(data);
334+
options.log = this._setLogMethod(options.log);
318335
options.learningRate = _options.learningRate || this.learningRate || options.learningRate;
319336
let endTime = Date.now() + options.trainTimeMs;
320337

test/base/options.js

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -131,20 +131,12 @@ describe ('async neural network options', () => {
131131

132132

133133
describe('log', () => {
134-
let logCalled;
135-
let oldLog;
134+
let logCalled = false;
136135

137136
beforeEach (() => { logCalled = false; });
138137

139-
before (() => { oldLog = console.log; console.log = logFunction; });
140-
after (() => { console.log = oldLog; });
141-
142138
function logFunction(str) {
143-
if (typeof str === "string" && !str.includes('iterations:') && !str.includes('training error:')) {
144-
oldLog(str);
145-
} else {
146-
logCalled = true;
147-
}
139+
logCalled = true;
148140
}
149141

150142
function trainWithLog (log, expected) {
@@ -159,7 +151,10 @@ describe('log', () => {
159151
function trainWithLogAsync (log, expected, done) {
160152
let net = new brain.NeuralNetwork();
161153
net
162-
.trainAsync ([ {input: [0], output: [0]} ], { log: log, logPeriod: 1, iterations: 1 })
154+
.trainAsync (
155+
[ {input: [0], output: [0]} ],
156+
{ log: log, logPeriod: 1, iterations: 1 }
157+
)
163158
.then (res => {
164159
assert.equal (logCalled, expected);
165160
done ();
@@ -169,10 +164,9 @@ describe('log', () => {
169164
});
170165
}
171166

172-
// TODO fix this test :(
173-
it('should call console.log if log === true', () => { trainWithLog (true, true); });
174-
it('should call console.log if log === false', () => { trainWithLog (false, false); });
167+
it('should call log method', () => { trainWithLog (logFunction, true); });
168+
it('should not call log method', () => { trainWithLog (false, false); });
175169

176-
it('ASYNC should call console.log if log === true', done => { trainWithLogAsync (true, true, done); }).timeout(5000);
177-
it('ASYNC should call console.log if log === false', done => { trainWithLogAsync (false, false, done); }).timeout(5000);
170+
it('ASYNC should call log method', done => { trainWithLogAsync (logFunction, true, done); }).timeout(5000);
171+
it('ASYNC should not call log method', done => { trainWithLogAsync (false, false, done); }).timeout(5000);
178172
});

0 commit comments

Comments
 (0)