'Embedding Matrix for GloVe Dictionary

I am new to Sentiment Analysis using LSTM and GloVe Embeddings.

During my code, I got an error says that:

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
C:\Users\JAMES~1\AppData\Local\Temp/ipykernel_16128/3567447988.py in <module>
      7   embedding_vector = word_to_vec_map.get(word)
      8   if not embedding_vector is None:
----> 9     emb_matrix[index, :] = embedding_vector

IndexError: index 2684 is out of bounds for axis 0 with size 2684

I try search up for solution but still this error occur. This is my code:

vocab_len = len(words_to_index)
embed_vector_len = word_to_vec_map['speed'].shape[0]

emb_matrix = np.zeros((vocab_len, embed_vector_len))

for word, index in words_to_index.items():
  embedding_vector = word_to_vec_map.get(word)
  if not embedding_vector is None:
    emb_matrix[index, :] = embedding_vector

Hope someone can help me. Thank you.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source