-
Notifications
You must be signed in to change notification settings - Fork 0
/
connection.js
94 lines (85 loc) · 1.67 KB
/
connection.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
92
93
94
/**
* Neural network
*
* Cody Smith
* 2019
* made for cs362
*
*
*/
function RELU(arr) {
return arr.map(a => {
return Math.max(0, a);
});
}
export function RELUprime(arr) {
if (!arr.length) arr = [arr];
let funk = x => {
if (x < 0) return 0;
return 1;
};
return arr.map(funk);
}
/**
*
* connections between network levels
*
*/
export class Connection {
constructor(sizeA, sizeB) {
this.weights = this.zeros(sizeA, sizeB);
}
connect(nodeA, nodeB, weight) {
this.weights[nodeA][nodeB] = weight;
}
connectList(list, weight) {
for (var i of list) {
this.connect(i[0], i[1], weight);
}
}
connectListToOne(list, nodeB, weight) {
for (var i of list) {
this.connect(i, nodeB, weight);
}
}
identity(list, weight = 1) {
for (var i of list) {
this.connect(i, i, weight);
}
}
fullyConnect(weight = 1, random = false) {
for (var i = 0; i < this.weights.length; i++) {
for (var j = 0; j < this.weights[0].length; j++) {
let w = weight;
if (random) w = Math.random();
this.weights[i][j] = w;
}
}
}
get(nodeA, nodeB) {
return this.weights[nodeA][nodeB];
}
// zeros
zeros(m, n) {
let res = [];
for (var i = 0; i < m; i++) {
let row = new Array(n);
for (var j = 0; j < n; j++) {
row[j] = 0;
}
res.push(row);
}
return res;
}
forwardPropogate(neuronsA) {
let z = numeric.dot(numeric.transpose(this.weights), neuronsA);
let activations = RELU(z);
return { activations, z };
}
toString() {
return this.weights;
}
size() {
return [this.weights.length, this.weights[0].length];
}
}