Question: I want to load a pre-trained word2vec embedding with gensim into a PyTorch embedding layer.

How do I get the embedding weights loaded by gensim into the PyTorch embedding layer?

Answer: 
I just wanted to report my findings about loading a gensim embedding with PyTorch.

---

* Solution for PyTorch `0.4.0` and newer:
---------------------------------------

From `v0.4.0` there is a new function [`from_pretrained()`](https://pytorch.org/docs/stable/nn.html#torch.nn.Embedding.from_pretrained) which makes loading an embedding very comfortable.
Here is an example from the documentation.

```
import torch
import torch.nn as nn

# FloatTensor containing pretrained weights
weight = torch.FloatTensor([[1, 2.3, 3], [4, 5.1, 6.3]])
embedding = nn.Embedding.from_pretrained(weight)
# Get embeddings for index 1
input = torch.LongTensor([1])
embedding(input)

```

The weights from [*gensim*](https://radimrehurek.com/gensim/) can easily be obtained by:

```
import gensim
model = gensim.models.KeyedVectors.load_word2vec_format('path/to/file')
weights = torch.FloatTensor(model.vectors) # formerly syn0, which is soon deprecated

```

As noted by @Guglie: in newer gensim versions the weights can be obtained by [`model.wv`](https://radimrehurek.com/gensim/models/word2vec.html):

```
weights = model.wv

```

---

* Solution for PyTorch version `0.3.1` and older:
-----------------------------------------------

I'm using version `0.3.1` and [`from_pretrained()`](https://pytorch.org/docs/stable/nn.html#torch.nn.Embedding.from_pretrained) isn't available in this version.

Therefore I created my own `from_pretrained` so I can also use it with `0.3.1`.

*Code for `from_pretrained` for PyTorch versions `0.3.1` or lower:*

```
def from_pretrained(embeddings, freeze=True):
    assert embeddings.dim() == 2, \
         'Embeddings parameter is expected to be 2-dimensional'
    rows, cols = embeddings.shape
    embedding = torch.nn.Embedding(num_embeddings=rows, embedding_dim=cols)
    embedding.weight = torch.nn.Parameter(embeddings)
    embedding.weight.requires_grad = not freeze
    return embedding

```

The embedding can be loaded then just like this:

```
embedding = from_pretrained(weights)

```

I hope this is helpful for someone.
Had similar problem: "after training and saving embeddings in **binary** format using gensim, how I load them to torchtext?"

I just saved the file to txt format and then follow the superb [tutorial](https://github.com/bentrevett/pytorch-sentiment-analysis/blob/master/C%20-%20Loading%2C%20Saving%20and%20Freezing%20Embeddings.ipynb) of loading custom word embeddings.

```
def convert_bin_emb_txt(out_path,emb_file):
    txt_name = basename(emb_file).split(".")[0] +".txt"
    emb_txt_file = os.path.join(out_path,txt_name)
    emb_model = KeyedVectors.load_word2vec_format(emb_file,binary=True)
    emb_model.save_word2vec_format(emb_txt_file,binary=False)
    return emb_txt_file

emb_txt_file = convert_bin_emb_txt(out_path,emb_bin_file)
custom_embeddings = vocab.Vectors(name=emb_txt_file,
                                  cache='custom_embeddings',
                                  unk_init=torch.Tensor.normal_)

TEXT.build_vocab(train_data,
                 max_size=MAX_VOCAB_SIZE,
                 vectors=custom_embeddings,
                 unk_init=torch.Tensor.normal_)

```

tested for: PyTorch: 1.2.0 and TorchText: 0.4.0.

I added this answer because with the accepted answer I was not sure how to follow the linked [tutorial](https://github.com/bentrevett/pytorch-sentiment-analysis/blob/master/C%20-%20Loading%2C%20Saving%20and%20Freezing%20Embeddings.ipynb) and initialize all words not in the embeddings using the normal distribution and how to make the vectors and equal to zero.