forked from evolvingstuff/RecurrentJava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRnnLayer.java
More file actions
58 lines (42 loc) · 1.21 KB
/
Copy pathRnnLayer.java
File metadata and controls
58 lines (42 loc) · 1.21 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
package model;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import matrix.Matrix;
import autodiff.Graph;
public class RnnLayer implements Model {
private static final long serialVersionUID = 1L;
int inputDimension;
int outputDimension;
Matrix W, b;
Matrix context;
Nonlinearity f;
public RnnLayer(int inputDimension, int outputDimension, Nonlinearity hiddenUnit, double initParamsStdDev, Random rng) {
this.inputDimension = inputDimension;
this.outputDimension = outputDimension;
this.f = hiddenUnit;
W = Matrix.rand(outputDimension, inputDimension+outputDimension, initParamsStdDev, rng);
b = new Matrix(outputDimension);
}
@Override
public Matrix forward(Matrix input, Graph g) throws Exception {
Matrix concat = g.concatVectors(input, context);
Matrix sum = g.mul(W, concat);
sum = g.add(sum, b);
Matrix output = g.nonlin(f, sum);
//rollover activations for next iteration
context = output;
return output;
}
@Override
public void resetState() {
context = new Matrix(outputDimension);
}
@Override
public List<Matrix> getParameters() {
List<Matrix> result = new ArrayList<>();
result.add(W);
result.add(b);
return result;
}
}