-
Notifications
You must be signed in to change notification settings - Fork 28
/
NetMatrix.pde
139 lines (121 loc) · 3.33 KB
/
NetMatrix.pde
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
class NetMatrix {
int rows, cols;
float[][] NetMatrix;
NetMatrix(int r, int c) {
rows = r;
cols = c;
NetMatrix = new float[rows][cols];
}
NetMatrix(float[][] m) {
NetMatrix = m;
rows = NetMatrix.length;
cols = NetMatrix[0].length;
}
void output() {
for(int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++) {
print(NetMatrix[i][j] + " ");
}
println();
}
println();
}
NetMatrix dot(NetMatrix n) {
NetMatrix result = new NetMatrix(rows, n.cols);
if(cols == n.rows) {
for(int i = 0; i < rows; i++) {
for(int j = 0; j < n.cols; j++) {
float sum = 0;
for(int k = 0; k < cols; k++) {
sum += NetMatrix[i][k]*n.NetMatrix[k][j];
}
result.NetMatrix[i][j] = sum;
}
}
}
return result;
}
void randomize() {
for(int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++) {
NetMatrix[i][j] = random(-1,1);
}
}
}
NetMatrix single_column_net_matrix_from_array(float[] arr) {
NetMatrix n = new NetMatrix(arr.length, 1);
for(int i = 0; i < arr.length; i++) {
n.NetMatrix[i][0] = arr[i];
}
return n;
}
float[] to_array() {
float[] arr = new float[rows*cols];
for(int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++) {
arr[j+i*cols] = NetMatrix[i][j];
}
}
return arr;
}
NetMatrix add_bias() {
NetMatrix n = new NetMatrix(rows+1, 1);
for(int i = 0; i < rows; i++) {
n.NetMatrix[i][0] = NetMatrix[i][0];
}
n.NetMatrix[rows][0] = 1;
return n;
}
NetMatrix activate() {
NetMatrix n = new NetMatrix(rows, cols);
for(int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++) {
n.NetMatrix[i][j] = relu(NetMatrix[i][j]);
}
}
return n;
}
float relu(float x) {
return max(0,x);
}
void mutate(float mutationRate) {
for(int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++) {
float rand = random(1);
if(rand<mutationRate) {
NetMatrix[i][j] += randomGaussian()/5;
if(NetMatrix[i][j] > 1) {
NetMatrix[i][j] = 1;
}
if(NetMatrix[i][j] <-1) {
NetMatrix[i][j] = -1;
}
}
}
}
}
NetMatrix crossover(NetMatrix partner) {
NetMatrix child = new NetMatrix(rows, cols);
int randC = floor(random(cols));
int randR = floor(random(rows));
for(int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++) {
if((i < randR) || (i == randR && j <= randC)) {
child.NetMatrix[i][j] = NetMatrix[i][j];
} else {
child.NetMatrix[i][j] = partner.NetMatrix[i][j];
}
}
}
return child;
}
NetMatrix clone() {
NetMatrix clone = new NetMatrix(rows, cols);
for(int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++) {
clone.NetMatrix[i][j] = NetMatrix[i][j];
}
}
return clone;
}
}