RuntimeError: Expected object of type torch.cuda.FloatTensor but found type torch.FloatTensor for argument #4 'mat1'

Cannot figure why I keep getting the following error:
Traceback (most recent call last):

File “/mnt/ilcompfad1/user/Test/transf/model.py”, line 149, in forward
linear1 = self.linear1(concate)

RuntimeError: Expected object of type torch.cuda.FloatTensor but found type torch.FloatTensor for argument #4 ‘mat1’

Code

class NewConvModule(nn.Module):
            def __init__(self, window_sizes, cov_dim, mem_dim):
                super(NewConvModule, self).__init__()
                self.window_sizes = window_sizes
                self.cov_dim = cov_dim
                self.mem_dim = mem_dim
                self.d_tempconv = {}
                self.d_dmax = {}
                for window_size in self.window_sizes:
                    self.d_tempconv[window_size] = TemporalConvoluation(self.cov_dim, self.mem_dim, window_size)
                    self.d_tempconv[window_size].to(_device)
                    self.d_dmax[window_size] = DMax(dimension=0, windowSize=window_size)
                    self.d_dmax[window_size].to(_device)

                self.linear1 = nn.Linear(len(window_sizes) * mem_dim, mem_dim)
                self.relu1 = nn.ReLU()
                self.tanh1 = nn.Tanh()

            def forward(self, input, sizes):
                conv = [None] * len(self.window_sizes)                         #list type
                pool = [None] * len(self.window_sizes)                        # #list type
                for i, window_size in enumerate(self.window_sizes):
                    tempconv = self.d_tempconv[window_size](input)
                    conv[i] = self.relu1(tempconv)
                    pool[i] = self.d_dmax[window_size](conv[i], sizes)

                concate = torch.cat(pool, 1)

                linear1 = self.linear1(concate)  #ERROR HERE

                output = self.tanh1(linear1)
                return output

        conv_module = NewConvModule(window_sizes, cov_dim, mem_dim)
        conv_module.to(_device)
        return conv_module

Any ideas? I exhausted everything I could find online…

2 Likes

Could you add the print statements in forward just before the erroneous line?

print(linear1.device)
print(self.linear1.weight.device)

As the error states, some tensors are not pushed onto the GPU.

5 Likes

And you could try switching from dicts to torch.nn.Modulelists as usually switching devices with plain iterables does not work that well

2 Likes

It says cuda is the device forlinear1

But it says cpu forprint(concate.device)

So concate tensor is on cpu. I do not understand why…

This saved me, thanks!

adding .cuda() after .float() to tensor saved life