0

I have a function def viterbi(). One of the input variabless is sequence. Later on in the function I am calling sequence. However, by doing so, it returns:

NameError: name 'sequence' is not defined

Here is my function:

def viterbi(sequence, p_start=None, p_trans=None, p_stop=None, p_emiss=None):


length = len(sequence)
num_states = len(p_start)
best_path = []

# trellis to store Viterbi scores
trellis = np.full([length, num_states], -np.inf)

# backpointers to backtrack
backpointers = -np.ones([length, num_states], dtype=int)

trellis[0] = p_start + p_emiss[obs2i[sequence[0].obs]] # log(viterbi(1,ck))

# Fill the remaining columns with transition probabilities
for i in range(1, length):
  matrix_product = p_trans + trellis[i - 1]
  backpointers[i] = [max_index(el) for el in matrix_product]
  max_over_c_l = [max(el) for el in matrix_product]
  trellis[i] = max_over_c_l + p_emiss[obs2i[sequence[i].obs]]

# Compute the maximum probability with end probabilities
ending_probabilities = p_stop + trellis[-1]
final_backpointer = max_index(ending_probabilities)
best_score = max(ending_probabilities)

# Compute the best path by backtracking
for seq in range(0,length):
    best_path.append(backpointers[seq][max_index(trellis[seq])])
best_path.append(final_backpointer)

return (best_score, best_path)

I have looked at similar questions, but none seem to deal with the same problem as I do:

Here, there is no input variable in the function.

Here, a variable that is created in the function is called outside of the function.

Here, the variable is simply not defined.

What confuses me is that the variable sequence is an input to the function. Therefore, I would expect that the function knows what sequence is. Also, when I initialize the variable length within the function, it does not return an error.

Who can tell me what is going on here? And what can I do to overcome this problem?

Emil
  • 1,531
  • 3
  • 22
  • 47

0 Answers0