Error comes: RuntimeError: Input type (torch.cuda.FloatTensor) and weight type (torch.FloatTensor) should be the same

cbd: <p>I checked in forward function using print(self.device). GPU id is cuda:0 and input tensor is  also device=‘cuda:0’</p>
<p>Following define in “def <strong>init</strong>(self, opt):” function of class<br>
self.r1= nn.Conv2d(6, 20, kernel_size=3,stride=1,padding=1)</p>
<p>Below calling from " def forward(self):" function of class which gives error.<br>
self.A=self.r1(self.A)</p>
<p>Looking after many topic on forum i come to know that model should be run on GPU. But it is already runs on GPU as i print it in forward function.</p>

jmandivarapu1: <aside class="quote no-group quote-modified" data-username="cbd" data-post="1" data-topic="81384">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/c/13edae/48.png" class="avatar"> cbd:</div>
<blockquote>
<p>Looking after may topic on forum i come to know that model should be run on GPU. But it is already runs on GPU as i print it in forward function.</p>
</blockquote>
</aside>
<p>Hey can you do one more thing print any layer weights in your model before passing the input.</p>
<p>Eg: <code>print(net.conv1.weight)</code> . It will print your weights but at the end you will also see <code>device='cuda:0', requires_grad=True)</code> which means device weights are in GPU cuda.</p>

cbd: <p>I print the weight it shows following.</p>
<p>[ 0.0092,  0.1345, -0.0752]]]], requires_grad=True).</p>
<p>No cuda:0 but when i print(model.device) before that it showing cuda:0.</p>
<p>If we assign GPU to model, can’t layer weight will also assign<br>
the GPU. where is the conflict?</p>

jmandivarapu1: <p>can you do <code>model.to('cuda')</code> before sending the inputs</p>

cbd: <p>I tried but it shows the error " ‘model’ object has no attribute ‘to’</p>

jmandivarapu1: <p>can you paste your model here and the way you send it to the GPU</p>

cbd: <p>I think i have made silly mistake and not assign the GPU. Let me explain the flow.</p>
<p>NOTE: “model1” is defined as</p>
<p>from abc import ABC<br>
class model1(ABC):</p>
<p>I am modifying existing code. I try to explain it in simple manner.</p>
<p>I create model in train.py by taking instance of class “model1”. “model1” doesn’t contain CNN. From “model1” i am passing image to CNN which return float tensor with cuda. Now i want to do post processing in “model1”. So want to do convolution as post processing with the image returned by CNN. Thats the line where the issue comes.</p>
<p>In train.py i created instance of “model1” and tried to assign cuda by writing</p>
<p>(1)  model.cuda() it shows AttributeError: ‘model’ object has no attribute ‘cuda’<br>
(2)  model.to(‘cuda’) it shows " ‘model’ object has no attribute ‘to’</p>
<p>How can i check model is running on cpu or gpu? Writing “print(self.device)” in forward function of “module1” is my mistake i think so.</p>

jmandivarapu1: <p>unfortunately one these below needs to be successful to shift the model to GPU otherwise it’s just a CPU based model. If u can post sample code for others to debug that would help</p>
<pre><code class="lang-auto">(1) model.cuda() 
(2) model.to(‘cuda’)
</code></pre>

cbd: <p>I transformed the post processing in CNN class. Thanks for your prompt reply. It helps in many ways.</p>