C5 W2 Exercise 1

Good afternoon, I am a bit stuck on W2 Excercise 1, the “sentence_to_avg” function.

I ended up with the following output:

avg =

The error I am getting is:

AssertionError Traceback (most recent call last)
25 print("\033[92mAll tests passed!")
—> 27 sentence_to_avg_test(sentence_to_avg)

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)

AssertionError: Check the shape of your avg array

I have my avg initialization as:

Step 1: Split sentence into list of lower case words (≈ 1 line)

words = sentence.lower()

Initialize the average word vector, should have the same shape as your word vectors.

avg = np.zeros((word_to_vec_map.get(words[0]).shape))

It is the same result using the any_word from the beginning of the cell.

Could anyone point me in the right direction?

Thank you

You should really avoid using the .get method in this exercise. For some reason, the grader doesn’t like it.

Don’t use “words[0]”. Use ‘any_word’, because you want to use a word from the dictionary, not a word from the sentence.

So “word_to_vec_map[any_word].shape” is preferred.

1 Like