# Why my UNQ\_C7 incorrect while I got same expected output?

**URL:** <https://community.deeplearning.ai/t/why-my-unq-c7-incorrect-while-i-got-same-expected-output/59987>\
**Category:** NLP with Probabilistic Models\
**Tags:** week-module-2\
**Created:** [November 5, 2021, 1:22am UTC](https://community.deeplearning.ai/t/why-my-unq-c7-incorrect-while-i-got-same-expected-output/59987 "2021-11-05T01:22:39Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hongbing\_Qin](https://avatars.discourse-cdn.com/v4/letter/h/a183cd/32.png) [@Hongbing\_Qin](https://community.deeplearning.ai/u/Hongbing_Qin)\
**Post date:** [November 5, 2021, 1:22am UTC](https://community.deeplearning.ai/t/why-my-unq-c7-incorrect-while-i-got-same-expected-output/59987/1 "2021-11-05T01:22:40Z")

</div>

I got the same output in UNQ\_C7 as expected output but grader out put says mine incorrect.

# UNQ\_C7 (UNIQUE CELL IDENTIFIER, DO NOT EDIT)

# GRADED FUNCTION: viterbi\_backward

def viterbi\_backward(best\_probs, best\_paths, corpus, states):  
‘’’  
This function returns the best path.

```
'''
# Get the number of words in the corpus
# which is also the number of columns in best_probs, best_paths
m = best_paths.shape[1] 

# Initialize array z, same length as the corpus
z = [None] * m

# Get the number of unique POS tags
num_tags = best_probs.shape[0]

# Initialize the best probability for the last word
best_prob_for_last_word = float('-inf')

# Initialize pred array, same length as corpus
pred = [None] * m

### START CODE HERE (Replace instances of 'None' with your code) ###
## Step 1 ##

# Go through each POS tag for the last word (last column of best_probs)
# in order to find the row (POS tag integer ID) 
# with highest probability for the last word
for k in range(num_tags): # complete this line

    # If the probability of POS tag at row k 
    # is better than the previously best probability for the last word:
    if best_probs[k, m-1] >= best_prob_for_last_word: # complete this line
        
        # Store the new best probability for the last word
        best_prob_for_last_word = best_probs[k, m-1]

        # Store the unique integer ID of the POS tag
        # which is also the row number in best_probs
        z[m - 1] = k
        
# Convert the last word's predicted POS tag
# from its unique integer ID into the string representation
# using the 'states' list
# store this in the 'pred' array for the last word
pred[m - 1] = states[z[m - 1]]

## Step 2 ##
# Find the best POS tags by walking backward through the best_paths
# From the last word in the corpus to the 0th word in the corpus
for i in range(m-1, 0, -1): # complete this line
    
    # Retrieve the unique integer ID of
    # the POS tag for the word at position 'i' in the corpus
    pos_tag_for_word_i = best_paths[z[i], i]
    
    # In best_paths, go to the row representing the POS tag of word i
    # and the column representing the word's position in the corpus
    # to retrieve the predicted POS for the word at position i-1 in the corpus
    z[i - 1] = best_paths[pos_tag_for_word_i, i]
    
    # Get the previous word's POS tag in string form
    # Use the 'states' list, 
    # where the key is the unique integer ID of the POS tag,
    # and the value is the string representation of that POS tag
    pred[i - 1] = states[z[i-1]]
    
 ### END CODE HERE ###

```

 ![Capture22_1](https://global.discourse-cdn.com/dlai/original/2X/0/0b3a71a59800ada10a6b90191da5832906af49e4.jpeg)  
 ![Capture22_2](https://global.discourse-cdn.com/dlai/original/2X/7/7293e98fcfce3b9601832b7aa554ee5b09953f62.jpeg)

---

<div class="post-metadata">

**Author:** ![sylviasc](https://avatars.discourse-cdn.com/v4/letter/s/c89c15/32.png) [@sylviasc](https://community.deeplearning.ai/u/sylviasc)\
**Post date:** [November 6, 2021, 5:10pm UTC](https://community.deeplearning.ai/t/why-my-unq-c7-incorrect-while-i-got-same-expected-output/59987/2 "2021-11-06T17:10:33Z")

</div>

> [@Hongbing\_Qin](#):
>
> `best_probs[k, m-1] >= best_prob_for_last_word`

here it says better than, have you tried to change it to ‘\>’?

---

<div class="post-metadata">

**Author:** ![sylviasc](https://avatars.discourse-cdn.com/v4/letter/s/c89c15/32.png) [@sylviasc](https://community.deeplearning.ai/u/sylviasc)\
**Post date:** [November 6, 2021, 6:47pm UTC](https://community.deeplearning.ai/t/why-my-unq-c7-incorrect-while-i-got-same-expected-output/59987/4 "2021-11-06T18:47:36Z")

</div>

You might need to restart and run all the cells again before submission.

---

<div class="post-metadata">

**Author:** ![Hongbing\_Qin](https://avatars.discourse-cdn.com/v4/letter/h/a183cd/32.png) [@Hongbing\_Qin](https://community.deeplearning.ai/u/Hongbing_Qin)\
**Post date:** [November 8, 2021, 7:29pm UTC](https://community.deeplearning.ai/t/why-my-unq-c7-incorrect-while-i-got-same-expected-output/59987/5 "2021-11-08T19:29:56Z")

</div>

I did “restart and rerun” every time. It doesn’t help at all.

---

<div class="post-metadata">

**Author:** ![wiereen](https://avatars.discourse-cdn.com/v4/letter/w/e79b87/32.png) [@wiereen](https://community.deeplearning.ai/u/wiereen)\
**Post date:** [February 19, 2022, 5:40pm UTC](https://community.deeplearning.ai/t/why-my-unq-c7-incorrect-while-i-got-same-expected-output/59987/6 "2022-02-19T17:40:48Z")

</div>

> [@Hongbing\_Qin](#):
>
> `pos_tag_for_word_i = best_paths[z[i], i]`

For future reference, this line of code is incorrect. I got the same problem when the variable was defined like this. It took a while to see that I should not take the index-value of the previous column as is being done with this code.

---

<div class="post-metadata">

**Author:** ![Laks\_P](https://avatars.discourse-cdn.com/v4/letter/l/ecd19e/32.png) [@Laks\_P](https://community.deeplearning.ai/u/Laks_P)\
**Post date:** [July 22, 2022, 6:42pm UTC](https://community.deeplearning.ai/t/why-my-unq-c7-incorrect-while-i-got-same-expected-output/59987/7 "2022-07-22T18:42:01Z")

</div>

I have a similar problem; did somebody figure out why the code is not working? any suggestion?
