Week2 Emojify_v3a key error

Hello, and thank you for your time to answer. Currently working on Emojify. In " Exercise 1 - sentence_to_avg" at "

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

avg = np.zeros(word_to_vec_map['of'].shape)" part I cant initilize the avg to a certain shape. I tryed hardcoding but it is not working as said in description. I am trying to write as it is described, trying to get one of the "word_to_vec_map" keys and use it's shape. However even though I can output it, I cant use it in avg

===
for key, value in word_to_vec_map.items():
continue
print (key, value)
print(word_to_vec_map[‘of’])
print(word_to_vec_map[‘of’].shape)
print("===")
# Initialize the average word vector, should have the same shape as your word vectors.
avg = np.zeros(word_to_vec_map[‘of’].shape)

Output:
===
[‘morrocan’, ‘couscous’, ‘is’, ‘my’, ‘favorite’, ‘dish’]

[ 0.70853 0.57088 -0.4716 0.18048 0.54449 0.72603
0.18157 -0.52393 0.10381 -0.17566 0.078852 -0.36216
-0.11829 -0.83336 0.11917 -0.16605 0.061555 -0.012719
-0.56623 0.013616 0.22851 -0.14396 -0.067549 -0.38157
-0.23698 -1.7037 -0.86692 -0.26704 -0.2589 0.1767
3.8676 -0.1613 -0.13273 -0.68881 0.18444 0.0052464
-0.33874 -0.078956 0.24185 0.36576 -0.34727 0.28483
0.075693 -0.062178 -0.38988 0.22902 -0.21617 -0.22562
-0.093918 -0.80375 ]
(50,)

[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.]
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’]

After that it gives key error at the avg part. I am stuck and burned out :smiley:

Replace [‘of’] with [any_word].

The “any_word” variable is defined specifically for this purpose by some code earlier in the notebook:

# Get a valid word contained in the word_to_vec_map. 
any_word = list(word_to_vec_map.keys())[0]

But any_word just prints out “the” in the descriptions didnt they said dont just use “the”

I tryed and now I am getting " Check that you are finding the 4 words" error

The reason your method using [‘of’] doesn’t work is because ‘of’ isn’t in the word_to_vec_map that the test case uses. It is only in the exercise example data set.

That’s why you get the “KeyError”

And that’s why the notebook gives you the “any_word” variable to use.

The instructions told you to not hard-code any words from the dictionary - so don’t use ‘the’, use the any_word variable, because that’s always going to be the first key in the dictionary.

That’s a different error. So there is another issue in your code.

I understand thank you, I am still getting “Check that you are finding the 4 words” error but idk if it is related to my first question

The “4 words” error means that either you’re not finding the correct words, or you’re not calculating the average correctly.

It’s a different problem entirely.

I tryed some experiemnts and found out I implemented a part incorrectly, it seem I have an other error, I will try to fix it, thank you for your time, :slight_smile:

2 Likes

Doesn’t work for me, what do i do?

What exactly doesn’t work for you? Please be specific.

TypeError Traceback (most recent call last)
in
1 # BEGIN UNIT TEST
----> 2 avg = sentence_to_avg(“Morrocan couscous is my favorite dish”, word_to_vec_map)
3 print(“avg = \n”, avg)
4
5 def sentence_to_avg_test(target):

in sentence_to_avg(sentence, word_to_vec_map)
31 # Check that word exists in word_to_vec_map
32 if w in words:
—> 33 avg += word_to_vec_map(w)
34
35 #avg += word_to_vec_map.get(w)

TypeError: ‘dict’ object is not callable

Hi, I’m getting this error and don’t know how to solve. Please help

“word_to_vec_map” is a python dictionary. So you have to access it using square brackets, not parenthesis.

hey i’m unable to fix this error.

avg = np.zeros(word_to_vec_map[any_word].shape)
avg += word_to_vec_map[w]

what am i doing wrong?

hey i’m unable to fix this error.

avg = np.zeros(word_to_vec_map[any_word].shape)
avg += word_to_vec_map[w]

what am i doing wrong?

1 Like

What error are you seeing? You’re just posting some code fragments.

the 4 words error only.
avg = np.zeros(word_to_vec_map[any_word].shape)
avg += word_to_vec_map[w]

i have been stuck on this for a while

Why do you feel there is a problem with those lines of code?