78 lines
2.7 KiB
TypeScript
78 lines
2.7 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 process = (index:number):Cloud.M => M.Batch.Sigmoid(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);
|
|
|
|
for(i=inLayers.length-1; i>=0; i--)
|
|
{
|
|
let errorScaled:Cloud.M = M.Batch.Multiply(errorBack, M.Batch.Derivative(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 =>
|
|
{
|
|
let vectorData = [ ...inHeaderKeep.map((i:number)=>row[i]), 1];
|
|
let vectorLabel = inHeaderLabel.map((i:number)=>row[i])
|
|
data.push( vectorData );
|
|
label.push( vectorLabel );
|
|
});
|
|
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):Cloud.M =>
|
|
{
|
|
let stages:N = Forward(inData, inLayers);
|
|
return stages[stages.length-1];
|
|
};
|
|
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(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 }; |