nn/m.ts

60 lines
3.0 KiB
TypeScript
Raw Permalink Normal View History

2021-07-28 12:04:00 -04:00
export namespace Cloud
{
export type V = Array<number>
export type M = Array<Array<number>>
export type HandleLoop = (indexComponent:number, indexRow:number, array:Array<number>) => number
export type HandleEdit = (component:number, index:number, array:Array<number>) => number
};
2021-07-26 11:33:07 -04:00
const Methods = {
Iterate:
{
2021-07-28 12:04:00 -04:00
Loop: (inDimensions:number, inCount:number, inFunction:Cloud.HandleLoop):Cloud.M =>
2021-07-26 11:33:07 -04:00
{
2021-07-28 12:04:00 -04:00
let i:number, j:number, outputVector:Cloud.V;
const outputCloud:Cloud.M = [];
2021-07-26 11:33:07 -04:00
for(i=0; i<inCount; i++)
{
outputVector = [];
for(j=0; j<inDimensions; j++)
{
outputVector.push(inFunction(j, i, outputVector));
}
outputCloud.push(outputVector);
}
return outputCloud;
},
2021-07-28 12:04:00 -04:00
Edit: (inCloud:Cloud.M, inFunction:Cloud.HandleEdit):Cloud.M=> inCloud.map((row:Cloud.V):Cloud.V=>row.map(inFunction))
2021-07-26 11:33:07 -04:00
},
Create:
{
2021-07-28 12:04:00 -04:00
Box: (inV1:Cloud.V, inV2:Cloud.V, inCount:number):Cloud.M=> Methods.Iterate.Loop(inV1.length, inCount, i=> inV1[i]+(inV2[i]-inV1[i])*Math.random()),
Transpose: (inCloud:Cloud.M):Cloud.M=> Methods.Iterate.Loop(inCloud.length, inCloud[0].length, (i, row)=> inCloud[i][row]),
Outer: (inV1:Cloud.V, inV2:Cloud.V):Cloud.M=> Methods.Iterate.Loop(inV1.length, inV2.length, (i, row)=> inV1[i]*inV2[row]),
Clone: (inCloud:Cloud.M):Cloud.M=> Methods.Iterate.Edit(inCloud, i=> i)
2021-07-26 11:33:07 -04:00
},
Mutate:
{
2021-07-28 12:04:00 -04:00
Pad: (inCloud:Cloud.M):Cloud.M=> {inCloud.forEach((row:Cloud.V)=> row.push(1)); return inCloud; },
Unpad: (inCloud:Cloud.M):Cloud.M=> {inCloud.forEach((row:Cloud.V)=> row.pop()); return inCloud; }
2021-07-26 11:33:07 -04:00
},
Single:
{
2021-07-28 12:04:00 -04:00
Subtract: (inV1:Cloud.V, inV2:Cloud.V):Cloud.V=> inV1.map((component, i)=> component-inV2[i]),
Multiply: (inV1:Cloud.V, inV2:Cloud.V):Cloud.V=> inV1.map((component, i)=> component*inV2[i]),
Affine: (inV:Cloud.V, inMatrix:Cloud.M):Cloud.V=> inMatrix.map((row:Cloud.V)=> row.reduce((sum, current, index)=> sum + current*inV[index]))
2021-07-26 11:33:07 -04:00
},
Batch:
{
2021-07-28 12:04:00 -04:00
Subtract: (inCloud1:Cloud.M, inCloud2:Cloud.M):Cloud.M=> inCloud1.map((row:Cloud.V, rowIndex:number)=> Methods.Single.Subtract(row, inCloud2[rowIndex])),
Multiply: (inCloud1:Cloud.M, inCloud2:Cloud.M):Cloud.M=> inCloud1.map((row:Cloud.V, rowIndex:number)=> Methods.Single.Multiply(row, inCloud2[rowIndex])),
Affine: (inCloud1:Cloud.M, inCloud2:Cloud.M):Cloud.M=> inCloud1.map((row:Cloud.V)=> Methods.Single.Affine(row, inCloud2)),
Sigmoid: (inCloud:Cloud.M):Cloud.M=> Methods.Iterate.Edit(inCloud, i=>1/(1+Math.pow(Math.E, -i))),
Derivative: (inCloud:Cloud.M):Cloud.M=> Methods.Iterate.Edit(inCloud, i=>i*(1-i)),
Scale: (inCloud:Cloud.M, inScalar:number):Cloud.M=> Methods.Iterate.Edit(inCloud, i=>i*inScalar)
2021-07-26 11:33:07 -04:00
}
};
2021-07-28 12:04:00 -04:00
2021-07-26 11:33:07 -04:00
export default Methods;