Error on forward pass in LSTM module

I’m trying to familiarize myself with a basic LSTM in PyTorch, and I’m getting a strange error. The bottom of the traceback refers to a _copyParams function in the torch/backends/cudnn/rnn.py file. Basically, the statement asserts that the from/to tensors of the parameters being copied fails, suggesting there’s a tensor type mismatch. I added a quick print statement in the file for debugging, and it says that the function is trying to copy a FloatTensor to a DoubleTensor. I’m sure this is an error in my code and not a bug in the codebase, which is why I’m posting it here. You can find my code in this notebook.

> torch.cuda.FloatTensor torch.cuda.DoubleTensor
> ---------------------------------------------------------------------------
> AssertionError                            Traceback (most recent call last)
> <ipython-input-7-bccbf62ef6f2> in <module>()
>      14 
>      15         # forward + backward + optimize
> ---> 16         outputs = tester(inputs)
>      17         loss = loss_function(outputs, labels)
>      18         loss.backward()

> ~/anaconda3/envs/torch/lib/python3.6/site-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
>     204 
>     205     def __call__(self, *input, **kwargs):
> --> 206         result = self.forward(*input, **kwargs)
>     207         for hook in self._forward_hooks.values():
>     208             hook_result = hook(self, input, result)

> <ipython-input-4-fbaa41914871> in forward(self, x)
>      15 
>      16     def forward(self, x):
> ---> 17         output, self.hidden = self.lstm(x,self.hidden)
>      18         output = self.fc(output.view(x.size()[1],-1))
>      19         return torch.clamp(output,0,49688)

> ~/anaconda3/envs/torch/lib/python3.6/site-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
>     204 
>     205     def __call__(self, *input, **kwargs):
> --> 206         result = self.forward(*input, **kwargs)
>     207         for hook in self._forward_hooks.values():
>     208             hook_result = hook(self, input, result)

> ~/anaconda3/envs/torch/lib/python3.6/site-packages/torch/nn/modules/rnn.py in forward(self, input, hx)
>      89             dropout_state=self.dropout_state
>      90         )
> ---> 91         output, hidden = func(input, self.all_weights, hx)
>      92         if is_packed:
>      93             output = PackedSequence(output, batch_sizes)

> ~/anaconda3/envs/torch/lib/python3.6/site-packages/torch/nn/_functions/rnn.py in forward(input, *fargs, **fkwargs)
>     341         else:
>     342             func = AutogradRNN(*args, **kwargs)
> --> 343         return func(input, *fargs, **fkwargs)
>     344 
>     345     return forward

> ~/anaconda3/envs/torch/lib/python3.6/site-packages/torch/autograd/function.py in _do_forward(self, *input)
>     200         self._nested_input = input
>     201         flat_input = tuple(_iter_variables(input))
> --> 202         flat_output = super(NestedIOFunction, self)._do_forward(*flat_input)
>     203         nested_output = self._nested_output
>     204         nested_variables = _unflatten(flat_output, self._nested_output)

> ~/anaconda3/envs/torch/lib/python3.6/site-packages/torch/autograd/function.py in forward(self, *args)
>     222     def forward(self, *args):
>     223         nested_tensors = _map_variable_tensor(self._nested_input)
> --> 224         result = self.forward_extended(*nested_tensors)
>     225         del self._nested_input
>     226         self._nested_output = result

> ~/anaconda3/envs/torch/lib/python3.6/site-packages/torch/nn/_functions/rnn.py in forward_extended(self, input, weight, hx)
>     283             hy = tuple(h.new() for h in hx)
>     284 
> --> 285         cudnn.rnn.forward(self, input, hx, weight, output, hy)
>     286 
>     287         self.save_for_backward(input, hx, weight, output)

> ~/anaconda3/envs/torch/lib/python3.6/site-packages/torch/backends/cudnn/rnn.py in forward(fn, input, hx, weight, output, hy)
>     254         w.zero_()
>     255         params = get_parameters(fn, handle, w)
> --> 256         _copyParams(weight, params)
>     257 
>     258         if tuple(hx.size()) != hidden_size:

> ~/anaconda3/envs/torch/lib/python3.6/site-packages/torch/backends/cudnn/rnn.py in _copyParams(params_from, params_to)
>     182         for param_from, param_to in zip(layer_params_from, layer_params_to):
>     183             print(param_from.type(), param_to.type())
> --> 184             assert param_from.type() == param_to.type()
>     185             param_to.copy_(param_from)
>     186 

> AssertionError: 

Thanks for your help!

i suspect that the data returned from the DataLoader is returned as DoubleTensor instead of what the model wants by default: FloatTensor.

Right after # get the inputs, i.e. after the inputs, labels = ..., add the line:

inputs = inputs.float()
1 Like

Thanks a ton, Soumith! I’d used a transform in the DataLoader, but only transformed the targets to FloatTensor.