In [1]:
import torch
import torch.nn as nn
from torch.autograd import Variable

In [10]:
# One Hot Vector (max val = N) to M-dim vector
N = 1000
M = 3
embedding = nn.Embedding(N, M)
input = Variable(torch.LongTensor([[1, 2, 4, 5, 1],[4, 3, 0, 9, 999]]))
output = embedding(input)
output.size(), output.data, output.data.max(), output.data.mean(), output.data.min()


Out[10]:
(torch.Size([2, 5, 3]), 
 (0 ,.,.) = 
   0.3966  1.1329  0.9703
   1.2760 -0.3156 -0.3003
   0.4951 -2.4289 -1.8240
  -1.2025 -0.5043 -0.1327
   0.3966  1.1329  0.9703
 
 (1 ,.,.) = 
   0.4951 -2.4289 -1.8240
  -0.3016 -1.0829  1.2743
  -0.0533 -0.9346 -0.3806
  -1.8540 -1.1871 -0.9740
   0.5305 -0.0377 -0.4296
 [torch.FloatTensor of size 2x5x3], 1.275978684425354, -0.304186017687122, -2.4288904666900635)

In [13]:
M_new = 9
embedding_new = nn.Embedding(N, M_new)
output = embedding_new(input)
output.size(), output.data, output.data.max(), output.data.mean(), output.data.min()


Out[13]:
(torch.Size([2, 5, 9]), 
 (0 ,.,.) = 
   1.2783 -2.0387  0.3489  2.5870  0.4003 -0.8323  0.4795 -0.1188  1.5058
   1.9345  0.6939 -0.6005  0.0113 -1.3579 -1.5732  0.6476  0.3265  2.0663
  -1.7766  1.7161  1.5391 -0.2763 -1.0611 -1.7473  1.4869  0.1519  0.5832
   0.2865  1.8251 -0.3772  0.1366 -0.1392 -1.4507 -0.0412  0.3509 -1.0199
   1.2783 -2.0387  0.3489  2.5870  0.4003 -0.8323  0.4795 -0.1188  1.5058
 
 (1 ,.,.) = 
  -1.7766  1.7161  1.5391 -0.2763 -1.0611 -1.7473  1.4869  0.1519  0.5832
  -0.1941 -0.7290 -1.2425  2.0444 -0.2431 -0.7992 -1.1772  0.0754  0.0805
  -1.7917  0.3172 -0.4379 -0.0060  0.6569  2.5088 -0.9982 -1.5249  0.3790
  -0.9209 -0.2454  1.3805 -2.9321 -0.3620  1.3712  1.5372 -0.0832 -0.0649
  -0.7073 -0.0540  0.0487  1.0955 -0.9316  0.5288  0.2074  0.9193  0.6603
 [torch.FloatTensor of size 2x5x9], 2.587045907974243, 0.09486205115293463, -2.9320576190948486)

In [11]:
N_invalid = 999  # the last val in input is illegal
embedding_invalid = nn.Embedding(N_invalid, M)
embedding_invalid(input)


---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-11-52b35ee6c52a> in <module>()
      1 N_invalid = 999  # the last val in input is illegal
      2 embedding_invalid = nn.Embedding(N_invalid, M)
----> 3 embedding_invalid(input)

~/miniconda3/envs/ml/lib/python3.6/site-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
    323         for hook in self._forward_pre_hooks.values():
    324             hook(self, input)
--> 325         result = self.forward(*input, **kwargs)
    326         for hook in self._forward_hooks.values():
    327             hook_result = hook(self, input, result)

~/miniconda3/envs/ml/lib/python3.6/site-packages/torch/nn/modules/sparse.py in forward(self, input)
    101             input, self.weight,
    102             padding_idx, self.max_norm, self.norm_type,
--> 103             self.scale_grad_by_freq, self.sparse
    104         )
    105 

~/miniconda3/envs/ml/lib/python3.6/site-packages/torch/nn/_functions/thnn/sparse.py in forward(cls, ctx, indices, weight, padding_idx, max_norm, norm_type, scale_grad_by_freq, sparse)
     57             output = torch.index_select(weight, 0, indices)
     58         else:
---> 59             output = torch.index_select(weight, 0, indices.view(-1))
     60             output = output.view(indices.size(0), indices.size(1), weight.size(1))
     61 

RuntimeError: index out of range at /Users/soumith/minicondabuild3/conda-bld/pytorch_1512381214802/work/torch/lib/TH/generic/THTensorMath.c:277

In [ ]: