Compare commits
4 Commits
b1c95d4819
...
3d9b692d4d
Author | SHA1 | Date | |
---|---|---|---|
3d9b692d4d | |||
9fcfb9115d | |||
44771715d3 | |||
3861d41c83 |
13
index.js
13
index.js
@ -1,11 +1,10 @@
|
|||||||
import { Build, Learn, Label } from "./nn.ts";
|
import { Build, Learn, Label } from "./nn.ts";
|
||||||
|
import { default as Clean } from "./iris.js";
|
||||||
|
|
||||||
const inputs = [[0.10, 0.50], [0.00, 0.06], [0.99, 0.85], [0.80, 0.95]];
|
let [ inputs, labels ] = Clean();
|
||||||
const labels = [[ 0, 1 ], [ 0, 1 ], [ 1, 0 ], [ 1, 0 ]];
|
|
||||||
|
|
||||||
const layers = Build(2, 5, 2);
|
let layers = Build(4, 10, 3);
|
||||||
const errors = Learn(inputs, layers, labels, 1000, 0.1);
|
let errors = Learn(inputs, layers, labels, 500, 0.1);
|
||||||
const output = Label(inputs, layers);
|
let output = Label(inputs, layers, true);
|
||||||
|
|
||||||
console.log("error after training:", errors);
|
console.log(output);
|
||||||
console.log("re-classified inputs:", output);
|
|
238
iris.js
Normal file
238
iris.js
Normal file
@ -0,0 +1,238 @@
|
|||||||
|
export default () =>
|
||||||
|
{
|
||||||
|
let inputs = [];
|
||||||
|
let labels = [];
|
||||||
|
|
||||||
|
let min = [999, 999, 999, 999];
|
||||||
|
let max = [-99, -99, -99, -99];
|
||||||
|
|
||||||
|
DataBig.split("\n").forEach((inRowValue, inRowIndex)=>
|
||||||
|
{
|
||||||
|
let currentInput = [];
|
||||||
|
let currentLabel = [];
|
||||||
|
|
||||||
|
if(inRowIndex == 0){ return; }
|
||||||
|
|
||||||
|
inRowValue.split(",").forEach((inCellValue, inCellIndex)=>
|
||||||
|
{
|
||||||
|
if(inCellIndex == 4)
|
||||||
|
{
|
||||||
|
switch(inCellValue)
|
||||||
|
{
|
||||||
|
case `"Setosa"`:
|
||||||
|
currentLabel = [1, 0, 0];
|
||||||
|
break;
|
||||||
|
case `"Versicolor"` :
|
||||||
|
currentLabel = [0, 1, 0];
|
||||||
|
break;
|
||||||
|
case `"Virginica"` :
|
||||||
|
currentLabel = [0, 0, 1];
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
let value = parseFloat(inCellValue);
|
||||||
|
if(min[inCellIndex] > value){ min[inCellIndex] = value; }
|
||||||
|
if(max[inCellIndex] < value){ max[inCellIndex] = value; }
|
||||||
|
currentInput.push(value);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
inputs.push(currentInput);
|
||||||
|
labels.push(currentLabel);
|
||||||
|
});
|
||||||
|
|
||||||
|
console.log(min, max);
|
||||||
|
inputs.forEach((inRowValue, inRowIndex)=>
|
||||||
|
{
|
||||||
|
inRowValue.forEach((inCellValue, inCellIndex)=>
|
||||||
|
{
|
||||||
|
inputs[inRowIndex][inCellIndex] = (inCellValue - min[inCellIndex])/(max[inCellIndex] - min[inCellIndex]);
|
||||||
|
});
|
||||||
|
})
|
||||||
|
|
||||||
|
return [ inputs, labels ];
|
||||||
|
};
|
||||||
|
const Data = `"sepal.length","sepal.width","petal.length","petal.width","variety"
|
||||||
|
5.1,3.5,1.4,.2,"Setosa"
|
||||||
|
4.9,3,1.4,.2,"Setosa"
|
||||||
|
4.7,3.2,1.3,.2,"Setosa"
|
||||||
|
4.6,3.1,1.5,.2,"Setosa"
|
||||||
|
5,3.6,1.4,.2,"Setosa"
|
||||||
|
5.4,3.9,1.7,.4,"Setosa"
|
||||||
|
4.6,3.4,1.4,.3,"Setosa"
|
||||||
|
7,3.2,4.7,1.4,"Versicolor"
|
||||||
|
6.4,3.2,4.5,1.5,"Versicolor"
|
||||||
|
6.9,3.1,4.9,1.5,"Versicolor"
|
||||||
|
5.5,2.3,4,1.3,"Versicolor"
|
||||||
|
6.5,2.8,4.6,1.5,"Versicolor"
|
||||||
|
5.7,2.8,4.5,1.3,"Versicolor"
|
||||||
|
6.3,3.3,4.7,1.6,"Versicolor"
|
||||||
|
4.9,2.4,3.3,1,"Versicolor"
|
||||||
|
6.6,2.9,4.6,1.3,"Versicolor"
|
||||||
|
5.2,2.7,3.9,1.4,"Versicolor"
|
||||||
|
5,2,3.5,1,"Versicolor"
|
||||||
|
5.7,2.5,5,2,"Virginica"
|
||||||
|
5.8,2.8,5.1,2.4,"Virginica"
|
||||||
|
6.4,3.2,5.3,2.3,"Virginica"
|
||||||
|
6.5,3,5.5,1.8,"Virginica"
|
||||||
|
7.7,3.8,6.7,2.2,"Virginica"
|
||||||
|
7.7,2.6,6.9,2.3,"Virginica"
|
||||||
|
6,2.2,5,1.5,"Virginica"
|
||||||
|
6.9,3.2,5.7,2.3,"Virginica"
|
||||||
|
5.6,2.8,4.9,2,"Virginica"
|
||||||
|
7.7,2.8,6.7,2,"Virginica"
|
||||||
|
6.3,2.7,4.9,1.8,"Virginica"
|
||||||
|
6.7,3.3,5.7,2.1,"Virginica"`;
|
||||||
|
const DataBig = `"sepal.length","sepal.width","petal.length","petal.width","variety"
|
||||||
|
5.1,3.5,1.4,.2,"Setosa"
|
||||||
|
4.9,3,1.4,.2,"Setosa"
|
||||||
|
4.7,3.2,1.3,.2,"Setosa"
|
||||||
|
4.6,3.1,1.5,.2,"Setosa"
|
||||||
|
5,3.6,1.4,.2,"Setosa"
|
||||||
|
5.4,3.9,1.7,.4,"Setosa"
|
||||||
|
4.6,3.4,1.4,.3,"Setosa"
|
||||||
|
5,3.4,1.5,.2,"Setosa"
|
||||||
|
4.4,2.9,1.4,.2,"Setosa"
|
||||||
|
4.9,3.1,1.5,.1,"Setosa"
|
||||||
|
5.4,3.7,1.5,.2,"Setosa"
|
||||||
|
4.8,3.4,1.6,.2,"Setosa"
|
||||||
|
4.8,3,1.4,.1,"Setosa"
|
||||||
|
4.3,3,1.1,.1,"Setosa"
|
||||||
|
5.8,4,1.2,.2,"Setosa"
|
||||||
|
5.7,4.4,1.5,.4,"Setosa"
|
||||||
|
5.4,3.9,1.3,.4,"Setosa"
|
||||||
|
5.1,3.5,1.4,.3,"Setosa"
|
||||||
|
5.7,3.8,1.7,.3,"Setosa"
|
||||||
|
5.1,3.8,1.5,.3,"Setosa"
|
||||||
|
5.4,3.4,1.7,.2,"Setosa"
|
||||||
|
5.1,3.7,1.5,.4,"Setosa"
|
||||||
|
4.6,3.6,1,.2,"Setosa"
|
||||||
|
5.1,3.3,1.7,.5,"Setosa"
|
||||||
|
4.8,3.4,1.9,.2,"Setosa"
|
||||||
|
5,3,1.6,.2,"Setosa"
|
||||||
|
5,3.4,1.6,.4,"Setosa"
|
||||||
|
5.2,3.5,1.5,.2,"Setosa"
|
||||||
|
5.2,3.4,1.4,.2,"Setosa"
|
||||||
|
4.7,3.2,1.6,.2,"Setosa"
|
||||||
|
4.8,3.1,1.6,.2,"Setosa"
|
||||||
|
5.4,3.4,1.5,.4,"Setosa"
|
||||||
|
5.2,4.1,1.5,.1,"Setosa"
|
||||||
|
5.5,4.2,1.4,.2,"Setosa"
|
||||||
|
4.9,3.1,1.5,.2,"Setosa"
|
||||||
|
5,3.2,1.2,.2,"Setosa"
|
||||||
|
5.5,3.5,1.3,.2,"Setosa"
|
||||||
|
4.9,3.6,1.4,.1,"Setosa"
|
||||||
|
4.4,3,1.3,.2,"Setosa"
|
||||||
|
5.1,3.4,1.5,.2,"Setosa"
|
||||||
|
5,3.5,1.3,.3,"Setosa"
|
||||||
|
4.5,2.3,1.3,.3,"Setosa"
|
||||||
|
4.4,3.2,1.3,.2,"Setosa"
|
||||||
|
5,3.5,1.6,.6,"Setosa"
|
||||||
|
5.1,3.8,1.9,.4,"Setosa"
|
||||||
|
4.8,3,1.4,.3,"Setosa"
|
||||||
|
5.1,3.8,1.6,.2,"Setosa"
|
||||||
|
4.6,3.2,1.4,.2,"Setosa"
|
||||||
|
5.3,3.7,1.5,.2,"Setosa"
|
||||||
|
5,3.3,1.4,.2,"Setosa"
|
||||||
|
7,3.2,4.7,1.4,"Versicolor"
|
||||||
|
6.4,3.2,4.5,1.5,"Versicolor"
|
||||||
|
6.9,3.1,4.9,1.5,"Versicolor"
|
||||||
|
5.5,2.3,4,1.3,"Versicolor"
|
||||||
|
6.5,2.8,4.6,1.5,"Versicolor"
|
||||||
|
5.7,2.8,4.5,1.3,"Versicolor"
|
||||||
|
6.3,3.3,4.7,1.6,"Versicolor"
|
||||||
|
4.9,2.4,3.3,1,"Versicolor"
|
||||||
|
6.6,2.9,4.6,1.3,"Versicolor"
|
||||||
|
5.2,2.7,3.9,1.4,"Versicolor"
|
||||||
|
5,2,3.5,1,"Versicolor"
|
||||||
|
5.9,3,4.2,1.5,"Versicolor"
|
||||||
|
6,2.2,4,1,"Versicolor"
|
||||||
|
6.1,2.9,4.7,1.4,"Versicolor"
|
||||||
|
5.6,2.9,3.6,1.3,"Versicolor"
|
||||||
|
6.7,3.1,4.4,1.4,"Versicolor"
|
||||||
|
5.6,3,4.5,1.5,"Versicolor"
|
||||||
|
5.8,2.7,4.1,1,"Versicolor"
|
||||||
|
6.2,2.2,4.5,1.5,"Versicolor"
|
||||||
|
5.6,2.5,3.9,1.1,"Versicolor"
|
||||||
|
5.9,3.2,4.8,1.8,"Versicolor"
|
||||||
|
6.1,2.8,4,1.3,"Versicolor"
|
||||||
|
6.3,2.5,4.9,1.5,"Versicolor"
|
||||||
|
6.1,2.8,4.7,1.2,"Versicolor"
|
||||||
|
6.4,2.9,4.3,1.3,"Versicolor"
|
||||||
|
6.6,3,4.4,1.4,"Versicolor"
|
||||||
|
6.8,2.8,4.8,1.4,"Versicolor"
|
||||||
|
6.7,3,5,1.7,"Versicolor"
|
||||||
|
6,2.9,4.5,1.5,"Versicolor"
|
||||||
|
5.7,2.6,3.5,1,"Versicolor"
|
||||||
|
5.5,2.4,3.8,1.1,"Versicolor"
|
||||||
|
5.5,2.4,3.7,1,"Versicolor"
|
||||||
|
5.8,2.7,3.9,1.2,"Versicolor"
|
||||||
|
6,2.7,5.1,1.6,"Versicolor"
|
||||||
|
5.4,3,4.5,1.5,"Versicolor"
|
||||||
|
6,3.4,4.5,1.6,"Versicolor"
|
||||||
|
6.7,3.1,4.7,1.5,"Versicolor"
|
||||||
|
6.3,2.3,4.4,1.3,"Versicolor"
|
||||||
|
5.6,3,4.1,1.3,"Versicolor"
|
||||||
|
5.5,2.5,4,1.3,"Versicolor"
|
||||||
|
5.5,2.6,4.4,1.2,"Versicolor"
|
||||||
|
6.1,3,4.6,1.4,"Versicolor"
|
||||||
|
5.8,2.6,4,1.2,"Versicolor"
|
||||||
|
5,2.3,3.3,1,"Versicolor"
|
||||||
|
5.6,2.7,4.2,1.3,"Versicolor"
|
||||||
|
5.7,3,4.2,1.2,"Versicolor"
|
||||||
|
5.7,2.9,4.2,1.3,"Versicolor"
|
||||||
|
6.2,2.9,4.3,1.3,"Versicolor"
|
||||||
|
5.1,2.5,3,1.1,"Versicolor"
|
||||||
|
5.7,2.8,4.1,1.3,"Versicolor"
|
||||||
|
6.3,3.3,6,2.5,"Virginica"
|
||||||
|
5.8,2.7,5.1,1.9,"Virginica"
|
||||||
|
7.1,3,5.9,2.1,"Virginica"
|
||||||
|
6.3,2.9,5.6,1.8,"Virginica"
|
||||||
|
6.5,3,5.8,2.2,"Virginica"
|
||||||
|
7.6,3,6.6,2.1,"Virginica"
|
||||||
|
4.9,2.5,4.5,1.7,"Virginica"
|
||||||
|
7.3,2.9,6.3,1.8,"Virginica"
|
||||||
|
6.7,2.5,5.8,1.8,"Virginica"
|
||||||
|
7.2,3.6,6.1,2.5,"Virginica"
|
||||||
|
6.5,3.2,5.1,2,"Virginica"
|
||||||
|
6.4,2.7,5.3,1.9,"Virginica"
|
||||||
|
6.8,3,5.5,2.1,"Virginica"
|
||||||
|
5.7,2.5,5,2,"Virginica"
|
||||||
|
5.8,2.8,5.1,2.4,"Virginica"
|
||||||
|
6.4,3.2,5.3,2.3,"Virginica"
|
||||||
|
6.5,3,5.5,1.8,"Virginica"
|
||||||
|
7.7,3.8,6.7,2.2,"Virginica"
|
||||||
|
7.7,2.6,6.9,2.3,"Virginica"
|
||||||
|
6,2.2,5,1.5,"Virginica"
|
||||||
|
6.9,3.2,5.7,2.3,"Virginica"
|
||||||
|
5.6,2.8,4.9,2,"Virginica"
|
||||||
|
7.7,2.8,6.7,2,"Virginica"
|
||||||
|
6.3,2.7,4.9,1.8,"Virginica"
|
||||||
|
6.7,3.3,5.7,2.1,"Virginica"
|
||||||
|
7.2,3.2,6,1.8,"Virginica"
|
||||||
|
6.2,2.8,4.8,1.8,"Virginica"
|
||||||
|
6.1,3,4.9,1.8,"Virginica"
|
||||||
|
6.4,2.8,5.6,2.1,"Virginica"
|
||||||
|
7.2,3,5.8,1.6,"Virginica"
|
||||||
|
7.4,2.8,6.1,1.9,"Virginica"
|
||||||
|
7.9,3.8,6.4,2,"Virginica"
|
||||||
|
6.4,2.8,5.6,2.2,"Virginica"
|
||||||
|
6.3,2.8,5.1,1.5,"Virginica"
|
||||||
|
6.1,2.6,5.6,1.4,"Virginica"
|
||||||
|
7.7,3,6.1,2.3,"Virginica"
|
||||||
|
6.3,3.4,5.6,2.4,"Virginica"
|
||||||
|
6.4,3.1,5.5,1.8,"Virginica"
|
||||||
|
6,3,4.8,1.8,"Virginica"
|
||||||
|
6.9,3.1,5.4,2.1,"Virginica"
|
||||||
|
6.7,3.1,5.6,2.4,"Virginica"
|
||||||
|
6.9,3.1,5.1,2.3,"Virginica"
|
||||||
|
5.8,2.7,5.1,1.9,"Virginica"
|
||||||
|
6.8,3.2,5.9,2.3,"Virginica"
|
||||||
|
6.7,3.3,5.7,2.5,"Virginica"
|
||||||
|
6.7,3,5.2,2.3,"Virginica"
|
||||||
|
6.3,2.5,5,1.9,"Virginica"
|
||||||
|
6.5,3,5.2,2,"Virginica"
|
||||||
|
6.2,3.4,5.4,2.3,"Virginica"
|
||||||
|
5.9,3,5.1,1.8,"Virginica"`;
|
31
m.test.js
31
m.test.js
@ -11,10 +11,10 @@ Deno.test("Iterate.Loop", ()=>
|
|||||||
assertEquals(cloud[0][0], 0);
|
assertEquals(cloud[0][0], 0);
|
||||||
assertEquals(cloud[3][2], 5, "correct output");
|
assertEquals(cloud[3][2], 5, "correct output");
|
||||||
});
|
});
|
||||||
Deno.test("Iterate.Edit", ()=>
|
Deno.test("Iterate.Copy", ()=>
|
||||||
{
|
{
|
||||||
const c = [[1, 2], [3, 4]]
|
const c = [[1, 2], [3, 4]]
|
||||||
const t = M.Iterate.Edit(c, (i)=>i);
|
const t = M.Iterate.Copy(c, (i)=>i);
|
||||||
assertEquals(t.length, c.length, "correct count");
|
assertEquals(t.length, c.length, "correct count");
|
||||||
assertEquals(t[0][0], c[0][0], "correct dimensions");
|
assertEquals(t[0][0], c[0][0], "correct dimensions");
|
||||||
assertEquals(t[1][1], c[1][1], "correct placement");
|
assertEquals(t[1][1], c[1][1], "correct placement");
|
||||||
@ -146,20 +146,39 @@ Deno.test("Batch.Subtract", ()=>
|
|||||||
assertEquals(t[0].length, 2, "correct dimensions");
|
assertEquals(t[0].length, 2, "correct dimensions");
|
||||||
assertEquals(t[1][0], 2.5, "correct placement");
|
assertEquals(t[1][0], 2.5, "correct placement");
|
||||||
});
|
});
|
||||||
Deno.test("Batch.Sigmoid", ()=>
|
Deno.test("Batch.Sig", ()=>
|
||||||
{
|
{
|
||||||
const m = [[-1000, 1000]];
|
const m = [[-1000, 1000]];
|
||||||
const t = M.Batch.Sigmoid(m);
|
const t = M.Batch.Sig(m);
|
||||||
assertEquals(t.length, 1, "correct count");
|
assertEquals(t.length, 1, "correct count");
|
||||||
assertEquals(t[0].length, 2, "correct dimensions");
|
assertEquals(t[0].length, 2, "correct dimensions");
|
||||||
assert(t[0][0]>=0 && t[0][0]<0.5);
|
assert(t[0][0]>=0 && t[0][0]<0.5);
|
||||||
assert(t[0][1]<=1 && t[0][1]>0.5, "correct placement");
|
assert(t[0][1]<=1 && t[0][1]>0.5, "correct placement");
|
||||||
|
|
||||||
});
|
});
|
||||||
Deno.test("Batch.Derivative", ()=>
|
Deno.test("Batch.SigDeriv", ()=>
|
||||||
{
|
{
|
||||||
const m = [[-1000, 0, 1000]];
|
const m = [[-1000, 0, 1000]];
|
||||||
const t = M.Batch.Derivative(M.Batch.Sigmoid(m));
|
const t = M.Batch.SigDeriv(M.Batch.Sig(m));
|
||||||
assertEquals(t.length, 1, "correct count");
|
assertEquals(t.length, 1, "correct count");
|
||||||
assertEquals(t[0].length, 3, "correct dimensions");
|
assertEquals(t[0].length, 3, "correct dimensions");
|
||||||
assert(t[0][0]<t[0][1] && t[0][1]>t[0][2]);
|
assert(t[0][0]<t[0][1] && t[0][1]>t[0][2]);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Deno.test("Batch.Rec", ()=>
|
||||||
|
{
|
||||||
|
const m = [[-1, 1, 10]];
|
||||||
|
const t = M.Batch.Rec(m);
|
||||||
|
assert(t[0][0] == 0);
|
||||||
|
assert(t[0][1] == 1);
|
||||||
|
assert(t[0][2] == 10);
|
||||||
|
});
|
||||||
|
Deno.test("Batch.RecDeriv", ()=>
|
||||||
|
{
|
||||||
|
const m = [[-1, 1, 10]];
|
||||||
|
const t = M.Batch.RecDeriv(m);
|
||||||
|
|
||||||
|
assert(t[0][0] == 0);
|
||||||
|
assert(t[0][1] == 1);
|
||||||
|
assert(t[0][2] == 1);
|
||||||
|
});
|
25
m.ts
25
m.ts
@ -16,22 +16,20 @@ const Methods = {
|
|||||||
for(i=0; i<inCount; i++)
|
for(i=0; i<inCount; i++)
|
||||||
{
|
{
|
||||||
outputVector = [];
|
outputVector = [];
|
||||||
for(j=0; j<inDimensions; j++)
|
for(j=0; j<inDimensions; j++){ outputVector.push(inFunction(j, i, outputVector)); }
|
||||||
{
|
|
||||||
outputVector.push(inFunction(j, i, outputVector));
|
|
||||||
}
|
|
||||||
outputCloud.push(outputVector);
|
outputCloud.push(outputVector);
|
||||||
}
|
}
|
||||||
return outputCloud;
|
return outputCloud;
|
||||||
},
|
},
|
||||||
Edit: (inCloud:Cloud.M, inFunction:Cloud.HandleEdit):Cloud.M=> inCloud.map((row:Cloud.V):Cloud.V=>row.map(inFunction))
|
Copy: (inCloud:Cloud.M, inFunction:Cloud.HandleEdit):Cloud.M=> inCloud.map((row:Cloud.V):Cloud.V=> row.map(inFunction)),
|
||||||
|
Edit: (inCloud:Cloud.M, inFunction:Cloud.HandleEdit):void => inCloud.forEach((row:Cloud.V):void=>row.forEach(inFunction))
|
||||||
},
|
},
|
||||||
Create:
|
Create:
|
||||||
{
|
{
|
||||||
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()),
|
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]),
|
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]),
|
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),
|
Clone: (inCloud:Cloud.M):Cloud.M=> Methods.Iterate.Copy(inCloud, i=> i),
|
||||||
Padded: (inCloud:Cloud.M):Cloud.M=> inCloud.map((row:Cloud.V)=> [...row, 1])
|
Padded: (inCloud:Cloud.M):Cloud.M=> inCloud.map((row:Cloud.V)=> [...row, 1])
|
||||||
},
|
},
|
||||||
Mutate:
|
Mutate:
|
||||||
@ -39,13 +37,6 @@ const Methods = {
|
|||||||
Pad: (inCloud:Cloud.M):Cloud.M=> {inCloud.forEach((row:Cloud.V)=> row.push(1)); return inCloud; },
|
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; }
|
Unpad: (inCloud:Cloud.M):Cloud.M=> {inCloud.forEach((row:Cloud.V)=> row.pop()); return inCloud; }
|
||||||
},
|
},
|
||||||
Test:
|
|
||||||
{
|
|
||||||
Dot:(v1:Cloud.V, v2:Cloud.V):number=>
|
|
||||||
{
|
|
||||||
return v1.reduce((sum, current, index)=> sum + current*v2[index]);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
Single:
|
Single:
|
||||||
{
|
{
|
||||||
Subtract: (inV1:Cloud.V, inV2:Cloud.V):Cloud.V=> inV1.map((component, i)=> component-inV2[i]),
|
Subtract: (inV1:Cloud.V, inV2:Cloud.V):Cloud.V=> inV1.map((component, i)=> component-inV2[i]),
|
||||||
@ -57,9 +48,11 @@ const Methods = {
|
|||||||
Subtract: (inCloud1:Cloud.M, inCloud2:Cloud.M):Cloud.M=> inCloud1.map((row:Cloud.V, rowIndex:number)=> Methods.Single.Subtract(row, inCloud2[rowIndex])),
|
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])),
|
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)),
|
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))),
|
Sig: (inCloud:Cloud.M):Cloud.M=> Methods.Iterate.Copy(inCloud, i=>1/(1+Math.pow(Math.E, -i))),
|
||||||
Derivative: (inCloud:Cloud.M):Cloud.M=> Methods.Iterate.Edit(inCloud, i=>i*(1-i)),
|
SigDeriv: (inCloud:Cloud.M):Cloud.M=> Methods.Iterate.Copy(inCloud, i=>i*(1-i)),
|
||||||
Scale: (inCloud:Cloud.M, inScalar:number):Cloud.M=> Methods.Iterate.Edit(inCloud, i=>i*inScalar)
|
Rec: (inCloud:Cloud.M):Cloud.M=> Methods.Iterate.Copy(inCloud, i=> i<=0 ? 0 : i),
|
||||||
|
RecDeriv: (inCloud:Cloud.M):Cloud.M=> Methods.Iterate.Copy(inCloud, i=> i<=0 ? 0 : 1),
|
||||||
|
Scale: (inCloud:Cloud.M, inScalar:number):Cloud.M=> Methods.Iterate.Copy(inCloud, i=>i*inScalar)
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -17,7 +17,6 @@ Deno.test("NN.Split", ()=>
|
|||||||
assert(input);
|
assert(input);
|
||||||
assert(output);
|
assert(output);
|
||||||
assertEquals(input.length, output.length, "data split into equal input and output");
|
assertEquals(input.length, output.length, "data split into equal input and output");
|
||||||
console.log(output);
|
|
||||||
|
|
||||||
assertEquals(input[0].length, 2, "unpadded input");
|
assertEquals(input[0].length, 2, "unpadded input");
|
||||||
assertEquals(output[0].length, 2, "unpadded output");
|
assertEquals(output[0].length, 2, "unpadded output");
|
||||||
@ -44,7 +43,8 @@ Deno.test("NN.Label", ()=>
|
|||||||
|
|
||||||
Deno.test("NN.Learn", ()=>
|
Deno.test("NN.Learn", ()=>
|
||||||
{
|
{
|
||||||
let error = Learn(input, layers, output, 1000, 0.1);
|
let error = Learn(input, layers, output, 50, 0.2);
|
||||||
|
console.log(error);
|
||||||
assertEquals(error.length, output.length);
|
assertEquals(error.length, output.length);
|
||||||
let total = 0;
|
let total = 0;
|
||||||
let count = error.length*error[0].length;
|
let count = error.length*error[0].length;
|
||||||
|
21
nn.ts
21
nn.ts
@ -5,7 +5,8 @@ const Forward = (inData:Cloud.M, inLayers:N):N =>
|
|||||||
{
|
{
|
||||||
let i:number;
|
let i:number;
|
||||||
let stages:N = [inData];
|
let stages:N = [inData];
|
||||||
let process = (index:number):Cloud.M => M.Batch.Sigmoid(M.Batch.Affine(stages[index], inLayers[index]));
|
let nonLinear = (inIndex:number):any=> inIndex >= inLayers.length-1 ? M.Batch.Sig : M.Batch.Rec;
|
||||||
|
let process = (index:number):Cloud.M => nonLinear(index)(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] = M.Mutate.Pad(process(i)); }
|
||||||
stages[i+1] = process(i);
|
stages[i+1] = process(i);
|
||||||
@ -15,10 +16,11 @@ 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(inStages[inStages.length-1], inGoals);
|
||||||
|
let nonLinear = (inIndex:number):any=> inIndex >= inLayers.length-1 ? M.Batch.SigDeriv : M.Batch.RecDeriv;
|
||||||
|
|
||||||
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 errorScaled:Cloud.M = M.Batch.Multiply(errorBack, nonLinear(i)(inStages[i+1]));
|
||||||
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)=>
|
||||||
{
|
{
|
||||||
@ -56,10 +58,21 @@ const Build = (...inLayers:Array<number>):N =>
|
|||||||
}
|
}
|
||||||
return output;
|
return output;
|
||||||
};
|
};
|
||||||
const Label = (inData:Cloud.M, inLayers:N):Cloud.M =>
|
const Label = (inData:Cloud.M, inLayers:N, inRound:boolean):Cloud.M =>
|
||||||
{
|
{
|
||||||
let stages:N = Forward(M.Create.Padded(inData), inLayers);
|
let stages:N = Forward(M.Create.Padded(inData), inLayers);
|
||||||
return stages[stages.length-1];
|
let output = stages[stages.length-1];
|
||||||
|
if(inRound)
|
||||||
|
{
|
||||||
|
output.forEach(row=>
|
||||||
|
{
|
||||||
|
row.forEach((cell, i)=>
|
||||||
|
{
|
||||||
|
row[i] = (Math.round(cell * 100) / 100);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return output;
|
||||||
};
|
};
|
||||||
const Learn = (inData:Cloud.M, inLayers:N, inLabels:Cloud.M, inIterations:number, inRate:number):Cloud.M =>
|
const Learn = (inData:Cloud.M, inLayers:N, inLabels:Cloud.M, inIterations:number, inRate:number):Cloud.M =>
|
||||||
{
|
{
|
||||||
|
Loading…
Reference in New Issue
Block a user