-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLayer.java
More file actions
131 lines (116 loc) · 4.48 KB
/
Layer.java
File metadata and controls
131 lines (116 loc) · 4.48 KB
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
import java.util.Arrays;
public class Layer{
int numLayer;
double[] nodes;
double[][] weights;
double[] biases;
double[] unactivatedNeurons;
double[] prev_unactivatedNodes; // Z passed from the previous layer.
double[] dCdZ; // Derivative of cost with respect to the unactivatedNode values. Super important as it gets passed back and back.
double[] dCdZ_before; // Derivative of cost with respect to the unactivatedNodes of the Layer before it.
public Layer(int numNodes, int numBiases, int numLayer){
this.numLayer = numLayer;
nodes = new double[numNodes];
biases = new double[numBiases];
weights = new double[numBiases][numNodes];
initWeights();
}
public void initWeights(){
// Something to take a look at.
for(int i = 0; i < weights.length; i++){
biases[i] = Math.random()-0.5;
for(int j = 0; j < weights[0].length; j++){
weights[i][j] = Math.random()-0.5;
}
}
}
public static double[] MatrixVectorProduct(double[][] matrix, double[] vector){
double[] product = new double[matrix.length];
for(int i = 0; i < matrix.length; i++){
product[i] = innerProduct(matrix[i], vector);
}
return product;
}
public static double innerProduct(double[] vector, double[] weights){
double output = 0;
for(int i = 0; i < vector.length; i++){
output+=vector[i]*weights[i];
}
return output;
}
public static double[] elementProduct(double[] v1, double[] v2){
double[] product = new double[v1.length];
for(int i = 0; i < v1.length; i++){
product[i] = v1[i]*v2[i];
}
return product;
}
public static double[] addVectors(double[] v1, double[] v2, double alpha){
double[] sum = new double[v1.length];
for(int i = 0; i < v1.length; i++){
sum[i] = v1[i] + alpha*v2[i];
}
return sum;
}
private static double[][] addMatrices(double[][] m1, double[][] m2, double alpha){
double[][] sum = new double[m1.length][m1[0].length];
for(int i = 0; i < m1.length; i++){
for(int j = 0; j < m1[0].length; j++){
sum[i][j] = m1[i][j] + alpha*m2[i][j];
}
}
return sum;
}
public static double[][] transpose(double[][] m){
double[][] mt = new double[m[0].length][m.length];
for(int i = 0; i < m.length; i++){
for(int j = 0; j < m[0].length; j++){
mt[j][i] = m[i][j];
}
}
//System.out.println("transpose: " + mt.length + ", " + mt[0].length);
return mt;
}
public double[] feedForward(){
unactivatedNeurons = addVectors(MatrixVectorProduct(weights, nodes), biases, 1);
//System.out.println(Arrays.toString(unactivatedNeurons));
return unactivatedNeurons;
}
public static double[] reLU(double[] Z){
double[] A = new double[Z.length];
for(int i = 0; i < Z.length; i++){
A[i] = Math.max(0, Z[i]);
}
return A;
}
private void calculate_dCdZ(){
//System.out.println("Useful dCdZ size: " + dCdZ_before.length);
//System.out.println("Matrix of weights: " + weights.length + ", " + weights[0].length);
dCdZ = elementProduct(MatrixVectorProduct(transpose(weights), dCdZ_before), d_ReLU());
}
private double[] d_ReLU(){
double[] output = new double[prev_unactivatedNodes.length];
for(int i = 0; i < prev_unactivatedNodes.length; i++){
output[i] = prev_unactivatedNodes[i] > 0 ? 1.0 : 0.0;
}
//System.out.println("d_ReLU: " + output.length);
return output;
}
public void backProp(double alpha){
if(prev_unactivatedNodes != null){
calculate_dCdZ();
}
double[] dCdB = dCdZ_before;
double[][] dCdW = new double[weights.length][weights[0].length];
for(int i = 0; i < dCdZ_before.length; i++){
for(int j = 0; j < nodes.length; j++){
dCdW[i][j] = dCdZ_before[i]*nodes[j];
}
}
biases = addVectors(biases, dCdB, -alpha);
weights = addMatrices(weights, dCdW, -alpha);
}
public String toString(){
return "Layer :" + numLayer + "\n" + Arrays.toString(nodes);
}
}