Compare commits

..

No commits in common. "master" and "debug" have entirely different histories.

3 changed files with 128 additions and 119 deletions

View File

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

View File

@ -1,52 +1,89 @@
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 { Split, Build, Label, Learn, Check } from "./nn.ts"; import { Label, Forward, Backward } from "./nn.ts";
import { default as M } from "./m.ts";
import { default as Methods } from "./m.ts";
let data = [ let training = [];
[ 0.10, 0.05, 0, 1], let stages = [];
[ 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 = [];
Deno.test("NN.Split", ()=> let typeA = [
{ [ 0.1, 0.05],
[input, output] = Split(data, columns); [ 0.0, -0.06]
assert(input); ];
assert(output); let typeB = [
assertEquals(input.length, output.length, "data split into equal input and output"); [ 0.99, 0.85],
[ 1.2, 1.05]
];
assertEquals(input[0].length, 3, "padded input");
assertEquals(output[0].length, 2, "unpadded output"); Deno.test("check.forward", ()=>
}); {
let training = [];
Deno.test("NN.Build", ()=> let stages = [];
{ let layers = [
layers = Build(2, 5, 2); [
[-0.43662948305036675, -0.368590640707799, -0.23227179558890843],
assertEquals(layers.length, 2, "correct number of matrices"); [-0.004292653969505622, 0.38670055222186317, -0.2478421495365568],
assertEquals(layers[0][0].length, input[0].length, "input: padded input"); [0.738181366836224, 0.3389203747353555, 0.4920200816404332]
assertEquals(layers[0].length, 5, "input: unpadded output"); ],
[
assertEquals(layers[1][0].length, 6, "hidden: padded input"); [0.5793881115472015, 0.9732593374796092, 0.15207639877016987, -0.5356575655337803]
assertEquals(layers[1].length, output[0].length, "hidden: unpadded output"); ]
];
let typeA = [
[ 0.1, 0.05],
[ 0.0, -0.06]
];
let typeB = [
[ 0.99, 0.85],
[ 1.2, 1.05]
];
Label(training, typeA, [1]);
stages.push(training[0]);
Forward(stages, layers);
console.log(stages);
}); });
/*
Deno.test("NN.Label", ()=> Deno.test("NN.Label", ()=>
{ {
let labels = Label(input, layers); Label(training, typeA, [1]);
assertEquals(labels.length, output.length); Label(training, typeB, [0]);
assertEquals(labels[0].length, output[0].length); stages.push(training[0]);
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.Learn", ()=> Deno.test("NN.Backward", ()=>
{ {
let error = Learn(input, layers, output, 1000, 0.1); let layer1 = M.Create.Box([-1, -1, -1], [1, 1, 1], 2);
assertEquals(error.length, output.length); let layer2 = M.Create.Box([-1, -1, -1], [1, 1, 1], 1);
let total = 0; let copy1 = M.Create.Clone(layer1);
let count = error.length*error[0].length; let copy2 = M.Create.Clone(layer2);
error.forEach(row=> row.forEach(component=> total+=Math.abs(component))); layers.push(layer1);
assert(total/count < 0.3); layers.push(layer2);
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,78 +1,54 @@
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 Forward = (inData:Cloud.M, inLayers:N):N => const Label = (inSet:any, inData:Cloud.M, inLabel:Cloud.V):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 stages:N = [inData]; let process = (index:number):Cloud.M => M.Batch.Sigmoid(M.Batch.Affine(inStages[index], inLayers[index]));
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)); } for(i=0; i<inLayers.length-1; i++)
stages[i+1] = process(i); {
return stages; inStages[i+1] = M.Mutate.Pad(process(i));
}
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(inStages[inStages.length-1], inGoals); let errorBack:Cloud.M = M.Batch.Subtract(Forward(inStages, inLayers), inGoals);
for(i=inLayers.length-1; i>=0; i--) for(i=inLayers.length-1; i>=0; i--)
{ {
let errorScaled:Cloud.M = M.Batch.Multiply(errorBack, M.Batch.Derivative(inStages[i+1])); 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(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)=> {
inLayers[i] = M.Batch.Subtract( const deltas = M.Batch.Scale(M.Create.Outer(layerInput[inIndex], inScaledError), inRate);
inLayers[i], inLayers[i] = M.Batch.Subtract(inLayers[i], deltas);
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 { Split, Build, Label, Learn, Check, Forward, Backward }; export { Label, Forward, Backward };
export type { Cloud };