Compare commits

...

6 Commits

Author SHA1 Message Date
3b97e163e4 Merge pull request 'network' (#1) from network into master
Reviewed-on: #1
2021-07-29 16:16:32 -04:00
20e27bcb17 network tests passing 2021-07-29 16:15:05 -04:00
68aafafc0d netowrk functions started 2021-07-29 14:51:00 -04:00
1e644a70dc layer batch training ok 2021-07-29 10:13:21 -04:00
23e71542f2 adjust ok 2021-07-29 09:19:27 -04:00
09053ebf97 error good 2021-07-29 08:42:28 -04:00
3 changed files with 118 additions and 127 deletions

View File

@ -614,7 +614,7 @@ NN.Network.Create = function()
var i; var i;
obj.Layers = []; obj.Layers = [];
obj.LearningRate = 0.8; obj.LearningRate = 0.1;
obj.Error = []; obj.Error = [];
for(i=0; i<arguments.length-1; i++) for(i=0; i<arguments.length-1; i++)
@ -688,26 +688,30 @@ NN.Network.Stochastic = function(inNetwork, inTrainingSet, inIterations)
]; ];
let matrix2 = [ let matrix2 = [
[0.5793881115472015, 0.9732593374796092, 0.15207639877016987, -0.5356575655337803] [0.7098703863463034, 0.35485944251238033, 0.7642849892333241, 0.03046174288491077],
[-0.30655426258144347, 0.45509633551425077, -0.5013795222004322, -0.3421292736637427]
]; ];
let typeA = [ let input = [
[ 0.1, 0.05], [ 0.1, 0.05],
[ 0.0, -0.06] [ 0.0, -0.06],
[ 0.99, 0.85],
[ 1.2, 1.05]
]; ];
let typeB = [ let output = [
[ 0.99, 0.85], [1, 0],
[ 1.2, 1.05] [1, 0],
[0, 1],
[0, 1]
]; ];
var layer1 = NN.Layer.Create(1, 1); let nn1 = NN.Network.Create(2, 3, 2);
layer1.Forward.Matrix = matrix1; nn1.Layers[0].Forward.Matrix = matrix1;
nn1.Layers[1].Forward.Matrix = matrix2;
nn1.LearningRate = 0.1;
//let logLayers = inNN => inNN.Layers.forEach(L=>console.log(L.Forward.Matrix));
var layer2 = NN.Layer.Create(1, 1); NN.Network.Batch(nn1, {Input:input, Output:output}, 1000);
layer2.Forward.Matrix = matrix2; console.log(NN.Network.Observe(nn1, input));
let stage1 = NN.Layer.Forward(layer1, typeA);
console.log(stage1);
</script> </script>

View File

@ -1,89 +1,52 @@
import { assert, assertEquals } from "https://deno.land/std@0.102.0/testing/asserts.ts"; import { assert, assertEquals } from "https://deno.land/std@0.102.0/testing/asserts.ts";
import { Label, Forward, Backward } from "./nn.ts"; import { Split, Build, Label, Learn, Check } from "./nn.ts";
import { default as M } from "./m.ts";
import { default as Methods } from "./m.ts";
let training = []; let data = [
let stages = []; [ 0.10, 0.05, 0, 1],
[ 0.00, -0.06, 0, 1],
[ 0.99, 0.85, 1, 0],
[ 1.20, 1.05, 1, 0]
];
let columns = [2, 3];
let input, output;
let layers = []; let layers = [];
let typeA = [ Deno.test("NN.Split", ()=>
[ 0.1, 0.05],
[ 0.0, -0.06]
];
let typeB = [
[ 0.99, 0.85],
[ 1.2, 1.05]
];
Deno.test("check.forward", ()=>
{ {
let training = []; [input, output] = Split(data, columns);
let stages = []; assert(input);
let layers = [ assert(output);
[ assertEquals(input.length, output.length, "data split into equal input and output");
[-0.43662948305036675, -0.368590640707799, -0.23227179558890843],
[-0.004292653969505622, 0.38670055222186317, -0.2478421495365568],
[0.738181366836224, 0.3389203747353555, 0.4920200816404332]
],
[
[0.5793881115472015, 0.9732593374796092, 0.15207639877016987, -0.5356575655337803]
]
];
let typeA = [ assertEquals(input[0].length, 3, "padded input");
[ 0.1, 0.05], assertEquals(output[0].length, 2, "unpadded output");
[ 0.0, -0.06] });
];
let typeB = [ Deno.test("NN.Build", ()=>
[ 0.99, 0.85], {
[ 1.2, 1.05] layers = Build(2, 5, 2);
];
assertEquals(layers.length, 2, "correct number of matrices");
Label(training, typeA, [1]); assertEquals(layers[0][0].length, input[0].length, "input: padded input");
stages.push(training[0]); assertEquals(layers[0].length, 5, "input: unpadded output");
Forward(stages, layers);
console.log(stages); assertEquals(layers[1][0].length, 6, "hidden: padded input");
assertEquals(layers[1].length, output[0].length, "hidden: unpadded output");
}); });
/*
Deno.test("NN.Label", ()=> Deno.test("NN.Label", ()=>
{ {
Label(training, typeA, [1]); let labels = Label(input, layers);
Label(training, typeB, [0]); assertEquals(labels.length, output.length);
stages.push(training[0]); assertEquals(labels[0].length, output[0].length);
console.log(training);
assertEquals(training.length, 2, "input and output sets created");
assertEquals(training[0].length, training[1].length, "both sets have same length");
assertEquals(training[0][0].length, 3, "padded input component");
assertEquals(training[1][0].length, 1, "unchanged label vector");
}); });
Deno.test("NN.Backward", ()=> Deno.test("NN.Learn", ()=>
{ {
let layer1 = M.Create.Box([-1, -1, -1], [1, 1, 1], 2); let error = Learn(input, layers, output, 1000, 0.1);
let layer2 = M.Create.Box([-1, -1, -1], [1, 1, 1], 1); assertEquals(error.length, output.length);
let copy1 = M.Create.Clone(layer1); let total = 0;
let copy2 = M.Create.Clone(layer2); let count = error.length*error[0].length;
layers.push(layer1); error.forEach(row=> row.forEach(component=> total+=Math.abs(component)));
layers.push(layer2); assert(total/count < 0.3);
for(let i=0; i<100; i++)
{
Backward(stages, layers, training[1], 0.1);
}
assert(layers[0][0][0] != copy1[0][0], "first matrix has changed");
assert(layers[1][0][0] != copy2[0][0], "second matrix has changed");
}); });
Deno.test("NN.Forward", ()=>
{
console.log(Forward(stages, layers));
console.log(training[1]);
});
*/

94
nn.ts
View File

@ -1,54 +1,78 @@
import { default as M, Cloud } from "./m.ts"; import { default as M, Cloud } from "./m.ts";
export type N = Array<Array<Array<number>>> export type N = Array<Array<Array<number>>>
const Label = (inSet:any, inData:Cloud.M, inLabel:Cloud.V):N => const Forward = (inData:Cloud.M, inLayers:N):N =>
{
if(!inSet){inSet = [[], []];}
if(inSet.length == 0){inSet.push([]);}
if(inSet.length == 1){inSet.push([]);}
inData.forEach((row:Cloud.V) =>
{
row.push(1);
inSet[0].push(row);
inSet[1].push(inLabel);
});
return inSet;
};
const Forward = (inStages:N, inLayers:N):Cloud.M =>
{ {
let i:number; let i:number;
let process = (index:number):Cloud.M => M.Batch.Sigmoid(M.Batch.Affine(inStages[index], inLayers[index])); 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++) for(i=0; i<inLayers.length-1; i++){ stages[i+1] = M.Mutate.Pad(process(i)); }
{ stages[i+1] = process(i);
inStages[i+1] = M.Mutate.Pad(process(i)); return stages;
}
inStages[i+1] = process(i);
return inStages[i+1];
}; };
const Backward = (inStages:N, inLayers:N, inGoals:Cloud.M, inRate:number):N => const Backward = (inStages:N, inLayers:N, inGoals:Cloud.M, inRate:number):N =>
{ {
let i:number; let i:number;
let errorBack:Cloud.M = M.Batch.Subtract(Forward(inStages, inLayers), inGoals); let errorBack:Cloud.M = M.Batch.Subtract(inStages[inStages.length-1], inGoals);
for(i=inLayers.length-1; i>=0; i--) for(i=inLayers.length-1; i>=0; i--)
{ {
let layerInput:Cloud.M = inStages[i]; let errorScaled:Cloud.M = M.Batch.Multiply(errorBack, M.Batch.Derivative(inStages[i+1]));
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(inLayers[i])); errorBack = M.Batch.Affine(errorScaled, M.Create.Transpose(inLayers[i]));
errorScaled.forEach((inScaledError:Cloud.V, inIndex:number)=>
errorScaled.forEach((inScaledError:Cloud.V, inIndex:number)=> { {
const deltas = M.Batch.Scale(M.Create.Outer(layerInput[inIndex], inScaledError), inRate); inLayers[i] = M.Batch.Subtract(
inLayers[i] = M.Batch.Subtract(inLayers[i], deltas); inLayers[i],
M.Batch.Scale(M.Create.Outer(inStages[i][inIndex], inScaledError), inRate)
);
}); });
} }
return inLayers; 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 { Label, Forward, Backward }; export { Split, Build, Label, Learn, Check, Forward, Backward };
export type { Cloud };