nn/nn.test.js

113 lines
2.6 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 14:51:00 -04:00
import { Split, Forward, Backward } from "./nn.ts";
2021-07-28 14:08:46 -04:00
import { default as M } from "./m.ts";
2021-07-28 20:08:17 -04:00
import { default as Methods } from "./m.ts";
2021-07-28 12:04:00 -04:00
2021-07-28 14:51:49 -04:00
let training = [];
let stages = [];
let layers = [];
2021-07-28 14:08:46 -04:00
2021-07-28 20:08:17 -04:00
let typeA = [
[ 0.1, 0.05],
[ 0.0, -0.06]
];
let typeB = [
[ 0.99, 0.85],
[ 1.2, 1.05]
];
2021-07-29 14:51:00 -04:00
Deno.test("check forward/backward", ()=>
2021-07-28 12:04:00 -04:00
{
2021-07-29 08:42:28 -04:00
let matrix1 = [
[-0.43662948305036675, -0.368590640707799, -0.23227179558890843],
[-0.004292653969505622, 0.38670055222186317, -0.2478421495365568],
[0.738181366836224, 0.3389203747353555, 0.4920200816404332]
];
let matrix2 = [
2021-07-29 10:13:21 -04:00
[0.7098703863463034, 0.35485944251238033, 0.7642849892333241, 0.03046174288491077],
[-0.30655426258144347, 0.45509633551425077, -0.5013795222004322, -0.3421292736637427]
2021-07-28 20:08:17 -04:00
];
2021-07-29 10:13:21 -04:00
let input = [
[ 0.1, 0.05],
[ 0.0, -0.06],
[ 0.99, 0.85],
[ 1.2, 1.05]
2021-07-28 20:08:17 -04:00
];
2021-07-29 10:13:21 -04:00
let output = [
[1, 0],
[1, 0],
[0, 1],
[0, 1]
2021-07-29 08:42:28 -04:00
];
2021-07-28 20:08:17 -04:00
2021-07-29 10:13:21 -04:00
let layers = [matrix1, matrix2];
2021-07-29 14:51:00 -04:00
let stages = [];
for(let i=0; i<1000; i++)
2021-07-29 10:13:21 -04:00
{
2021-07-29 14:51:00 -04:00
stages = Forward(Methods.Mutate.Pad(input), layers);
2021-07-29 10:13:21 -04:00
Backward(stages, layers, output, 0.1);
}
2021-07-29 14:51:00 -04:00
stages = Forward(input, layers);
console.log(stages[stages.length-1]);
});
Deno.test("NN.Split", ()=>
{
let data = [
[3, 2, 1, 0, 1],
[6, 5, 4, 1, 0]
]
let split = Split(data, [3, 4]);
console.log(split);
2021-07-28 20:08:17 -04:00
});
2021-07-29 10:13:21 -04:00
2021-07-28 20:08:17 -04:00
/*
Deno.test("NN.Label", ()=>
{
2021-07-29 10:13:21 -04:00
Label(training, typeA, [1, 0]);
Label(training, typeB, [0, 1]);
2021-07-28 14:08:46 -04:00
assertEquals(training.length, 2, "input and output sets created");
assertEquals(training[0].length, training[1].length, "both sets have same length");
assertEquals(training[0][0].length, 3, "padded input component");
2021-07-29 10:13:21 -04:00
assertEquals(training[1][0].length, 2, "unchanged label vector");
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.Forward", ()=>
2021-07-28 12:04:00 -04:00
{
2021-07-28 20:08:17 -04:00
let layer1 = M.Create.Box([-1, -1, -1], [1, 1, 1], 2);
let layer2 = M.Create.Box([-1, -1, -1], [1, 1, 1], 1);
2021-07-28 14:51:49 -04:00
layers.push(layer1);
layers.push(layer2);
2021-07-28 14:08:46 -04:00
2021-07-29 10:13:21 -04:00
console.log(training[0]);
stages = Forward(training[0], layers);
console.log(stages);
});
Deno.test("NN.Backward", ()=>
{
let copy1 = M.Create.Clone(layers[0]);
let copy2 = M.Create.Clone(layers[1]);
2021-07-28 14:51:49 -04:00
for(let i=0; i<100; i++)
2021-07-28 14:08:46 -04:00
{
2021-07-28 16:40:58 -04:00
Backward(stages, layers, training[1], 0.1);
2021-07-28 14:08:46 -04:00
}
2021-07-28 16:40:58 -04:00
assert(layers[0][0][0] != copy1[0][0], "first matrix has changed");
assert(layers[1][0][0] != copy2[0][0], "second matrix has changed");
2021-07-28 12:04:00 -04:00
});
2021-07-28 14:08:46 -04:00
2021-07-28 16:40:58 -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 10:13:21 -04:00
let stages = Forward(training[0], layers);
console.log(stages[stages.length-1]);
2021-07-28 14:08:46 -04:00
});
2021-07-28 20:08:17 -04:00
*/