diff --git a/m.js b/m.js index e2914fb..1a85dac 100644 --- a/m.js +++ b/m.js @@ -2,7 +2,7 @@ const M = { Iterate: { - New(inDimensions, inCount, inFunction) + Loop: (inDimensions, inCount, inFunction)=> { let row, i, outputCloud, outputVector; outputCloud = []; @@ -17,17 +17,14 @@ const M = } return outputCloud; }, - Old(inCloud, inFunction) - { - return M.Iterate.New(inCloud[0].length, inCloud.length, inFunction); - } + Edit: (inCloud, inFunction)=> inCloud.map(row=>row.map(inFunction)) }, Create: { - Box: (inV1, inV2, inCount)=> 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]) + Box: (inV1, inV2, inCount)=> M.Iterate.Loop(inV1.length, inCount, (i, row)=> inV1[i]+(inV2[i]-inV1[i])*Math.random()), + Transpose: (inCloud)=> M.Iterate.Loop(inCloud.length, inCloud[0].length, (i, row)=> inCloud[i][row]), + Outer: (inV1, inV2)=> M.Iterate.Loop(inV1.length, inV2.length, (i, row)=> inV1[i]*inV2[row]), + Clone: (inCloud)=> M.Iterate.Edit(inCloud, i=> i) }, Mutate: { @@ -45,9 +42,9 @@ const M = Subtract: (inCloud1, inCloud2)=> inCloud1.map((row, rowIndex)=> M.Single.Subtract(row, inCloud2[rowIndex])), Multiply: (inCloud1, inCloud2)=> inCloud1.map((row, rowIndex)=> M.Single.Multiply(row, inCloud2[rowIndex])), 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)), - Scale: (inCloud, inScalar)=> M.Iterate.Old(inCloud, i=>i*inScalar) + Sigmoid: (inCloud)=> M.Iterate.Edit(inCloud, i=>1/(1+Math.pow(Math.E, -i))), + Derivative: (inCloud)=> M.Iterate.Edit(inCloud, i=>i*(1-i)), + Scale: (inCloud, inScalar)=> M.Iterate.Edit(inCloud, i=>i*inScalar) } } diff --git a/m.test.js b/m.test.js index 823298d..2bbab28 100644 --- a/m.test.js +++ b/m.test.js @@ -1,16 +1,25 @@ 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", ()=> +Deno.test("Iterate.Loop", ()=> { let dimensions = 3; let count = 4; - let cloud = M.Iterate.New(dimensions, count, (i, j)=>i+j); + let cloud = M.Iterate.Loop(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("Iterate.Edit", ()=> +{ + let c = [[1, 2], [3, 4]] + let t = M.Iterate.Edit(c, (i)=>i); + assertEquals(t.length, c.length, "correct count"); + assertEquals(t[0][0], c[0][0], "correct dimensions"); + assertEquals(t[1][1], c[1][1], "correct placement"); +}); + Deno.test("Create.Box", ()=> { @@ -30,7 +39,6 @@ Deno.test("Create.Box", ()=> } } }); - Deno.test("Create.Transpose", ()=> { let v1 = [1, 2, 3]; @@ -41,7 +49,6 @@ Deno.test("Create.Transpose", ()=> assertEquals(tpose[0][0], v1[0]); assertEquals(tpose[0][1], v2[0], "correct placement"); }); - Deno.test("Create.Outer", ()=> { let v1 = [1, 2, 3]; @@ -51,7 +58,6 @@ Deno.test("Create.Outer", ()=> 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]; @@ -62,6 +68,7 @@ Deno.test("Create.Clone", ()=> assertEquals(clone[1][0], v2[0], "correct placement"); }); + Deno.test("Mutate.Pad", ()=> { let matrix = [ @@ -133,8 +140,28 @@ Deno.test("Batch.Scale", ()=> let c = [[1, 2], [3, 4]]; let s = 0.5; let t = M.Batch.Scale(c, s); + console.log(t); assertEquals(t.length, 2, "correct count"); assertEquals(t[0].length, 2, "correct dimensions"); - console.log(t); assertEquals(t[1][0], 1.5, "correct placement"); }); + + +Deno.test("Batch.Sigmoid", ()=> +{ + let m = [[-1000, 1000]]; + let t = M.Batch.Sigmoid(m); + console.log(t); + assertEquals(t.length, 1, "correct count"); + assertEquals(t[0].length, 2, "correct dimensions"); + assert(t[0][0]>=0 && t[0][0]<0.5); + assert(t[0][1]<=1 && t[0][1]>0.5, "correct placement"); +}); +Deno.test("Batch.Derivative", ()=> +{ + let m = [[-1000, 0, 1000]]; + let t = M.Batch.Derivative(M.Batch.Sigmoid(m)); + assertEquals(t.length, 1, "correct count"); + assertEquals(t[0].length, 3, "correct dimensions"); + assert(t[0][0]t[0][2]); +}); \ No newline at end of file diff --git a/methods.md b/methods.md index b149d13..e365088 100644 --- a/methods.md +++ b/methods.md @@ -7,8 +7,8 @@ pad(inCloud) // done unpad(inCloud) // done transform(inCloud, inMatrix) // done -sigmoid(inCloud) // 1/(1+e^x) // -derivative(inCloud) // x*(1-x) // -scale(inCloud1, inV) // +sigmoid(inCloud) // 1/(1+e^x) // done +derivative(inCloud) // x*(1-x) // done +scale(inCloud1, inV) // done subtract(inCloud1, inCloud2) // done multiply(inCloud1, inCloud2) // done \ No newline at end of file