diff --git a/index.js b/index.js new file mode 100644 index 0000000..6055b66 --- /dev/null +++ b/index.js @@ -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); \ No newline at end of file diff --git a/m.ts b/m.ts index a6882d2..82ce151 100644 --- a/m.ts +++ b/m.ts @@ -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()), 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]), - 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: { diff --git a/nn.test.js b/nn.test.js index 6519caf..5a4c01e 100644 --- a/nn.test.js +++ b/nn.test.js @@ -17,8 +17,9 @@ Deno.test("NN.Split", ()=> assert(input); assert(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"); }); @@ -27,7 +28,7 @@ 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, "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[1][0].length, 6, "hidden: padded input"); diff --git a/nn.ts b/nn.ts index a09e8a1..351d44b 100644 --- a/nn.ts +++ b/nn.ts @@ -40,10 +40,8 @@ const Split = (inTrainingSet:Cloud.M, inHeaderLabel:Cloud.V, inHeaderKeep:Cloud. } inTrainingSet.forEach((row:Cloud.V):void => { - let vectorData = [ ...inHeaderKeep.map((i:number)=>row[i]), 1]; - let vectorLabel = inHeaderLabel.map((i:number)=>row[i]) - data.push( vectorData ); - label.push( vectorLabel ); + data.push( inHeaderKeep.map((i:number)=>row[i]) ); + label.push( inHeaderLabel.map((i:number)=>row[i]) ); }); return [ data, label ]; }; @@ -60,7 +58,7 @@ const Build = (...inLayers:Array):N => }; 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]; }; 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 = []; for(let i=0; i