nn/nn.test.js

53 lines
1.5 KiB
JavaScript
Raw Normal View History

2021-07-28 12:04:00 -04:00
import { assert, assertEquals } from "https://deno.land/std@0.102.0/testing/asserts.ts";
2021-07-29 16:15:05 -04:00
import { Split, Build, Label, Learn, Check } from "./nn.ts";
2021-07-28 12:04:00 -04:00
2021-07-29 16:15:05 -04:00
let data = [
[ 0.10, 0.05, 0, 1],
[ 0.00, -0.06, 0, 1],
[ 0.99, 0.85, 1, 0],
[ 1.20, 1.05, 1, 0]
2021-07-28 20:08:17 -04:00
];
2021-07-29 16:15:05 -04:00
let columns = [2, 3];
let input, output;
let layers = [];
2021-07-29 14:51:00 -04:00
Deno.test("NN.Split", ()=>
{
2021-07-29 16:15:05 -04:00
[input, output] = Split(data, columns);
assert(input);
assert(output);
assertEquals(input.length, output.length, "data split into equal input and output");
2021-07-29 10:13:21 -04:00
2021-07-29 20:53:31 -04:00
assertEquals(input[0].length, 2, "unpadded input");
2021-07-29 16:15:05 -04:00
assertEquals(output[0].length, 2, "unpadded output");
2021-07-28 12:04:00 -04:00
});
2021-07-28 14:08:46 -04:00
2021-07-29 16:15:05 -04:00
Deno.test("NN.Build", ()=>
2021-07-28 12:04:00 -04:00
{
2021-07-29 16:15:05 -04:00
layers = Build(2, 5, 2);
2021-07-28 14:08:46 -04:00
2021-07-29 16:15:05 -04:00
assertEquals(layers.length, 2, "correct number of matrices");
2021-07-29 20:53:31 -04:00
assertEquals(layers[0][0].length, input[0].length+1, "input: padded input");
2021-07-29 16:15:05 -04:00
assertEquals(layers[0].length, 5, "input: unpadded output");
2021-07-29 10:13:21 -04:00
2021-07-29 16:15:05 -04:00
assertEquals(layers[1][0].length, 6, "hidden: padded input");
assertEquals(layers[1].length, output[0].length, "hidden: unpadded output");
2021-07-28 12:04:00 -04:00
});
2021-07-28 14:08:46 -04:00
2021-07-29 10:13:21 -04:00
Deno.test("NN.Label", ()=>
2021-07-28 14:08:46 -04:00
{
2021-07-29 16:15:05 -04:00
let labels = Label(input, layers);
assertEquals(labels.length, output.length);
assertEquals(labels[0].length, output[0].length);
2021-07-28 14:08:46 -04:00
});
2021-07-29 16:15:05 -04:00
Deno.test("NN.Learn", ()=>
{
2021-07-30 23:28:25 -04:00
let error = Learn(input, layers, output, 50, 0.2);
console.log(error);
2021-07-29 16:15:05 -04:00
assertEquals(error.length, output.length);
let total = 0;
let count = error.length*error[0].length;
error.forEach(row=> row.forEach(component=> total+=Math.abs(component)));
assert(total/count < 0.3);
});