forked from BrainJS/brain.js
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c8a62f1
commit b396559
Showing
3 changed files
with
94 additions
and
83 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,79 +1,68 @@ | ||
import AE from "./autoencoder"; | ||
import AE from './autoencoder'; | ||
|
||
const trainingData = [ | ||
[0, 0, 0], | ||
[0, 1, 1], | ||
[1, 0, 1], | ||
[1, 1, 0] | ||
[1, 1, 0], | ||
]; | ||
|
||
const xornet = new AE<number[], number[]>( | ||
{ | ||
decodedSize: 3, | ||
hiddenLayers: [ 5, 2, 5 ] | ||
} | ||
); | ||
const xornet = new AE<number[], number[]>({ | ||
decodedSize: 3, | ||
hiddenLayers: [5, 2, 5], | ||
}); | ||
|
||
const errorThresh = 0.011; | ||
|
||
const result = xornet.train( | ||
trainingData, { | ||
iterations: 100000, | ||
errorThresh | ||
} | ||
); | ||
|
||
test( | ||
"denoise a data sample", | ||
async () => { | ||
expect(result.error).toBeLessThanOrEqual(errorThresh); | ||
|
||
function xor(...args: number[]) { | ||
return Math.round(xornet.denoise(args)[2]); | ||
} | ||
const result = xornet.train(trainingData, { | ||
iterations: 100000, | ||
errorThresh, | ||
}); | ||
|
||
const run1 = xor(0, 0, 0); | ||
const run2 = xor(0, 1, 1); | ||
const run3 = xor(1, 0, 1); | ||
const run4 = xor(1, 1, 0); | ||
test('denoise a data sample', async () => { | ||
expect(result.error).toBeLessThanOrEqual(errorThresh); | ||
|
||
expect(run1).toBe(0); | ||
expect(run2).toBe(1); | ||
expect(run3).toBe(1); | ||
expect(run4).toBe(0); | ||
function xor(...args: number[]) { | ||
return Math.round(xornet.denoise(args)[2]); | ||
} | ||
); | ||
|
||
test( | ||
"encode and decode a data sample", | ||
async () => { | ||
expect(result.error).toBeLessThanOrEqual(errorThresh); | ||
const run1 = xor(0, 0, 0); | ||
const run2 = xor(0, 1, 1); | ||
const run3 = xor(1, 0, 1); | ||
const run4 = xor(1, 1, 0); | ||
|
||
const run1$input = [0, 0, 0]; | ||
const run1$encoded = xornet.encode(run1$input); | ||
const run1$decoded = xornet.decode(run1$encoded); | ||
expect(run1).toBe(0); | ||
expect(run2).toBe(1); | ||
expect(run3).toBe(1); | ||
expect(run4).toBe(0); | ||
}); | ||
|
||
const run2$input = [0, 1, 1]; | ||
const run2$encoded = xornet.encode(run2$input); | ||
const run2$decoded = xornet.decode(run2$encoded); | ||
test('encode and decode a data sample', async () => { | ||
expect(result.error).toBeLessThanOrEqual(errorThresh); | ||
|
||
for (let i = 0; i < 3; i++) expect(Math.round(run1$decoded[i])).toBe(run1$input[i]); | ||
for (let i = 0; i < 3; i++) expect(Math.round(run2$decoded[i])).toBe(run2$input[i]); | ||
} | ||
); | ||
const run1$input = [0, 0, 0]; | ||
const run1$encoded = xornet.encode(run1$input); | ||
const run1$decoded = xornet.decode(run1$encoded); | ||
|
||
const run2$input = [0, 1, 1]; | ||
const run2$encoded = xornet.encode(run2$input); | ||
const run2$decoded = xornet.decode(run2$encoded); | ||
|
||
test( | ||
"test a data sample for anomalies", | ||
async () => { | ||
expect(result.error).toBeLessThanOrEqual(errorThresh); | ||
for (let i = 0; i < 3; i++) | ||
expect(Math.round(run1$decoded[i])).toBe(run1$input[i]); | ||
for (let i = 0; i < 3; i++) | ||
expect(Math.round(run2$decoded[i])).toBe(run2$input[i]); | ||
}); | ||
|
||
function includesAnomalies(...args: number[]) { | ||
expect(xornet.likelyIncludesAnomalies(args)).toBe(false); | ||
} | ||
test('test a data sample for anomalies', async () => { | ||
expect(result.error).toBeLessThanOrEqual(errorThresh); | ||
|
||
includesAnomalies(0, 0, 0); | ||
includesAnomalies(0, 1, 1); | ||
includesAnomalies(1, 0, 1); | ||
includesAnomalies(1, 1, 0); | ||
function includesAnomalies(...args: number[]) { | ||
expect(xornet.likelyIncludesAnomalies(args)).toBe(false); | ||
} | ||
); | ||
|
||
includesAnomalies(0, 0, 0); | ||
includesAnomalies(0, 1, 1); | ||
includesAnomalies(1, 0, 1); | ||
includesAnomalies(1, 1, 0); | ||
}); |
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,9 @@ | ||
export class UntrainedNeuralNetworkError extends Error { | ||
constructor ( | ||
neuralNetwork: any | ||
) { | ||
super(`Cannot run a ${neuralNetwork.constructor.name} before it is trained.`); | ||
export class UntrainedNeuralNetworkError< | ||
T extends { constructor: { name: string } } | ||
> extends Error { | ||
constructor(neuralNetwork: T) { | ||
super( | ||
`Cannot run a ${neuralNetwork.constructor.name} before it is trained.` | ||
); | ||
} | ||
} |