-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathActivationFunction.java
More file actions
42 lines (35 loc) · 1.5 KB
/
Copy pathActivationFunction.java
File metadata and controls
42 lines (35 loc) · 1.5 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
package basicneuralnetwork.activationfunctions;
import org.ejml.simple.SimpleMatrix;
/**
* Created by KimFeichtinger on 20.04.18.
*/
public abstract class ActivationFunction {
// Activation function
public SimpleMatrix applyActivationFunctionToMatrix(SimpleMatrix input) {
SimpleMatrix output = new SimpleMatrix(input.numRows(), input.numCols());
for (int i = 0; i < input.numRows(); i++) {
for (int j = 0; j < input.numCols(); j ++) {
double value = input.get(i, j);
output.set(i, j, apply(value));
}
}
return output;
}
// Derivative of activation function (not real derivative because Activation function has already been applied to the input)
public SimpleMatrix applyDerivativeOfActivationFunctionToMatrix(SimpleMatrix input) {
SimpleMatrix output = new SimpleMatrix(input.numRows(), input.numCols());
for (int i = 0; i < input.numRows(); i++) {
for (int j = 0; j < input.numCols(); j ++) {
double value = input.get(i, j);
output.set(i, j, applyDerivative(value));
}
}
return output;
}
/** Applies the function to a single value */
protected abstract double apply(double value);
/** Applies the pseudo-derivative of the function to a single value */
protected abstract double applyDerivative(double value);
/** Returns the name of the function */
public abstract String getName();
}