move padding into learn/label

This commit is contained in:
TreetopFlyer 2021-07-29 20:53:31 -04:00
parent 94c7abdda5
commit b1c95d4819
4 changed files with 20 additions and 9 deletions

11
index.js Normal file
View File

@ -0,0 +1,11 @@
import { Build, Learn, Label } from "./nn.ts";
const inputs = [[0.10, 0.50], [0.00, 0.06], [0.99, 0.85], [0.80, 0.95]];
const labels = [[ 0, 1 ], [ 0, 1 ], [ 1, 0 ], [ 1, 0 ]];
const layers = Build(2, 5, 2);
const errors = Learn(inputs, layers, labels, 1000, 0.1);
const output = Label(inputs, layers);
console.log("error after training:", errors);
console.log("re-classified inputs:", output);

3
m.ts
View File

@ -31,7 +31,8 @@ const Methods = {
Box: (inV1:Cloud.V, inV2:Cloud.V, inCount:number):Cloud.M=> Methods.Iterate.Loop(inV1.length, inCount, i=> inV1[i]+(inV2[i]-inV1[i])*Math.random()), Box: (inV1:Cloud.V, inV2:Cloud.V, inCount:number):Cloud.M=> Methods.Iterate.Loop(inV1.length, inCount, i=> inV1[i]+(inV2[i]-inV1[i])*Math.random()),
Transpose: (inCloud:Cloud.M):Cloud.M=> Methods.Iterate.Loop(inCloud.length, inCloud[0].length, (i, row)=> inCloud[i][row]), Transpose: (inCloud:Cloud.M):Cloud.M=> Methods.Iterate.Loop(inCloud.length, inCloud[0].length, (i, row)=> inCloud[i][row]),
Outer: (inV1:Cloud.V, inV2:Cloud.V):Cloud.M=> Methods.Iterate.Loop(inV1.length, inV2.length, (i, row)=> inV1[i]*inV2[row]), Outer: (inV1:Cloud.V, inV2:Cloud.V):Cloud.M=> Methods.Iterate.Loop(inV1.length, inV2.length, (i, row)=> inV1[i]*inV2[row]),
Clone: (inCloud:Cloud.M):Cloud.M=> Methods.Iterate.Edit(inCloud, i=> i) Clone: (inCloud:Cloud.M):Cloud.M=> Methods.Iterate.Edit(inCloud, i=> i),
Padded: (inCloud:Cloud.M):Cloud.M=> inCloud.map((row:Cloud.V)=> [...row, 1])
}, },
Mutate: Mutate:
{ {

View File

@ -17,8 +17,9 @@ Deno.test("NN.Split", ()=>
assert(input); assert(input);
assert(output); assert(output);
assertEquals(input.length, output.length, "data split into equal input and output"); assertEquals(input.length, output.length, "data split into equal input and output");
console.log(output);
assertEquals(input[0].length, 3, "padded input"); assertEquals(input[0].length, 2, "unpadded input");
assertEquals(output[0].length, 2, "unpadded output"); assertEquals(output[0].length, 2, "unpadded output");
}); });
@ -27,7 +28,7 @@ Deno.test("NN.Build", ()=>
layers = Build(2, 5, 2); layers = Build(2, 5, 2);
assertEquals(layers.length, 2, "correct number of matrices"); assertEquals(layers.length, 2, "correct number of matrices");
assertEquals(layers[0][0].length, input[0].length, "input: padded input"); assertEquals(layers[0][0].length, input[0].length+1, "input: padded input");
assertEquals(layers[0].length, 5, "input: unpadded output"); assertEquals(layers[0].length, 5, "input: unpadded output");
assertEquals(layers[1][0].length, 6, "hidden: padded input"); assertEquals(layers[1][0].length, 6, "hidden: padded input");

10
nn.ts
View File

@ -40,10 +40,8 @@ const Split = (inTrainingSet:Cloud.M, inHeaderLabel:Cloud.V, inHeaderKeep:Cloud.
} }
inTrainingSet.forEach((row:Cloud.V):void => inTrainingSet.forEach((row:Cloud.V):void =>
{ {
let vectorData = [ ...inHeaderKeep.map((i:number)=>row[i]), 1]; data.push( inHeaderKeep.map((i:number)=>row[i]) );
let vectorLabel = inHeaderLabel.map((i:number)=>row[i]) label.push( inHeaderLabel.map((i:number)=>row[i]) );
data.push( vectorData );
label.push( vectorLabel );
}); });
return [ data, label ]; return [ data, label ];
}; };
@ -60,7 +58,7 @@ const Build = (...inLayers:Array<number>):N =>
}; };
const Label = (inData:Cloud.M, inLayers:N):Cloud.M => const Label = (inData:Cloud.M, inLayers:N):Cloud.M =>
{ {
let stages:N = Forward(inData, inLayers); let stages:N = Forward(M.Create.Padded(inData), inLayers);
return stages[stages.length-1]; return stages[stages.length-1];
}; };
const Learn = (inData:Cloud.M, inLayers:N, inLabels:Cloud.M, inIterations:number, inRate:number):Cloud.M => const Learn = (inData:Cloud.M, inLayers:N, inLabels:Cloud.M, inIterations:number, inRate:number):Cloud.M =>
@ -68,7 +66,7 @@ const Learn = (inData:Cloud.M, inLayers:N, inLabels:Cloud.M, inIterations:number
let stages:N = []; let stages:N = [];
for(let i=0; i<inIterations; i++) for(let i=0; i<inIterations; i++)
{ {
stages = Forward(inData, inLayers); stages = Forward(M.Create.Padded(inData), inLayers);
Backward(stages, inLayers, inLabels, inRate); Backward(stages, inLayers, inLabels, inRate);
} }
return M.Batch.Subtract(stages[stages.length-1], inLabels); return M.Batch.Subtract(stages[stages.length-1], inLabels);