From 642e4203a06652d4c61f8ade6233ba57c469838f Mon Sep 17 00:00:00 2001 From: TreetopFlyer Date: Sun, 25 Jul 2021 18:17:54 -0400 Subject: [PATCH] setup --- m.js | 49 +++++++++++++ m.test.js | 107 +++++++++++++++++++++++++++ methods.md | 17 +++++ nn.js | 211 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 384 insertions(+) create mode 100644 m.js create mode 100644 m.test.js create mode 100644 methods.md create mode 100644 nn.js diff --git a/m.js b/m.js new file mode 100644 index 0000000..341dbd4 --- /dev/null +++ b/m.js @@ -0,0 +1,49 @@ +const M = +{ + Iterate: + { + New(inDimensions, inCount, inFunction) + { + let row, i, outputCloud, outputVector; + outputCloud = []; + for(row=0; row M.Iterate.New(inV1.length, inCount, (i, row)=> inV1[i]+(inV2[i]-inV1[i])*Math.random()), + Transpose: (inCloud)=> M.Iterate.New(inCloud.length, inCloud[0].length, (i, row)=> inCloud[i][row]), + Outer: (inV1, inV2)=> M.Iterate.New(inV1.length, inV2.length, (i, row)=> inV1[i]*inV2[row]), + Clone: (inCloud)=> M.Iterate.Old(inCloud, (i, row)=> inCloud[row][i]) + }, + Mutate: + { + Pad: inCloud=> inCloud.forEach(row=> row.push(1)), + Unpad: inCloud=> inCloud.forEach(row=> row.pop()) + }, + Single: + { + Affine: (inV, inMatrix)=> inMatrix.map(row=> row.reduce((sum, current, index)=> sum + current*inV[index])) + }, + Batch: + { + Affine: (inCloud, inMatrix)=> inCloud.map(row=> M.Single.Affine(row, inMatrix)), + Sigmoid: (inCloud)=> M.Iterate.Old(inCloud, i=>1/(1+Math.Pow(Math.E, i))), + Derivative: (inCloud)=> M.Iterate.Old(inCloud, i=>i*(1-i)) + } +} + +export default M; \ No newline at end of file diff --git a/m.test.js b/m.test.js new file mode 100644 index 0000000..dfb4a1d --- /dev/null +++ b/m.test.js @@ -0,0 +1,107 @@ +import { assert, assertEquals } from "https://deno.land/std@0.102.0/testing/asserts.ts"; +import { default as M } from "./m.js"; + +Deno.test("Iterate.New", ()=> +{ + let dimensions = 3; + let count = 4; + let cloud = M.Iterate.New(dimensions, count, (i, j)=>i+j); + assertEquals(cloud.length, count, "correct count"); + assertEquals(cloud[0].length, dimensions, "correct dimensions"); + assertEquals(cloud[0][0], 0); + assertEquals(cloud[3][2], 5, "correct output"); +}); + +Deno.test("Create.Box", ()=> +{ + let min = [-1, -2, -3]; + let max = [1, 2, 3]; + let count = 10; + + let box = M.Create.Box(min, max, count); + assertEquals(box.length, count, "correct count"); + for(let i=0; i= min[j], true); + assert(box[i][j] <= max[j], true, "correct range"); + } + } +}); + +Deno.test("Create.Transpose", ()=> +{ + let v1 = [1, 2, 3]; + let v2 = [4, 5, 6]; + let tpose = M.Create.Transpose([v1, v2]); + assertEquals(tpose.length, 3, "correct count"); + assertEquals(tpose[0].length, 2, "correct dimensions"); + assertEquals(tpose[0][0], v1[0]); + assertEquals(tpose[0][1], v2[0], "correct placement"); +}); + +Deno.test("Create.Outer", ()=> +{ + let v1 = [1, 2, 3]; + let v2 = [4, 5]; + let outer = M.Create.Outer(v1, v2); + assertEquals(outer.length, v2.length, "correct count"); + assertEquals(outer[0].length, v1.length, "correct dimensions"); + assertEquals(outer[1][0], v1[0]*v2[1], "correct placement") +}); + +Deno.test("Create.Clone", ()=> +{ + let v1 = [1, 2, 3]; + let v2 = [4, 5, 6]; + let clone = M.Create.Clone([v1, v2]); + assertEquals(clone.length, 2, "correct count"); + assertEquals(clone[0].length, v1.length, "correct dimensions"); + assertEquals(clone[1][0], v2[0], "correct placement"); +}); + +Deno.test("Mutate.Pad", ()=> +{ + let matrix = [ + [1, 2, 3], + [4, 5, 6] + ]; + M.Mutate.Pad(matrix); + assertEquals(matrix.length, 2, "correct count"); + assertEquals(matrix[0].length, 4, "correct dimensions"); + assertEquals(matrix[0][3], 1, "correct placement"); +}); + +Deno.test("Mutate.Unpad", ()=> +{ + let matrix = [ + [1, 2, 3, 1], + [4, 5, 6, 1] + ]; + M.Mutate.Unpad(matrix); + assertEquals(matrix.length, 2, "correct count"); + assertEquals(matrix[0].length, 3, "correct dimensions"); + assertEquals(matrix[1][0], 4, "correct placement"); +}); + +Deno.test("Single.Affine", ()=> +{ + let v = [1, 2]; + let m = [[0.1, 0.2], [0.3, 0.4]]; + let t = M.Single.Affine(v, m); + assertEquals(t.length, 2, "correct dimensions"); + assertEquals(t[0], 0.5) + assertEquals(t[1], 1.1, "correct placement"); +}); + +Deno.test("Batch.Affine", ()=> +{ + let c = [[1, 2], [3, 4]]; + let m = [[0.1, 0.2], [0.3, 0.4]]; + let t = M.Batch.Affine(c, m); + assertEquals(t.length, 2, "correct count"); + assertEquals(t[0].length, 2, "correct dimensions") + assertEquals(t[0][1], 1.1, "correct placement"); +}); diff --git a/methods.md b/methods.md new file mode 100644 index 0000000..2c4f9bc --- /dev/null +++ b/methods.md @@ -0,0 +1,17 @@ +box(boundingBox, count) // done +transpose(inMatrix) // done +outer(inv1, inv2) // done +clone(inCloud) // done + +pad(inCloud) // done +unpad(inCloud) // done + +// batch filter +transform(inCloud, inMatrix) // done +sigmoid(inCloud) // 1/(1+e^x) // +derivative(inCloud) // x*(1-x) +scale(inCloud1, inV) + +// batch of pairs +subtract(inCloud1, inCloud2) +multiply(inCloud1, inCloud2) \ No newline at end of file diff --git a/nn.js b/nn.js new file mode 100644 index 0000000..262aec3 --- /dev/null +++ b/nn.js @@ -0,0 +1,211 @@ +var NN = {}; + +NN.TrainingSet = {}; +NN.TrainingSet.Instances = []; +NN.TrainingSet.Create = function() +{ + var obj = {}; + + obj.Input = []; + obj.Output = []; + obj.Order = []; + + NN.TrainingSet.Instances.push(obj); + return obj; +}; +NN.TrainingSet.AddPoint = function(inTrainingSet, inType, inData) +{ + inTrainingSet.Input.push(inData); + inTrainingSet.Output.push(inType); + inTrainingSet.Order.push(inTrainingSet.Order.length); +}; +NN.TrainingSet.AddCloud = function(inTrainingSet, inLabel, inCloud) +{ + var i; + for(i=0; i=0; i--) + { + input = NN.Layer.Backward(inNetwork.Layers[i], input); + NN.Layer.Adjust(inNetwork.Layers[i], inNetwork.LearningRate); + } +}; + + +NN.Network.Batch = function(inNetwork, inTrainingSet, inIterations) +{ + var i; + for(i=0; i