Compare commits
6 Commits
Author | SHA1 | Date | |
---|---|---|---|
3b97e163e4 | |||
20e27bcb17 | |||
68aafafc0d | |||
1e644a70dc | |||
23e71542f2 | |||
09053ebf97 |
36
index.html
36
index.html
@ -614,7 +614,7 @@ NN.Network.Create = function()
|
||||
var i;
|
||||
|
||||
obj.Layers = [];
|
||||
obj.LearningRate = 0.8;
|
||||
obj.LearningRate = 0.1;
|
||||
obj.Error = [];
|
||||
|
||||
for(i=0; i<arguments.length-1; i++)
|
||||
@ -688,26 +688,30 @@ NN.Network.Stochastic = function(inNetwork, inTrainingSet, inIterations)
|
||||
];
|
||||
|
||||
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 = [
|
||||
[ 0.1, 0.05],
|
||||
[ 0.0, -0.06]
|
||||
let input = [
|
||||
[ 0.1, 0.05],
|
||||
[ 0.0, -0.06],
|
||||
[ 0.99, 0.85],
|
||||
[ 1.2, 1.05]
|
||||
];
|
||||
let typeB = [
|
||||
[ 0.99, 0.85],
|
||||
[ 1.2, 1.05]
|
||||
let output = [
|
||||
[1, 0],
|
||||
[1, 0],
|
||||
[0, 1],
|
||||
[0, 1]
|
||||
];
|
||||
|
||||
var layer1 = NN.Layer.Create(1, 1);
|
||||
layer1.Forward.Matrix = matrix1;
|
||||
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 layer2 = NN.Layer.Create(1, 1);
|
||||
layer2.Forward.Matrix = matrix2;
|
||||
|
||||
let stage1 = NN.Layer.Forward(layer1, typeA);
|
||||
|
||||
console.log(stage1);
|
||||
NN.Network.Batch(nn1, {Input:input, Output:output}, 1000);
|
||||
console.log(NN.Network.Observe(nn1, input));
|
||||
|
||||
</script>
|
113
nn.test.js
113
nn.test.js
@ -1,89 +1,52 @@
|
||||
import { assert, assertEquals } from "https://deno.land/std@0.102.0/testing/asserts.ts";
|
||||
import { Label, Forward, Backward } from "./nn.ts";
|
||||
import { default as M } from "./m.ts";
|
||||
import { default as Methods } from "./m.ts";
|
||||
import { Split, Build, Label, Learn, Check } from "./nn.ts";
|
||||
|
||||
let training = [];
|
||||
let stages = [];
|
||||
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 layers = [];
|
||||
|
||||
let typeA = [
|
||||
[ 0.1, 0.05],
|
||||
[ 0.0, -0.06]
|
||||
];
|
||||
let typeB = [
|
||||
[ 0.99, 0.85],
|
||||
[ 1.2, 1.05]
|
||||
];
|
||||
|
||||
|
||||
Deno.test("check.forward", ()=>
|
||||
Deno.test("NN.Split", ()=>
|
||||
{
|
||||
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]
|
||||
]
|
||||
];
|
||||
[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]
|
||||
];
|
||||
|
||||
Label(training, typeA, [1]);
|
||||
stages.push(training[0]);
|
||||
Forward(stages, layers);
|
||||
console.log(stages);
|
||||
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("NN.Label", ()=>
|
||||
{
|
||||
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");
|
||||
let labels = Label(input, layers);
|
||||
assertEquals(labels.length, output.length);
|
||||
assertEquals(labels[0].length, output[0].length);
|
||||
});
|
||||
|
||||
Deno.test("NN.Backward", ()=>
|
||||
Deno.test("NN.Learn", ()=>
|
||||
{
|
||||
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");
|
||||
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);
|
||||
});
|
||||
|
||||
|
||||
Deno.test("NN.Forward", ()=>
|
||||
{
|
||||
console.log(Forward(stages, layers));
|
||||
console.log(training[1]);
|
||||
});
|
||||
|
||||
|
||||
*/
|
94
nn.ts
94
nn.ts
@ -1,54 +1,78 @@
|
||||
import { default as M, Cloud } from "./m.ts";
|
||||
export type N = Array<Array<Array<number>>>
|
||||
|
||||
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 =>
|
||||
const Forward = (inData:Cloud.M, inLayers:N):N =>
|
||||
{
|
||||
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++)
|
||||
{
|
||||
inStages[i+1] = M.Mutate.Pad(process(i));
|
||||
}
|
||||
inStages[i+1] = process(i);
|
||||
return inStages[i+1];
|
||||
for(i=0; i<inLayers.length-1; i++){ stages[i+1] = M.Mutate.Pad(process(i)); }
|
||||
stages[i+1] = process(i);
|
||||
return stages;
|
||||
};
|
||||
const Backward = (inStages:N, inLayers:N, inGoals:Cloud.M, inRate:number):N =>
|
||||
{
|
||||
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--)
|
||||
{
|
||||
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));
|
||||
|
||||
let errorScaled:Cloud.M = M.Batch.Multiply(errorBack, M.Batch.Derivative(inStages[i+1]));
|
||||
errorBack = M.Batch.Affine(errorScaled, M.Create.Transpose(inLayers[i]));
|
||||
|
||||
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);
|
||||
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)
|
||||
);
|
||||
});
|
||||
|
||||
}
|
||||
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 type { Cloud };
|
||||
export { Split, Build, Label, Learn, Check, Forward, Backward };
|
Loading…
Reference in New Issue
Block a user