Embedding returns an error that I dont understand
<SCODE>a = torch.Tensor(10)
a = Variable(a, requires_grad=True)
embedding = nn.Embedding(10, 100)
output = embedding(a)
<ECODE> <SCODE>---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-23-11b80614bb73> in <module>()
      2 a = Variable(a, requires_grad=True)
      3 embedding = nn.Embedding(10, 100)
----> 4 output = embedding(a)

/home/zeng/code/tensorfold/lib/python2.7/site-packages/torch/nn/modules/module.pyc in __call__(self, *input, **kwargs)
    200 
    201     def __call__(self, *input, **kwargs):
--> 202         result = self.forward(*input, **kwargs)
    203         for hook in self._forward_hooks.values():
    204             hook_result = hook(self, input, result)

/home/zeng/code/tensorfold/lib/python2.7/site-packages/torch/nn/modules/sparse.pyc in forward(self, input)
     92             padding_idx, self.max_norm, self.norm_type,
     93             self.scale_grad_by_freq, self.sparse
---> 94         )(input, self.weight)
     95 
     96     def __repr__(self):

/home/zeng/code/tensorfold/lib/python2.7/site-packages/torch/nn/_functions/thnn/sparse.pyc in forward(self, indices, weight)
     42     def forward(self, indices, weight):
     43         assert indices.dim() <= 2
---> 44         assert not self.needs_input_grad[0], "Embedding doesn't " \
     45             "compute the gradient w.r.t. the indices"
     46 

AssertionError: Embedding doesn't compute the gradient w.r.t. the indices
<ECODE>
zeng
Hi,
albanD
Then, how to get some of the embeddings updated and some fixed?
cdjhz
If you want to achieve that, I think you will need to set the gradients corresponding to these embeddings to 0 manually after performing the backward pass.
albanD
Janinanu
Hi,
albanD
Thank you very much! After some adjustments, my code now works.
Janinanu
SHAILESH_KUMAR