Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
The Forward Algorithm
Many paths give rise to the same sequence X
We would often like to know the total
probability of some sequence:
P(x) =
π(π₯, π)
π
The problem is that the number of possible paths increases
exponentially with the length of the sequence
# of possible paths = (number of underlying states)L
Problems that grow exponentially with size can only be
solved by brute force for remarkably small problem sizes
We need a better way!!
The Forward Algorithm
Defining the forward variable
βThe forward variable for state k at position iβ
fk(i) = P(x1β¦xi , pi = k)
βthe probability of the sequence from the beginning to the
symbol at position i, requiring that the path at position i is kβ
How does this help?
The Forward Algorithm
What if we had in our possession all of the forward
variables for L, the last position of the sequence?
P(X) =
ππ(π³) ππ β πππ
π
That would be pretty awesome, since to get the overall
probability of the sequence, we would need only sum the
forward variables for each state (after correcting for the
probability of the final transition to the end state)
Gee, thatβs great. But I donβt yet have those βfinal positionβ
forward variablesβ in my possession, do I?
The Forward Algorithm
A recursive definition for forward variables
fk(i) = P(x1β¦xi , pi = k)
We can recursively define forward variables in terms of
their own values at prior positions in the sequenceβ¦
fl(i+1) = el(xi+1)
ππ(π) ππ β π
π
Weβll examine in more detail later but the termination
condition is satisfied by virtue of the fact that sequences are
finite and so we must eventually come to the beginning..
The Forward Algorithm
Putting it all together
Initialization:
β’ fstart(0) = 1
β’ fk(0) = 0 for all initial states k
Recursion (i = 1 β¦ L):
fl(i) = el(xi)
Termination:
ππ(π β π) ππ β π
π
P(X) =
ππ(π³) ππ β πππ
π
How to express these equations in Python?
The Forward Algorithm
Reduction to Python code
The forward variables are conveniently kept in a table,
with indices that are state in one dimension and sequence
positions in another:
forward = [{}]
# a list of dicts
Hereβs how one of the initialization conditions might be set:
forward[0]["S"] = log_float(1)
Later on you might have something like:
forward[position][new_state_l] = \
sum_over_states * emission_prob
Try and implement this, weβll look carefully at how it works next time