-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
91 lines (88 loc) · 2.29 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/**
*
* tests
*
*
*/
import { Connection } from "./connection.js";
import { Network } from "./Network.js";
import {
getBatch,
convertBatchToInputAndOutput
} from "./generateTraingingData.js";
export function test() {
var n1 = new Connection(3, 5);
n1.connect(0, 0, 1);
n1.connect(1, 1, 2);
n1.connect(2, 2, 3);
n1.connect(0, 4, 1);
n1.connect(1, 4, 1);
n1.connect(2, 4, 1);
console.log(n1.weights);
let p2 = n1.forwardPropogate([1, 1, 1]).activations;
console.log({ activations: p2, weights: n1.weights });
console.assert(p2[4] == 3);
console.assert(p2[1] == 2);
//
let c2 = new Connection(6, 6);
c2.identity([0, 1, 2], 3);
c2.connectList([[3, 3], [4, 4], [5, 5]], 5);
console.log(c2);
// try XOR
let nxor = new Network();
let c3 = new Connection(3, 3);
c3.connectListToOne([1, 2], 1, -1);
c3.connectListToOne([1, 2], 2, 1);
c3.connect(0, 0, 1);
c3.connect(0, 1, 1);
c3.connect(0, 2, -1);
nxor.addConnection(c3);
let c4 = new Connection(3, 1);
c4.connect(0, 0, 1);
c4.connect(1, 0, -1);
c4.connect(2, 0, -2);
nxor.addConnection(c4);
for (var i of [0, 1]) {
for (var j of [0, 1]) {
let res = nxor.forwardPropogate([1, i, j]);
let solution = res.activations[2][0];
console.assert(i ^ (j == solution), "xor fail");
console.log(i, "xor", j, " = ", solution);
}
}
let canxor = nxor.draw();
document.body.appendChild(canxor);
//
// computeGradient
var defaultInput = [1, 1, 1, 1, 1, 1, 1]; // [0, 0, 0, 1, 0, 1, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0]; // 5, 13, 2
const net = new Network();
const input = [1].concat(defaultInput);
let c1 = new Connection(7, 11); //give me some space
c1.fullyConnect(undefined, true);
net.addConnection(c1);
c2 = new Connection(11, 13);
c2.fullyConnect(undefined, true);
net.addConnection(c2);
c3 = new Connection(13, 3);
c3.fullyConnect(undefined, true);
net.addConnection(c3);
let bob = net.forwardPropogate(input);
console.log({
weights: net.connections,
activations: bob.activations,
zs: bob.zs
});
let grr = net.calculateGradient(
[4, 5, 6],
bob.activations,
bob.zs,
net.connections,
[1, 2, 3]
);
console.log("gradient maybe", grr);
//
console.log("duplicate", net, "-->", net.duplicate());
//
//
}
// setTimeout(test, 10);