nn/m.js

49 lines
1.7 KiB
JavaScript
Raw Normal View History

2021-07-25 18:17:54 -04:00
const M =
{
Iterate:
{
New(inDimensions, inCount, inFunction)
{
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;
},
Old(inCloud, inFunction)
{
return M.Iterate.New(inCloud[0].length, inCloud.length, 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])
},
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;