Skip to content

Instantly share code, notes, and snippets.

@dhruvdcoder
Created April 12, 2019 21:17
Show Gist options
  • Save dhruvdcoder/cbb8d7967a499ba85418c18414e2cdce to your computer and use it in GitHub Desktop.
Save dhruvdcoder/cbb8d7967a499ba85418c18414e2cdce to your computer and use it in GitHub Desktop.
Load glove embeddings into pytorch
# convert glove to word2vec format
from gensim.scripts.glove2word2vec import glove2word2vec
glove2word2vec(glove_input_file="emb.txt", word2vec_output_file="emb_word2vec_format.txt")
import gensim
import torch
model = gensim.models.KeyedVectors.load_word2vec_format('emb_word2vec_format.txt')
weights = torch.FloatTensor(model.vectors)
# Ref: https://stackoverflow.com/questions/37793118/load-pretrained-glove-vectors-in-python
# https://stackoverflow.com/questions/49710537/pytorch-gensim-how-to-load-pre-trained-word-embeddings
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment