nn/nn.ts
2021-07-31 10:37:17 -04:00

89 lines
3.1 KiB
TypeScript

import { default as M, Cloud } from "./m.ts";
export type N = Array<Array<Array<number>>>
const Forward = (inData:Cloud.M, inLayers:N):N =>
{
let i:number;
let stages:N = [inData];
let nonLinear = (inIndex:number):any=> inIndex >= inLayers.length-1 ? M.Batch.Sig : M.Batch.Rec;
let process = (index:number):Cloud.M => nonLinear(index)(M.Batch.Affine(stages[index], inLayers[index]));
for(i=0; i<inLayers.length-1; i++){ stages[i+1] = M.Mutate.Pad(process(i)); }
stages[i+1] = process(i);
return stages;
};
const Backward = (inStages:N, inLayers:N, inGoals:Cloud.M, inRate:number):N =>
{
let i:number;
let errorBack:Cloud.M = M.Batch.Subtract(inStages[inStages.length-1], inGoals);
let nonLinear = (inIndex:number):any=> inIndex >= inLayers.length-1 ? M.Batch.SigDeriv : M.Batch.RecDeriv;
for(i=inLayers.length-1; i>=0; i--)
{
let errorScaled:Cloud.M = M.Batch.Multiply(errorBack, nonLinear(i)(inStages[i+1]));
errorBack = M.Batch.Affine(errorScaled, M.Create.Transpose(inLayers[i]));
errorScaled.forEach((inScaledError:Cloud.V, inIndex:number)=>
{
inLayers[i] = M.Batch.Subtract(
inLayers[i],
M.Batch.Scale(M.Create.Outer(inStages[i][inIndex], inScaledError), inRate)
);
});
}
return inLayers;
};
const Split = (inTrainingSet:Cloud.M, inHeaderLabel:Cloud.V, inHeaderKeep:Cloud.V = []):N =>
{
let data:Cloud.M = [];
let label:Cloud.M = [];
if(!inHeaderKeep.length)
{
inTrainingSet[0].forEach( (item:number, index:number)=> inHeaderLabel.includes(index) ? false : inHeaderKeep.push(index) );
}
inTrainingSet.forEach((row:Cloud.V):void =>
{
data.push( inHeaderKeep.map((i:number)=>row[i]) );
label.push( inHeaderLabel.map((i:number)=>row[i]) );
});
return [ data, label ];
};
const Build = (...inLayers:Array<number>):N =>
{
let i:number;
let output:N = [];
let rand = (inDimensions:number, inCount:number):Cloud.M => M.Create.Box( new Array(inDimensions).fill(-1), new Array(inDimensions).fill(1), inCount);
for(i=0; i<inLayers.length-1; i++)
{
output.push(rand( inLayers[i]+1, inLayers[i+1]));
}
return output;
};
const Label = (inData:Cloud.M, inLayers:N, inRound:boolean):Cloud.M =>
{
let stages:N = Forward(M.Create.Padded(inData), inLayers);
let output = stages[stages.length-1];
if(inRound)
{
output.forEach(row=>
{
row.forEach((cell, i)=>
{
row[i] = (Math.round(cell * 100) / 100);
});
});
}
return output;
};
const Learn = (inData:Cloud.M, inLayers:N, inLabels:Cloud.M, inIterations:number, inRate:number):Cloud.M =>
{
let stages:N = [];
for(let i=0; i<inIterations; i++)
{
stages = Forward(M.Create.Padded(inData), inLayers);
Backward(stages, inLayers, inLabels, inRate);
}
return M.Batch.Subtract(stages[stages.length-1], inLabels);
};
const Check = (inData:Cloud.M, inLayers:N, inLabels:Cloud.M):Cloud.M => Learn(inData, inLayers, inLabels, 1, 0);
export { Split, Build, Label, Learn, Check, Forward, Backward };