-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathExample11.java
executable file
·66 lines (50 loc) · 1.8 KB
/
Example11.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
59
60
61
62
63
64
65
66
package examples.ml_examples.example11;
import org.nd4j.linalg.api.ndarray.INDArray;
import org.nd4j.linalg.factory.Nd4j;
import jstat.ml.models.HMMHelpers;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Example11 {
public static void main(String[] args){
// create the transition probability matrix
int nRows = 2;
int nColumns = 2;
INDArray A = Nd4j.zeros(nRows, nColumns);
A.putScalar(0,0, 0.7);
A.putScalar(0,1, 0.3);
A.putScalar(1,0, 0.4);
A.putScalar(1,1, 0.6);
// create the emission probability matrix
INDArray B = Nd4j.zeros(2, 3);
B.putScalar(0,0, 0.5);
B.putScalar(0,1, 0.4);
B.putScalar(0,2, 0.1);
B.putScalar(1,0, 0.1);
B.putScalar(1,1, 0.3);
B.putScalar(1,2, 0.6);
// create the initialization vector
INDArray pi = Nd4j.create(new double[]{0.6, 0.4});
// create a sequence of observations
List<String> sequence = new ArrayList<String>();
sequence.add("a");
sequence.add("b");
sequence.add("c");
// map the sequnce observation to a column index
Map<String, Integer> obsToIdx = new HashMap<>();
obsToIdx.put("a", 0);
obsToIdx.put("b", 1);
obsToIdx.put("c", 2);
// compute alpha
INDArray beta = HMMHelpers.backward(sequence, A, B, pi, obsToIdx);
System.out.println("beta matrix: ");
System.out.println(beta);
// we can now calculate the probability
double p = 0.0;
for(int i=0; i<A.shape()[0]; ++i){
p += pi.getDouble(0, i)*B.getDouble(i, obsToIdx.get(sequence.get(0)))*beta.getDouble(1, i);
}
System.out.println("probability: " + p);
}
}