C5 W2 A2 - Error in sentence_to_avg

I am getting an error in the function sentence_to_avg. Please help me in here

avg =
[-0.008005 0.56370833 -0.50427333 0.258865 0.55131103 0.03104983
-0.21013718 0.16893933 -0.09590267 0.141784 -0.15708967 0.18525867
0.6495785 0.38371117 0.21102167 0.11301667 0.02613967 0.26037767
0.05820667 -0.01578167 -0.12078833 -0.02471267 0.4128455 0.5152061
0.38756167 -0.898661 -0.535145 0.33501167 0.68806933 -0.2156265
1.797155 0.10476933 -0.36775333 0.750785 0.10282583 0.348925
-0.27262833 0.66768 -0.10706167 -0.283635 0.59580117 0.28747333
-0.3366635 0.23393817 0.34349183 0.178405 0.1166155 -0.076433
0.1445417 0.09808667]

AssertionError Traceback (most recent call last)
in
25 print("\033[92mAll tests passed!")
26
—> 27 sentence_to_avg_test(sentence_to_avg)
28
29 # END UNIT TEST

in sentence_to_avg_test(target)
17 assert np.allclose(avg, [1.25, 2.5]), “Check that you are finding the 4 words”
18 avg = target(“love a a_nw c_w a_s”, word_to_vec_map)
—> 19 assert np.allclose(avg, [1.25, 2.5]), “Divide by count, not len(words)”
20 avg = target(“love”, word_to_vec_map)
21 assert np.allclose(avg, [0, 0]), “Average of no words must give an array of zeros”

AssertionError: Divide by count, not len(words)

Perhaps there is an error in your code for computing the average value when count > 0.

But that is not the only place there could be an error.

Thank you @TMosh for you reply
I am still facing the error
avg =
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
0. 0.]

AssertionError Traceback (most recent call last)
in
25 print("\033[92mAll tests passed!")
26
—> 27 sentence_to_avg_test(sentence_to_avg)
28
29 # END UNIT TEST

in sentence_to_avg_test(target)
15 avg = target(“a a_nw c_w a_s”, word_to_vec_map)
16 assert tuple(avg.shape) == tuple(word_to_vec_map[‘a’].shape), “Check the shape of your avg array”
—> 17 assert np.allclose(avg, [1.25, 2.5]), “Check that you are finding the 4 words”
18 avg = target(“love a a_nw c_w a_s”, word_to_vec_map)
19 assert np.allclose(avg, [1.25, 2.5]), “Divide by count, not len(words)”

AssertionError: Check that you are finding the 4 words

I have looped on words in for loop, used word_to_vec_map[w] in if for condition and calculated the average for word_to_vec_map[w]

Hi @ChandiniSaiKumarDukk ,

Print statement is very helpful for debugging. Use print statement to print out what is in words, and check if your code is able to find those words in the word_to_vec_map. Also print the value for avg as each word is found.

Thank you @Kic for your reply
I have included the print statements before the if condition and for avg before and after computing. But still I am unable to find the issue.

morrocan
couscous
is
my
favorite
dish
avg =
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
0. 0.]
a
a_nw
c_w
a_s

AssertionError Traceback (most recent call last)
in
25 print("\033[92mAll tests passed!")
26
—> 27 sentence_to_avg_test(sentence_to_avg)
28
29 # END UNIT TEST

in sentence_to_avg_test(target)
15 avg = target(“a a_nw c_w a_s”, word_to_vec_map)
16 assert tuple(avg.shape) == tuple(word_to_vec_map[‘a’].shape), “Check the shape of your avg array”
—> 17 assert np.allclose(avg, [1.25, 2.5]), “Check that you are finding the 4 words”
18 avg = target(“love a a_nw c_w a_s”, word_to_vec_map)
19 assert np.allclose(avg, [1.25, 2.5]), “Divide by count, not len(words)”

AssertionError: Check that you are finding the 4 words

for w in words:
print(w)
# Check that word exists in word_to_vec_map
if w in word_to_vec_map[w]:
print(avg)
avg += word_to_vec_map[w]
print(avg)
# Increment count
count +=1

if count > 0:
    # Get the average. But only if count > 0
    avg = avg / len(words)

Hi @ChandiniSaiKumarDukk

avg should be divided by count, not len(words)

Also, just to make it fool proof:
avg = avg + word_to_vec_map[w]

Hi @Kic
I am sorry I have changed the code but the same issue persists.

for w in words:
print(w)
# Check that word exists in word_to_vec_map
if w in word_to_vec_map[w]:
print(avg)
avg = avg + word_to_vec_map[w]
print(avg)
# Increment count
count = count + 1
print(count)

if count > 0:
    # Get the average. But only if count > 0
    avg = avg/count

Am I assigning the value avg wrong or wrong identations? I have used this for avg, Please suggest me here
avg = np.zeros(word_to_vec_map[any_word].shape)

Hi @ChandiniSaiKumarDukk ,

ignore my last reply. I have misread the first line.
Try refresh the kernel and rerun you code from start.

Hi @Kic
Sorry, I have tried everything, even tried updating the latest version but nothing worked out. Still the same issue persists nothing has changed
Please help me

The input sentence has been broken up into words successfully. So there is no problem in that.
But what value did you get for avg and count when printed out?

This is when I change the indentation of count = count + 1 outside of if condition
morrocan
1
couscous
2
is
3
my
4
favorite
5
dish
6
avg =
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
0. 0.]
a
1
a_nw
2
c_w
3
a_s
4

However if the increment of count is present inside the if condition nothing is being printed for the count
morrocan
couscous
is
my
favorite
dish
avg =
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
0. 0.]
a
a_nw
c_w
a_s

Hi @ChandiniSaiKumarDukk

It looks like there is a problem in executing the code either in the for loop or the if statement inside the loop.

Again, use print statement to check what is in w, words and word_to_vec_map?

Hi @Kic
Still the same error with no change, I mean even if I include any print statement inside the if statement it is not executing may be this is because the if condition is not getting satisfied so the statements inside the if condition are not executing. That’s what I understood, I may be wrong

{mentor edit: code removed}

If you move those print statements out, then you would be able to see what those values are.
I noticed your code is still using len of words which should be count

{mentor edit: code removed}

morrocan
0
couscous
0
is
0
my
0
favorite
0
dish
0
avg =
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
0. 0.]
a
0
a_nw
0
c_w
0
a_s
0

AssertionError Traceback (most recent call last)
in
25 print("\033[92mAll tests passed!")
26
—> 27 sentence_to_avg_test(sentence_to_avg)
28
29 # END UNIT TEST

in sentence_to_avg_test(target)
15 avg = target(“a a_nw c_w a_s”, word_to_vec_map)
16 assert tuple(avg.shape) == tuple(word_to_vec_map[‘a’].shape), “Check the shape of your avg array”
—> 17 assert np.allclose(avg, [1.25, 2.5]), “Check that you are finding the 4 words”
18 avg = target(“love a a_nw c_w a_s”, word_to_vec_map)
19 assert np.allclose(avg, [1.25, 2.5]), “Divide by count, not len(words)”

AssertionError: Check that you are finding the 4 words

I have a look at the code for you, put it in a DM to me please.

I think the problem is in the line “if word in word_to_vec_map[w]”

The test should not use [w], because it should test the whole word_to_vec_map dictionary.

Hi @TMosh, @Kic
Thank you that worked
Thanks for the support

Pro-tip: If anyone faces this issue even after going through all the related threads and doing everything correctly, write the whole code from scratch for this section yourself. The issue is somehow solved.

Solution:
In the for loop of “for w in words”, each “w” should be checked in the whole list of list(word_to_vec_map.keys()) not just in a single word(any_word). Therefore :

Replace:
for w in words:
if w in any_word:

To:
for w in words:
if w in list(word_to_vec_map.keys()):

May this answer work for you. Thank you