nn/m.js

51 lines
2.1 KiB
JavaScript
Raw Normal View History

2021-07-25 18:17:54 -04:00
const M =
{
Iterate:
{
2021-07-26 09:27:38 -04:00
Loop: (inDimensions, inCount, inFunction)=>
2021-07-25 18:17:54 -04:00
{
let row, i, outputCloud, outputVector;
outputCloud = [];
for(row=0; row<inCount; row++)
{
outputVector = [];
for(i=0; i<inDimensions; i++)
{
outputVector.push(inFunction(i, row, outputVector));
}
outputCloud.push(outputVector);
}
return outputCloud;
},
2021-07-26 09:27:38 -04:00
Edit: (inCloud, inFunction)=> inCloud.map(row=>row.map(inFunction))
2021-07-25 18:17:54 -04:00
},
Create:
{
2021-07-26 09:27:38 -04:00
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)
2021-07-25 18:17:54 -04:00
},
Mutate:
{
Pad: inCloud=> inCloud.forEach(row=> row.push(1)),
Unpad: inCloud=> inCloud.forEach(row=> row.pop())
},
Single:
{
2021-07-25 21:59:18 -04:00
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]))
2021-07-25 18:17:54 -04:00
},
Batch:
{
2021-07-25 21:59:18 -04:00
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)),
2021-07-26 09:27:38 -04:00
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)
2021-07-25 18:17:54 -04:00
}
}
export default M;