-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #281 from BrainJS/185-cross-validation-fixes
fix: Fix CrossValidate to have tests for when data too small
- Loading branch information
Showing
14 changed files
with
85 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,5 +31,5 @@ | |
"node_modules", | ||
"test" | ||
], | ||
"version": "1.4.1" | ||
"version": "1.4.2" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ | |
* license: MIT (http://opensource.org/licenses/MIT) | ||
* author: Heather Arthur <[email protected]> | ||
* homepage: https://github.com/brainjs/brain.js#readme | ||
* version: 1.4.1 | ||
* version: 1.4.2 | ||
* | ||
* acorn: | ||
* license: MIT (http://opensource.org/licenses/MIT) | ||
|
@@ -214,8 +214,13 @@ var CrossValidate = function () { | |
|
||
}, { | ||
key: "train", | ||
value: function train(data, trainOpts, k) { | ||
k = k || 4; | ||
value: function train(data) { | ||
var trainOpts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
var k = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 4; | ||
|
||
if (data.length <= k) { | ||
throw new Error("Training set size is too small for " + data.length + " k folds of " + k); | ||
} | ||
var size = data.length / k; | ||
|
||
if (data.constructor === Array) { | ||
|
@@ -1946,8 +1951,8 @@ var NeuralNetwork = function () { | |
falseNeg: falseNeg, | ||
falsePos: falsePos, | ||
total: data.length, | ||
precision: truePos / (truePos + falsePos), | ||
recall: truePos / (truePos + falseNeg), | ||
precision: truePos > 0 ? truePos / (truePos + falsePos) : 0, | ||
recall: truePos > 0 ? truePos / (truePos + falseNeg) : 0, | ||
accuracy: (trueNeg + truePos) / data.length | ||
}); | ||
} | ||
|
Large diffs are not rendered by default.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
{ | ||
"name": "brain.js", | ||
"description": "Neural network library", | ||
"version": "1.4.1", | ||
"version": "1.4.2", | ||
"author": "Heather Arthur <[email protected]>", | ||
"repository": { | ||
"type": "git", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import assert from 'assert'; | ||
import brain from '../../src'; | ||
import CrossValidate from '../../src/cross-validate'; | ||
|
||
describe('CrossValidation', () => { | ||
describe('simple xor example', () => { | ||
it('throws exception when training set is too small', () => { | ||
const xorTrainingData = [ | ||
{ input: [0, 1], output: [1] }, | ||
{ input: [0, 0], output: [0] }, | ||
{ input: [1, 1], output: [0] }, | ||
{ input: [1, 0], output: [1] } | ||
]; | ||
const net = new CrossValidate(brain.NeuralNetwork); | ||
assert.throws(() => { | ||
net.train(xorTrainingData); | ||
}); | ||
}); | ||
it('handles training and outputs values that are all numbers', () => { | ||
const xorTrainingData = [ | ||
{ input: [0, 1], output: [1] }, | ||
{ input: [0, 0], output: [0] }, | ||
{ input: [1, 1], output: [0] }, | ||
{ input: [1, 0], output: [1] }, | ||
|
||
{ input: [0, 1], output: [1] }, | ||
{ input: [0, 0], output: [0] }, | ||
{ input: [1, 1], output: [0] }, | ||
{ input: [1, 0], output: [1] } | ||
]; | ||
const net = new CrossValidate(brain.NeuralNetwork); | ||
net.train(xorTrainingData); | ||
const json = net.toJSON(); | ||
for (let p in json.avgs) { | ||
assert(json.avgs[p] >= 0); | ||
} | ||
for (let p in json.stats) { | ||
assert(json.stats[p] >= 0); | ||
} | ||
}); | ||
}); | ||
}); |