add more methods

This commit is contained in:
TreetopFlyer 2021-07-25 21:59:18 -04:00
parent 642e4203a0
commit b0ec2a54f8
3 changed files with 46 additions and 11 deletions

13
m.js
View File

@ -36,13 +36,18 @@ const M =
}, },
Single: Single:
{ {
Affine: (inV, inMatrix)=> inMatrix.map(row=> row.reduce((sum, current, index)=> sum + current*inV[index])) Subtract: (inV1, inV2)=> inV1.map((component, i)=> component-inV2[i]),
Multiply: (inV1, inV2)=> inV1.map((component, i)=> component*inV2[i]),
Affine: (inV, inMatrix)=> inMatrix.map(row=> row.reduce((sum, current, index)=> sum + current*inV[index]))
}, },
Batch: Batch:
{ {
Affine: (inCloud, inMatrix)=> inCloud.map(row=> M.Single.Affine(row, inMatrix)), Subtract: (inCloud1, inCloud2)=> inCloud1.map((row, rowIndex)=> M.Single.Subtract(row, inCloud2[rowIndex])),
Sigmoid: (inCloud)=> M.Iterate.Old(inCloud, i=>1/(1+Math.Pow(Math.E, i))), Multiply: (inCloud1, inCloud2)=> inCloud1.map((row, rowIndex)=> M.Single.Multiply(row, inCloud2[rowIndex])),
Derivative: (inCloud)=> M.Iterate.Old(inCloud, i=>i*(1-i)) 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)
} }
} }

View File

@ -96,6 +96,28 @@ Deno.test("Single.Affine", ()=>
assertEquals(t[1], 1.1, "correct placement"); assertEquals(t[1], 1.1, "correct placement");
}); });
Deno.test("Single.Subtract", ()=>
{
let v1 = [1, 2];
let v2 = [3, 4];
let t = M.Single.Subtract(v1, v2);
assertEquals(t.length, 2, "correct dimensions");
assertEquals(t[0], -2)
assertEquals(t[1], -2, "correct placement");
});
Deno.test("Single.Multiply", ()=>
{
let v1 = [1, 2];
let v2 = [3, 4];
let t = M.Single.Multiply(v1, v2);
assertEquals(t.length, 2, "correct dimensions");
assertEquals(t[0], 3)
assertEquals(t[1], 8, "correct placement");
});
Deno.test("Batch.Affine", ()=> Deno.test("Batch.Affine", ()=>
{ {
let c = [[1, 2], [3, 4]]; let c = [[1, 2], [3, 4]];
@ -105,3 +127,14 @@ Deno.test("Batch.Affine", ()=>
assertEquals(t[0].length, 2, "correct dimensions") assertEquals(t[0].length, 2, "correct dimensions")
assertEquals(t[0][1], 1.1, "correct placement"); assertEquals(t[0][1], 1.1, "correct placement");
}); });
Deno.test("Batch.Scale", ()=>
{
let c = [[1, 2], [3, 4]];
let s = 0.5;
let t = M.Batch.Scale(c, s);
assertEquals(t.length, 2, "correct count");
assertEquals(t[0].length, 2, "correct dimensions");
console.log(t);
assertEquals(t[1][0], 1.5, "correct placement");
});

View File

@ -6,12 +6,9 @@ clone(inCloud) // done
pad(inCloud) // done pad(inCloud) // done
unpad(inCloud) // done unpad(inCloud) // done
// batch filter
transform(inCloud, inMatrix) // done transform(inCloud, inMatrix) // done
sigmoid(inCloud) // 1/(1+e^x) // sigmoid(inCloud) // 1/(1+e^x) //
derivative(inCloud) // x*(1-x) derivative(inCloud) // x*(1-x) //
scale(inCloud1, inV) scale(inCloud1, inV) //
subtract(inCloud1, inCloud2) // done
// batch of pairs multiply(inCloud1, inCloud2) // done
subtract(inCloud1, inCloud2)
multiply(inCloud1, inCloud2)