36 lines
1.3 KiB
TypeScript
36 lines
1.3 KiB
TypeScript
|
import { default as M, Cloud } from "./m.ts";
|
||
|
|
||
|
const Observe = (inStages:Array<Cloud.M>, inLayers:Array<Cloud.M>):Cloud.M =>
|
||
|
{
|
||
|
let i:number;
|
||
|
let process = (index:number):Cloud.M => M.Batch.Sigmoid(M.Batch.Affine(inStages[index], inLayers[index]));
|
||
|
|
||
|
for(i=0; i<inLayers.length-1; i++)
|
||
|
{
|
||
|
inStages[i+1] = M.Mutate.Pad(process(i));
|
||
|
}
|
||
|
inStages[i+1] = process(i);
|
||
|
return inStages[i+1];
|
||
|
};
|
||
|
const Train = (inStages:Array<Cloud.M>, inLayers:Array<Cloud.M>, inGoals:Cloud.M, inRate:number):void =>
|
||
|
{
|
||
|
let i:number;
|
||
|
let errorBack:Cloud.M = M.Batch.Subtract(Observe(inStages, inLayers), inGoals);
|
||
|
|
||
|
for(i=inLayers.length-1; i>=0; i++)
|
||
|
{
|
||
|
let layerMatrix:Cloud.M = inLayers[i];
|
||
|
let layerInput:Cloud.M = inStages[i];
|
||
|
let layerOutput:Cloud.M = inStages[i+1];
|
||
|
let errorScaled:Cloud.M = M.Batch.Multiply(errorBack, M.Batch.Derivative(layerOutput));
|
||
|
|
||
|
errorBack = M.Batch.Affine(errorScaled, M.Create.Transpose(layerMatrix));
|
||
|
errorScaled.forEach((inScaledError:Cloud.V, inIndex:number)=> {
|
||
|
const deltas = M.Batch.Scale(M.Create.Outer(layerInput[inIndex], inScaledError), inRate);
|
||
|
layerMatrix = M.Batch.Subtract(layerMatrix, deltas);
|
||
|
});
|
||
|
}
|
||
|
};
|
||
|
|
||
|
export { Observe, Train };
|
||
|
export type { Cloud };
|