kinojs 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/main.js +86 -0
- package/package.json +19 -0
package/main.js
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
// minidependencies
|
|
2
|
+
|
|
3
|
+
const dot = (vectorA, vectorB) => {
|
|
4
|
+
return vectorA.reduce((acc, current, index) => {
|
|
5
|
+
return acc + current * vectorB[index];
|
|
6
|
+
}, 0);
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
// items in the library
|
|
10
|
+
|
|
11
|
+
let nn = [];
|
|
12
|
+
|
|
13
|
+
let t = {posdir: true, rewa: 1, prevrewa: 0, layer: 1, neuron: 1, bweight: -1}; // not in the library
|
|
14
|
+
|
|
15
|
+
export default {
|
|
16
|
+
nn,
|
|
17
|
+
init: (form) => {
|
|
18
|
+
// input layer
|
|
19
|
+
nn.push([]); // push layers
|
|
20
|
+
nn[0].push([]); // push output array
|
|
21
|
+
for(let i1 = 1; i1 < form[0] + 1; i1++) {
|
|
22
|
+
nn[0].push({weights: [], bias: 0}); // push neurons
|
|
23
|
+
for(let i2 = 0; i2 < form[0 - 1]; i2++) {
|
|
24
|
+
nn[0][i1].weights.push(1); // push weights
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
// hidden layers (includes output layer)
|
|
28
|
+
for(let i1 = 1; i1 < form.length; i1++) {
|
|
29
|
+
nn.push([]); // push layers
|
|
30
|
+
nn[i1].push([]); // push output array
|
|
31
|
+
for(let i2 = 1; i2 < form[i1] + 1; i2++) {
|
|
32
|
+
nn[i1].push({weights: [], bias: 0}); // push neurons
|
|
33
|
+
for(let i3 = 0; i3 < form[i1 - 1]; i3++) {
|
|
34
|
+
nn[i1][i2].weights.push(0); // push weights
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
run: (input) => {
|
|
40
|
+
nn[0][0] = input;
|
|
41
|
+
for(let i1 = 1; i1 < nn.length; i1++) {
|
|
42
|
+
|
|
43
|
+
nn[i1][0] = []; // reset layer output
|
|
44
|
+
for(let i2 = 1; i2 < nn[i1].length; i2++) {
|
|
45
|
+
|
|
46
|
+
nn[i1][0].push(dot(nn[i1 - 1][0], nn[i1][i2].weights) + nn[i1][i2].bias); // push neuron output to layer output
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return nn[nn.length - 1][0];
|
|
50
|
+
},
|
|
51
|
+
train: (amount) => {
|
|
52
|
+
if(t.rewa < t.prevrewa) {
|
|
53
|
+
t.posdir = !t.posdir;
|
|
54
|
+
}
|
|
55
|
+
let weigh;
|
|
56
|
+
if(t.posdir == true) {
|
|
57
|
+
weigh = amount;
|
|
58
|
+
} else {
|
|
59
|
+
weigh = 0 - amount;
|
|
60
|
+
}
|
|
61
|
+
if(t.bweight = -1) {
|
|
62
|
+
nn[t.layer][t.neuron].bias += weigh;
|
|
63
|
+
} else {
|
|
64
|
+
nn[t.layer][t.neuron].weights[t.bweight] += weigh;
|
|
65
|
+
}
|
|
66
|
+
if(t.bweight == nn[t.layer][t.neuron].weights.length - 1) {
|
|
67
|
+
if(t.neuron == nn[t.layer].length - 1) { // try minus 2 if don't work
|
|
68
|
+
if(t.layer == nn.length - 1) {
|
|
69
|
+
t.layer = 1; // layer set to one because input layer doesn't need training
|
|
70
|
+
} else {
|
|
71
|
+
t.layer += 1;
|
|
72
|
+
}
|
|
73
|
+
t.neuron = 1; // neuron set to one because output array is [0]
|
|
74
|
+
} else {
|
|
75
|
+
t.neuron += 1;
|
|
76
|
+
}
|
|
77
|
+
t.bweight = -1; // -1 because -1 means bias
|
|
78
|
+
} else {
|
|
79
|
+
t.bweight += 1;
|
|
80
|
+
}
|
|
81
|
+
},
|
|
82
|
+
reward: (amount) => {
|
|
83
|
+
t.prevrewa = t.rewa;
|
|
84
|
+
t.rewa = amount;
|
|
85
|
+
}
|
|
86
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "kinojs",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "an easy, flexable, lightweight ai framework",
|
|
5
|
+
"main": "main.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
8
|
+
},
|
|
9
|
+
"keywords": [
|
|
10
|
+
"ai",
|
|
11
|
+
"lightweight",
|
|
12
|
+
"framework",
|
|
13
|
+
"neural",
|
|
14
|
+
"network"
|
|
15
|
+
],
|
|
16
|
+
"author": "dopeycat",
|
|
17
|
+
"license": "UNLICENSED",
|
|
18
|
+
"type": "module"
|
|
19
|
+
}
|