Qu

forward

Builtin index · Statistics & ML

result = hmm.forward(obs)

Input: obs · Output: result

What it is

Runs the forward algorithm on an hmm model hmm for a length-T integer observation-symbol sequence obs (each entry a 0-based column index into emission).

Overloads

One call shape — this function is not overloaded.

CodeInputOutputDescription
result = hmm.forward(obs)
obs numbernot documented yet
result number
Returns a single number: the total probability of that observation sequence, summed over every possible hidden-state path.

Examples

From the documentation, and run by the build -- the output and the figure below are what the engine actually produced. Simplest first.

P = [0.9, 0.5, 0.1, 0.5] as matrix(2, 2)   # column-major: rows [0.9 0.1], [0.5 0.5]

chain = markov_chain(P, 0)
print("stationary: {round(chain.stationary(), 4)}")
print("3-step:     {round(chain.transition ^ 3, 4)}")

trans = [0.7, 0.4, 0.3, 0.6] as matrix(2, 2)
emit  = [0.5, 0.1, 0.4, 0.3, 0.1, 0.6] as matrix(2, 3)
h = hmm(trans, emit, [0.6, 0.4])
print("P(normal, cold, dizzy) = {round(h.forward([0, 1, 2]), 5)}")
print("most likely path       = {h.viterbi([0, 1, 2])}")
output
stationary: [0.8333, 0.1667]
3-step:     [0.844, 0.156; 0.78, 0.22]
P(normal, cold, dizzy) = 0.03628
most likely path       = [0, 0, 1]

from statistics-ml

How it works

forward(net, x) — callable as net.forward(x). Runs x through every layer in order, TRACKED on the tape (each layer's stored weights are freshly wrapped via param() on every call, exactly the manual W1 = param(W1_init) idiom benchmarks/mlp/bench.qu and the GRU/CNN acceptance tests already use by hand) — the return value is a Value::Tensor suitable for grad(loss, wrt=...). net.predict(x) is the untracked, dropout-off counterpart for inference. Also callable as hmm_model.forward(obs) (§ Hidden Markov Model, 2026-09-01): the forward algorithm's total probability of an observation sequence — an unrelated computation on an unrelated model kind that happens to share the same verb, so it's dispatched right here rather than adding a second "forward" match arm (which rustc rejects as unreachable — a match only ever takes its first matching arm). See hmm_forward's own doc comment for the algorithm.

From the note above this builtin in the interpreter, where the reasoning was written down as it was built.