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

View File

@ -1,52 +1,89 @@
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 = [
[ 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 training = [];
let stages = [];
let layers = [];
Deno.test("NN.Split", ()=>
{
[input, output] = Split(data, columns);
assert(input);
assert(output);
assertEquals(input.length, output.length, "data split into equal input and output");
let typeA = [
[ 0.1, 0.05],
[ 0.0, -0.06]
];
let typeB = [
[ 0.99, 0.85],
[ 1.2, 1.05]
];
assertEquals(input[0].length, 3, "padded input");
assertEquals(output[0].length, 2, "unpadded output");
});
Deno.test("NN.Build", ()=>
{
layers = Build(2, 5, 2);
assertEquals(layers.length, 2, "correct number of matrices");
assertEquals(layers[0][0].length, input[0].length, "input: padded input");
assertEquals(layers[0].length, 5, "input: unpadded output");
assertEquals(layers[1][0].length, 6, "hidden: padded input");
assertEquals(layers[1].length, output[0].length, "hidden: unpadded output");
Deno.test("check.forward", ()=>
{
let training = [];
let stages = [];
let layers = [
[
[-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 = [
[ 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", ()=>
{
let labels = Label(input, layers);
assertEquals(labels.length, output.length);
assertEquals(labels[0].length, output[0].length);
Label(training, typeA, [1]);
Label(training, typeB, [0]);
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);
assertEquals(error.length, output.length);
let total = 0;
let count = error.length*error[0].length;
error.forEach(row=> row.forEach(component=> total+=Math.abs(component)));
assert(total/count < 0.3);
let layer1 = M.Create.Box([-1, -1, -1], [1, 1, 1], 2);
let layer2 = M.Create.Box([-1, -1, -1], [1, 1, 1], 1);
let copy1 = M.Create.Clone(layer1);
let copy2 = M.Create.Clone(layer2);
layers.push(layer1);
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";
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 stages:N = [inData];
let process = (index:number):Cloud.M => M.Batch.Sigmoid(M.Batch.Affine(stages[index], inLayers[index]));
let process = (index:number):Cloud.M => M.Batch.Sigmoid(M.Batch.Affine(inStages[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;
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 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);
let errorBack:Cloud.M = M.Batch.Subtract(Forward(inStages, inLayers), inGoals);
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]));
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)
);
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], deltas);
});
}
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 };