Nework added

This commit is contained in:
TreetopFlyer 2021-07-28 12:04:00 -04:00
parent ffb8305617
commit 0d06d9e158
4 changed files with 78 additions and 23 deletions

51
m.ts
View File

@ -1,15 +1,18 @@
export type V = Array<number>;
export type M = Array<V>;
export type HandleLoop = (indexComponent:number, indexRow:number, array:Array<number>) => number;
export type HandleEdit = (component:number, index:number, array:Array<number>) => number;
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
};
const Methods = {
Iterate:
{
Loop: (inDimensions:number, inCount:number, inFunction:HandleLoop):M =>
Loop: (inDimensions:number, inCount:number, inFunction:Cloud.HandleLoop):Cloud.M =>
{
let i:number, j:number, outputVector:V;
const outputCloud:M = [];
let i:number, j:number, outputVector:Cloud.V;
const outputCloud:Cloud.M = [];
for(i=0; i<inCount; i++)
{
outputVector = [];
@ -21,35 +24,37 @@ const Methods = {
}
return outputCloud;
},
Edit: (inCloud:M, inFunction:HandleEdit):M=> inCloud.map((row:V):V=>row.map(inFunction))
Edit: (inCloud:Cloud.M, inFunction:Cloud.HandleEdit):Cloud.M=> inCloud.map((row:Cloud.V):Cloud.V=>row.map(inFunction))
},
Create:
{
Box: (inV1:V, inV2:V, inCount:number):M=> Methods.Iterate.Loop(inV1.length, inCount, i=> inV1[i]+(inV2[i]-inV1[i])*Math.random()),
Transpose: (inCloud:M):M=> Methods.Iterate.Loop(inCloud.length, inCloud[0].length, (i, row)=> inCloud[i][row]),
Outer: (inV1:V, inV2:V):M=> Methods.Iterate.Loop(inV1.length, inV2.length, (i, row)=> inV1[i]*inV2[row]),
Clone: (inCloud:M):M=> Methods.Iterate.Edit(inCloud, i=> i)
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)
},
Mutate:
{
Pad: (inCloud:M):void=> inCloud.forEach(row=> row.push(1)),
Unpad: (inCloud:M):void=> inCloud.forEach(row=> row.pop())
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; }
},
Single:
{
Subtract: (inV1:V, inV2:V):V=> inV1.map((component, i)=> component-inV2[i]),
Multiply: (inV1:V, inV2:V):V=> inV1.map((component, i)=> component*inV2[i]),
Affine: (inV:V, inMatrix:M):V=> inMatrix.map(row=> row.reduce((sum, current, index)=> sum + current*inV[index]))
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]))
},
Batch:
{
Subtract: (inCloud1:M, inCloud2:M):M=> inCloud1.map((row, rowIndex)=> Methods.Single.Subtract(row, inCloud2[rowIndex])),
Multiply: (inCloud1:M, inCloud2:M):M=> inCloud1.map((row, rowIndex)=> Methods.Single.Multiply(row, inCloud2[rowIndex])),
Affine: (inCloud:M, inMatrix:M):M=> inCloud.map(row=> Methods.Single.Affine(row, inMatrix)),
Sigmoid: (inCloud:M):M=> Methods.Iterate.Edit(inCloud, i=>1/(1+Math.pow(Math.E, -i))),
Derivative: (inCloud:M):M=> Methods.Iterate.Edit(inCloud, i=>i*(1-i)),
Scale: (inCloud:M, inScalar:number):M=> Methods.Iterate.Edit(inCloud, i=>i*inScalar)
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)
}
};
export default Methods;

14
nn.test.js Normal file
View File

@ -0,0 +1,14 @@
import { assert, assertEquals } from "https://deno.land/std@0.102.0/testing/asserts.ts";
import * as NN from "./nn.ts";
Deno.test("NN.Observe", ()=>
{
console.log(NN.Observe([[[1, 2, 3]]], [[[0.4, 0.5, 0.6]]]));
});
Deno.test("NN.Train", ()=>
{
const stages = [[[1, 2, 3]]];
const layers = [[[0.4, 0.5, 0.6]]];
console.log(NN.Train(stages, layers, [[0, 0, 1]], 0.1));
});

36
nn.ts Normal file
View File

@ -0,0 +1,36 @@
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 };

View File