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

username: NadiaMe
cooked: <p>Cannot figure why I keep getting the following error:<br>
Traceback (most recent call last):</p>
<p>File “/mnt/ilcompfad1/user/Test/transf/model.py”, line 149, in forward<br>
linear1 = self.linear1(concate)</p>
<p>RuntimeError: Expected object of type torch.cuda.FloatTensor but found type torch.FloatTensor for argument <span class="hashtag">#4</span> ‘mat1’</p>
<p>Code</p>
<pre><code class="lang-auto">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
</code></pre>
<p>Any ideas? I exhausted everything I could find online…</p>

username: ptrblck
cooked: <p>Could you add the print statements in <code>forward</code> just before the erroneous line?</p>
<pre><code class="lang-python">print(linear1.device)
print(self.linear1.weight.device)
</code></pre>
<p>As the error states, some tensors are not pushed onto the GPU.</p>

username: justusschock
cooked: <p>And you could try switching from dicts to <a href="https://pytorch.org/docs/stable/nn.html#torch.nn.ModuleList" rel="nofollow noopener"><code>torch.nn.Modulelists</code></a> as usually switching devices with plain iterables does not work that well</p>

username: NadiaMe
cooked: <p>It says cuda is the device for<code>linear1</code></p>
<p>But it says cpu for<code>print(concate.device)</code></p>
<p>So concate tensor is on cpu. I do not understand why…</p>

username: NadiaMe
cooked: <p>This saved me, thanks!</p>

username: mathematics
cooked: <p>adding <code>.cuda()</code> after <code>.float()</code> to tensor saved life</p>