-
Notifications
You must be signed in to change notification settings - Fork 0
/
hmm.js
197 lines (181 loc) · 6.91 KB
/
hmm.js
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
if (typeof define !== 'function') {
var define = require('amdefine')(module);
}
define(['./features'], function(Features) {
var tolog = function(x) {
return -Math.log(x)*2371.8;
};
var fromlog = function(x) {
return Math.exp(-x/2371.8);
};
var Token = function(model, state) {
this.model = model;
this.state = state;
};
var omerge = function() {
var r = arguments.length ? arguments[0] : {}, o;
for (var i=1; i<arguments.length; i++) {
o = arguments[i];
for (name in o) {
if (o.hasOwnProperty(name)) {
r[name] = o[name];
}
}
}
return r;
};
var extract_models = function(hmmdef, mkmodel, process_codebook) {
var globals = {};
var models = [];
for (var i=0; i<hmmdef.length; i++) {
switch(hmmdef[i].type) {
case '<codebook>':
process_codebook(hmmdef[i].value);
break;
case '~o':
globals = omerge(globals, hmmdef[i].value);
break;
case '~h':
models.push(mkmodel(hmmdef[i].name, globals, hmmdef[i].value));
break;
default:
// ignore other definitions for now.
// XXX in the future we might want to expand macro references
break;
}
}
return models;
};
var make_discrete_recog = function(hmmdef) {
var vq_features;
var process_codebook = function(codebook) {
vq_features = Features.make_vq(codebook);
};
var expand_weightlist = function(a) {
var r = [];
for (var i=0; i<a.length; i++) {
for (var j=0; j<a[i][1]; j++) {
r.push(a[i][0]);
}
}
return r;
};
var mkmodel = function(name, globals, def) {
var states = [], i, j;
// process output probabilities
states.push({ id: 0, start: true, pred: [] }); /* entry state */
for (i=2; i < def.NumStates; i++) {
states.push({
id: states.length,
output: def.States[i].Streams.map(function(d) {
return expand_weightlist(d.DProb);
}),
// XXX we ignore stream weights
weights: def.States[i].SWeights,
pred: []
});
def.States[i].NumMixes.forEach(function(len, j) {
console.assert(states[i-1].output[j].length===len);
});
}
states.push({ id: states.length, pred: [] }); /* exit state */
// process transition matrix
console.assert(def.TransP.type==='square');
console.assert(def.TransP.rows===def.NumStates);
for (i=0; i < def.NumStates-1; i++) { /* from state */
for (j=0; j < def.NumStates; j++) { /* to state */
var aij = def.TransP.entries[(i*def.NumStates)+j];
if (aij > 0)
states[j].pred.push([states[i], tolog(aij)]);
}
}
return { name: name, states: states };
};
var models = extract_models(hmmdef, mkmodel, process_codebook);
console.assert(models.length);
var make_maxp = function(input) {
var phi = function(phi, state, t) {
var j;
if (state.start) {
return (t===0) ? 0 : Infinity; /* base case */
}
if (t===0) return Infinity;
// compute probability of emitting signal o_t in this state
var o_t = input[t-1];
var b_j = 0;
for (j = 0; j<o_t.length; j++) {
/* XXX ignoring stream weights here */
b_j += state.output[j][o_t[j]];
}
// maximized prob of reaching this state
// (log probs are negated, so max prob == Math.min)
console.assert(state.pred.length);
var bestp = phi(phi, state.pred[0][0], t-1) + state.pred[0][1];
for (j = 1; j < state.pred.length; j++) {
var p = phi(phi, state.pred[j][0], t-1) + state.pred[j][1];
if (p < bestp) { bestp = p; }
}
return bestp + b_j;
};
var phiN = function(phi, pred_state, aiN) {
/*
console.log('-- phi_N('+input.length+')',
phi(phi, pred_state, input.length),
'+', aiN);
*/
return phi(phi, pred_state, input.length) + aiN;
};
var maxp = function(model) {
//console.log("Considering "+model.name);
// need to memoize the computation of phi
var memo_table = model.states.map(function(){ return [] });
var memoized_phi = function(_, state, t) {
if (!(t in memo_table[state.id])) {
memo_table[state.id][t] = phi(memoized_phi, state, t);
/*
console.log('phi_'+state.id+'('+t+')',
memo_table[state.id][t]);
*/
}
return memo_table[state.id][t];
};
// our log probs are negated, so max prob == min phi
// (negate result so that 'maxp' makes sense to caller)
var pred = model.states[model.states.length-1].pred;
console.assert(pred.length > 0);
var bestp = phiN(memoized_phi, pred[0][0], pred[0][1]);
for (var j=1; j<pred.length; j++) {
var p = phiN(memoized_phi, pred[j][0], pred[j][1]);
if (p < bestp) bestp = p;
}
return -bestp;
};
return maxp;
};
return function(data_set) {
vq_features(data_set);
if (false) return ["A1", 0]; // DEBUGGING: time VQ in isolation
var maxp = make_maxp(data_set.vq);
var best=0, bestp = maxp(models[0]), p;
for (var i=1; i<models.length; i++) {
p = maxp(models[i]);
if (p > bestp) {
best = i;
bestp = p;
}
}
return [models[best].name, bestp];
}
}
var make_recog = function(hmmdef) {
// XXX handle other types of HMM
return make_discrete_recog(hmmdef);
};
return {
// utility functions
tolog: tolog,
fromlog: fromlog,
// main recognizer
make_recog: make_recog
};
});