RuntimeError: Expected object of type torch.FloatTensor but found type torch.cuda.FloatTensor for argument #2 'weight', help me understand how and when to use cuda

username: barakb
cooked: <p>Hi, I got the above error, and from the docs I’m not able to understand where should I adjust to cuda.<br>
For example in the docs they are showing how to create a tensor on the gpu, but what with the case when I’m loading Images? still the same?</p>
<p>Now I have a situation my tensors are cuda type, but my layers not, and that’s why I’m getting :<br>
"Expected object of type torch.FloatTensor but found type torch.cuda.FloatTensor ".<br>
Now I need to put .cuda() on each layer?(or something like this) doesn’t make sense.</p>
<p>This is were the program stoped, in the nn.conv2d , tried to “.cuda()”<br>
, didn’t help</p>
<pre><code class="lang-auto">             return nn.Sequential(
                nn.Conv2d(inp, oup, 3, stride, 1, bias=False).cuda(),
                nn.BatchNorm2d(oup),
                nn.ReLU(inplace=True)
            )
</code></pre>
<p>I tried few of the solutions online, but nothing worked.</p>

username: ptrblck
cooked: <p>Your data as well as your model should be both on the GPU.<br>
To transfer both you can use something like this:</p>
<pre><code class="lang-python">model = model.to('cuda')
data = data.to('cuda')
target = target.to('cuda')
</code></pre>
<p>You don’t need to push each layer onto the GPU. The model will push all it’s layers to the device.</p>

username: JiageWang
cooked: <p>I have already do this job, but it doesn’t work.It seems the first batch can work well while the next gets error<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/2X/2/2f05ddab50ac44e6bc5879372bb323031d460eb7.png" data-download-href="https://discuss.pytorch.org/uploads/default/2f05ddab50ac44e6bc5879372bb323031d460eb7" title="Snipaste_2018-11-03_19-24-47"><img src="https://discuss.pytorch.org/uploads/default/original/2X/2/2f05ddab50ac44e6bc5879372bb323031d460eb7.png" alt="Snipaste_2018-11-03_19-24-47" data-base62-sha1="6HZ0xzwhKFhXu7bs99ZcEx7wqVN" width="690" height="345" data-dominant-color="313033"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">Snipaste_2018-11-03_19-24-47</span><span class="informations">1197×599 31.6 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>

username: ptrblck
cooked: <p>Could you post your code so that we could have a look?<br>
The issue is strange, as the first batch seems to work.<br>
If you can’t post code due to some restrictions, could you come up with a small dummy code snippet to reproduce this issue?</p>

username: JiageWang
cooked: <p>I found the bug when I was copying my code.this is my test data that not transfered lead to the error.<br>
Anyway, thanks for your attention.<img src="https://discuss.pytorch.org/images/emoji/apple/blush.png?v=6" title=":blush:" class="emoji" alt=":blush:"></p>

username: chunchun
cooked: <p>could you please tell how to solve this problem?</p>