all methods passing tests

This commit is contained in:
TreetopFlyer 2021-07-26 09:27:38 -04:00
parent b0ec2a54f8
commit aabf7eb566
3 changed files with 45 additions and 21 deletions

21
m.js
View File

@ -2,7 +2,7 @@ const M =
{ {
Iterate: Iterate:
{ {
New(inDimensions, inCount, inFunction) Loop: (inDimensions, inCount, inFunction)=>
{ {
let row, i, outputCloud, outputVector; let row, i, outputCloud, outputVector;
outputCloud = []; outputCloud = [];
@ -17,17 +17,14 @@ const M =
} }
return outputCloud; return outputCloud;
}, },
Old(inCloud, inFunction) Edit: (inCloud, inFunction)=> inCloud.map(row=>row.map(inFunction))
{
return M.Iterate.New(inCloud[0].length, inCloud.length, inFunction);
}
}, },
Create: Create:
{ {
Box: (inV1, inV2, inCount)=> M.Iterate.New(inV1.length, inCount, (i, row)=> inV1[i]+(inV2[i]-inV1[i])*Math.random()), Box: (inV1, inV2, inCount)=> M.Iterate.Loop(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]), Transpose: (inCloud)=> M.Iterate.Loop(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]), Outer: (inV1, inV2)=> M.Iterate.Loop(inV1.length, inV2.length, (i, row)=> inV1[i]*inV2[row]),
Clone: (inCloud)=> M.Iterate.Old(inCloud, (i, row)=> inCloud[row][i]) Clone: (inCloud)=> M.Iterate.Edit(inCloud, i=> i)
}, },
Mutate: Mutate:
{ {
@ -45,9 +42,9 @@ const M =
Subtract: (inCloud1, inCloud2)=> inCloud1.map((row, rowIndex)=> M.Single.Subtract(row, inCloud2[rowIndex])), 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])), Multiply: (inCloud1, inCloud2)=> inCloud1.map((row, rowIndex)=> M.Single.Multiply(row, inCloud2[rowIndex])),
Affine: (inCloud, inMatrix)=> inCloud.map(row=> M.Single.Affine(row, inMatrix)), 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))), Sigmoid: (inCloud)=> M.Iterate.Edit(inCloud, i=>1/(1+Math.pow(Math.E, -i))),
Derivative: (inCloud)=> M.Iterate.Old(inCloud, i=>i*(1-i)), Derivative: (inCloud)=> M.Iterate.Edit(inCloud, i=>i*(1-i)),
Scale: (inCloud, inScalar)=> M.Iterate.Old(inCloud, i=>i*inScalar) Scale: (inCloud, inScalar)=> M.Iterate.Edit(inCloud, i=>i*inScalar)
} }
} }

View File

@ -1,16 +1,25 @@
import { assert, assertEquals } from "https://deno.land/std@0.102.0/testing/asserts.ts"; import { assert, assertEquals } from "https://deno.land/std@0.102.0/testing/asserts.ts";
import { default as M } from "./m.js"; import { default as M } from "./m.js";
Deno.test("Iterate.New", ()=> Deno.test("Iterate.Loop", ()=>
{ {
let dimensions = 3; let dimensions = 3;
let count = 4; 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.length, count, "correct count");
assertEquals(cloud[0].length, dimensions, "correct dimensions"); assertEquals(cloud[0].length, dimensions, "correct dimensions");
assertEquals(cloud[0][0], 0); assertEquals(cloud[0][0], 0);
assertEquals(cloud[3][2], 5, "correct output"); 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", ()=> Deno.test("Create.Box", ()=>
{ {
@ -30,7 +39,6 @@ Deno.test("Create.Box", ()=>
} }
} }
}); });
Deno.test("Create.Transpose", ()=> Deno.test("Create.Transpose", ()=>
{ {
let v1 = [1, 2, 3]; let v1 = [1, 2, 3];
@ -41,7 +49,6 @@ Deno.test("Create.Transpose", ()=>
assertEquals(tpose[0][0], v1[0]); assertEquals(tpose[0][0], v1[0]);
assertEquals(tpose[0][1], v2[0], "correct placement"); assertEquals(tpose[0][1], v2[0], "correct placement");
}); });
Deno.test("Create.Outer", ()=> Deno.test("Create.Outer", ()=>
{ {
let v1 = [1, 2, 3]; let v1 = [1, 2, 3];
@ -51,7 +58,6 @@ Deno.test("Create.Outer", ()=>
assertEquals(outer[0].length, v1.length, "correct dimensions"); assertEquals(outer[0].length, v1.length, "correct dimensions");
assertEquals(outer[1][0], v1[0]*v2[1], "correct placement") assertEquals(outer[1][0], v1[0]*v2[1], "correct placement")
}); });
Deno.test("Create.Clone", ()=> Deno.test("Create.Clone", ()=>
{ {
let v1 = [1, 2, 3]; let v1 = [1, 2, 3];
@ -62,6 +68,7 @@ Deno.test("Create.Clone", ()=>
assertEquals(clone[1][0], v2[0], "correct placement"); assertEquals(clone[1][0], v2[0], "correct placement");
}); });
Deno.test("Mutate.Pad", ()=> Deno.test("Mutate.Pad", ()=>
{ {
let matrix = [ let matrix = [
@ -133,8 +140,28 @@ Deno.test("Batch.Scale", ()=>
let c = [[1, 2], [3, 4]]; let c = [[1, 2], [3, 4]];
let s = 0.5; let s = 0.5;
let t = M.Batch.Scale(c, s); let t = M.Batch.Scale(c, s);
console.log(t);
assertEquals(t.length, 2, "correct count"); assertEquals(t.length, 2, "correct count");
assertEquals(t[0].length, 2, "correct dimensions"); assertEquals(t[0].length, 2, "correct dimensions");
console.log(t);
assertEquals(t[1][0], 1.5, "correct placement"); 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][1] && t[0][1]>t[0][2]);
});

View File

@ -7,8 +7,8 @@ pad(inCloud) // done
unpad(inCloud) // done unpad(inCloud) // done
transform(inCloud, inMatrix) // done transform(inCloud, inMatrix) // done
sigmoid(inCloud) // 1/(1+e^x) // sigmoid(inCloud) // 1/(1+e^x) // done
derivative(inCloud) // x*(1-x) // derivative(inCloud) // x*(1-x) // done
scale(inCloud1, inV) // scale(inCloud1, inV) // done
subtract(inCloud1, inCloud2) // done subtract(inCloud1, inCloud2) // done
multiply(inCloud1, inCloud2) // done multiply(inCloud1, inCloud2) // done