nn/nn.test.js

53 lines
1.5 KiB
JavaScript

import { assert, assertEquals } from "https://deno.land/std@0.102.0/testing/asserts.ts";
import { Split, Build, Label, Learn, Check } from "./nn.ts";
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]
];
let columns = [2, 3];
let input, output;
let layers = [];
Deno.test("NN.Split", ()=>
{
[input, output] = Split(data, columns);
assert(input);
assert(output);
assertEquals(input.length, output.length, "data split into equal input and output");
console.log(output);
assertEquals(input[0].length, 2, "unpadded input");
assertEquals(output[0].length, 2, "unpadded output");
});
Deno.test("NN.Build", ()=>
{
layers = Build(2, 5, 2);
assertEquals(layers.length, 2, "correct number of matrices");
assertEquals(layers[0][0].length, input[0].length+1, "input: padded input");
assertEquals(layers[0].length, 5, "input: unpadded output");
assertEquals(layers[1][0].length, 6, "hidden: padded input");
assertEquals(layers[1].length, output[0].length, "hidden: unpadded output");
});
Deno.test("NN.Label", ()=>
{
let labels = Label(input, layers);
assertEquals(labels.length, output.length);
assertEquals(labels[0].length, output[0].length);
});
Deno.test("NN.Learn", ()=>
{
let error = Learn(input, layers, output, 1000, 0.1);
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);
});