Skip to content

RuntimeError: expected Double tensor (got Float tensor) #2138

@diggerdu

Description

@diggerdu
  # -*- coding: utf-8 -*-
  from __future__ import absolute_import
  import numpy as np
  import torch
  import torch.nn as nn
  from torch.autograd import Variable
    
    
  kernel = torch.from_numpy(np.random.random_sample((1, 3))).double()
  input_ = torch.from_numpy(np.random.random_sample((1, 1, 1, 6))).double()
                                                                                                           
  print(kernel)
  print(input_)
   
  model = nn.Sequential(nn.Conv2d(1, 1, (1, 3), stride=1, padding=0, bias=False))
  model[0].weight.data.copy_(kernel)
  A = Variable(input_)
  out = model(A)
  print(out.data)

But the input is actually DoubleTensor

$ python test.py

 0.0100  0.6528  0.8658
[torch.DoubleTensor of size 1x3]


(0 ,0 ,.,.) = 
  0.9582  0.9593  0.1207  0.5719  0.3984  0.4603
[torch.DoubleTensor of size 1x1x1x6]

Traceback (most recent call last):
  File "test.py", line 18, in <module>
    out = model(A)
  File "/home/diggerdu/anaconda3/lib/python3.6/site-packages/torch/nn/modules/module.py", line 206, in __call__
    result = self.forward(*input, **kwargs)
  File "/home/diggerdu/anaconda3/lib/python3.6/site-packages/torch/nn/modules/container.py", line 64, in forward
    input = module(input)
  File "/home/diggerdu/anaconda3/lib/python3.6/site-packages/torch/nn/modules/module.py", line 206, in __call__
    result = self.forward(*input, **kwargs)
  File "/home/diggerdu/anaconda3/lib/python3.6/site-packages/torch/nn/modules/conv.py", line 237, in forward
    self.padding, self.dilation, self.groups)
  File "/home/diggerdu/anaconda3/lib/python3.6/site-packages/torch/nn/functional.py", line 40, in conv2d
    return f(input, weight, bias)
RuntimeError: expected Double tensor (got Float tensor)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions