I am trying to build a text classifier using lstm which, in its first layer, has weights get by a Word2Vecmodel.

In order to build a matrix containing the indexes of each word for each sentence, I have tried: (as mentioned here)

X_tr_word2vec = np.array(X_tr_word2vec)
y_tr_word2vec = np.array(y_tr_word2vec)

train_x = np.zeros([X_tr_word2vec.shape[0], max_sentence_length], dtype=np.int32)
train_y = np.zeros([y_tr_word2vec.shape[0]], dtype=np.int32)

for i, sentence in enumerate(X_tr_word2vec):
    for j, word in enumerate(sentence[:-1]):
        train_x[i,j] = model_word2vec.wv.vocab[word].index

but, when I run the code, I get this error:KeyError: 'enquiringly', what does it mean? I suppose that it is about a wrong train_xdimension.


I have trained Word2Vec model before, with the entire training set:

model_word2vec = models.Word2Vec(X_tr_word2vec, size=150, window=9)

That means your word 'enquiringly' is not in your word embedding vocabulary vocab.

For out-of-vocabulary(OOV) words, there is usually a embedding vector dedicated to them. Try to find that special symbol in the vocab and use that corresponding embedding vector.

|improve this answer|||||
  • $\begingroup$ I have trained the model before, with the entire training set. Therefore how is it possible that the model doesn't have some words in its vocabulary? $\endgroup$ – Simone Sep 12 '18 at 15:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.