Week 2 emojify assignment

Hi!

I am getting this error, but makes no sense to me. To be clear, ‘love’ is clearly a word in dict word_to_vec_map, and why is that a bad call?

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]
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-14-cd40edd34d98> in <module>
     25     print("\033[92mAll tests passed!")
     26 
---> 27 sentence_to_avg_test(sentence_to_avg)
     28 
     29 # END UNIT TEST

<ipython-input-14-cd40edd34d98> in sentence_to_avg_test(target)
     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)"
     20     avg = target("love", word_to_vec_map)

<ipython-input-13-3e16967faaa0> in sentence_to_avg(sentence, word_to_vec_map)
     35         # Check that word exists in word_to_vec_map
     36         if w in words:
---> 37             avg += word_to_vec_map[w] #word_to_vec_map.get(w) ERRROR TypeError: ufunc 'add' output (typecode 'O') could not be coerced to provided output parameter (typecode 'd') according to the casting rule ''same_kind''
     38             # Increment count
     39             count +=1

KeyError: 'love'

also, there is a little mistake in previous cell (i am only reporting this, didnt matter in my question):

idx = 50
print(f"Sentence '{X_train[HERE THE NOTEBOOK SAYS 50, SHOULD SAY 'idx']}' has label index {Y_train[idx]}, which is emoji {label_to_emoji(Y_train[idx])}", )
print(f"Label index {Y_train[idx]} in one-hot encoding format is {Y_oh_train[idx]}")

I think you are confusing the words in the sentence and the words in the dictionary.

Your code in line 36 “if w in words” is incorrect.
You already know (from the previous for-loop) that w is in words. What you’re supposed to check here is whether w is in the word_to_vec_map dictionary.

If it’s in the dictionary, then you include it in the average.

1 Like