-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnit.java
More file actions
executable file
·47 lines (31 loc) · 981 Bytes
/
Copy pathUnit.java
File metadata and controls
executable file
·47 lines (31 loc) · 981 Bytes
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
public class Unit {
private double activation;
private double[] weights;
static final float lambda = 1.5f;
// main constructor
public Unit(int prevUnitsNo, java.util.Random rand) {
weights = new double[prevUnitsNo];
for (int i = 0; i < prevUnitsNo; ++i)
weights[i] = rand.nextFloat() - 0.5f;
}
public double activate(double inputs[]) {
activation = 0.0f;
assert(inputs.length == weights.length);
for (int i = 0; i < inputs.length; ++i) // dot product
activation += inputs[i] * weights[i];
return 2.0f / (1.0f + (float) Math.exp((-activation) * lambda)) - 1.0f;
}
public double getActivationDerivative() {
float expmlx = (float) Math.exp(lambda * activation);
return 2 * lambda * expmlx / ((1 + expmlx) * (1 + expmlx));
}
public double[] getWeights() {
return weights;
}
public double getWeight(int i) {
return weights[i];
}
public void setWeight(int i, double v) {
weights[i] = v;
}
}