-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOutputLayer.java
58 lines (48 loc) · 2.29 KB
/
OutputLayer.java
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
import java.util.Arrays;
import java.util.Objects;
public class OutputLayer extends HiddenLayer {
OutputLayerActivationFunctions function;
double[] nonNormalizedNeuronValues;
public OutputLayer(int inputSize, int outPutSize, OutputLayerActivationFunctions function) {
super(inputSize, outPutSize, HiddenLayerActivationFunction.SIGMOID);
this.function = function;
}
@Override
public void ForwardPropagate() {
if (previousLayerNeurons.length != getInputSize()) {
System.out.println("Error! previous layer size does not match the input size of this layer");
System.out.println("Previous layer size: " + previousLayerNeurons.length + " input size: " + inputSize);
} else {
for (int i = 0; i < getOutPutSize(); i++) {
double weightedSum = 0d;
for (int j = 0; j < getInputSize(); j++) {
weightedSum += layerWeights[i][j] * previousLayerNeurons[j];
}
super.neuronValues[i] = weightedSum + layerBiases[i];
}
nonNormalizedNeuronValues = super.neuronValues;
switch (function) {
case SOFTMAXX -> {
super.neuronValues = OutputLayerActivationFunctions.softMaxx(super.neuronValues);
}
}
}
}
public double[] calculateCostFunctionGradient(double[] output, int expectedIndex) {
// Compute gradients of MSE loss with respect to softmax output
double[] gradients = CostFunctions.getMSEGradients(output, expectedIndex);
return gradients;
}
public double[] calculateGradientsOutput(double[] gradients) {
double[] derivatives = null;
if (Objects.requireNonNull(function) == OutputLayerActivationFunctions.SOFTMAXX) {
derivatives = OutputLayerActivationFunctions.softMaxDerivatives(nonNormalizedNeuronValues, neuronValues);
// System.out.println("Softmaxx derivatives: " + Arrays.toString(derivatives));
// Multiply softmax derivatives by gradients of MSE loss with respect to softmax output
for (int i = 0; i < outPutSize; i++) {
derivatives[i] *= gradients[i];
}
}
return derivatives; // Return the computed derivatives
}
}