Expected device cuda:0 and dtype Float but got device cpu and dtype Float

username: Mandy
cooked: <p>Hi all,</p>
<p>I’m working on a project and I’d like to use <code>hook</code> to achieve the goal which is only update some weight tensor in the training.</p>
<p>However, I encountered a problem and tried my best with no result. Below is my codes:</p>
<pre data-code-wrap="python"><code class="lang-python"> def hook(self, model, inputs):
        with torch.no_grad():
            print(model.weight.cuda().data.dtype)
            print(self.sparse_mask[self.type[model]].dtype)
            #model.weight.data = model.weight.data * self.sparse_mask[self.type[model]]
            model.weight.cuda().data = model.weight.cuda().data * self.sparse_mask[self.type[model]]
 
    def register_hook(self,module):
        self.handle = module.register_forward_pre_hook(self.hook)
</code></pre>
<p>And I got this output</p>
<blockquote>
<p>torch.float32<br>
torch.float32</p>
</blockquote>
<pre><code class="lang-auto">Traceback (most recent call last):
  File "&lt;input&gt;", line 1, in &lt;module&gt;
  File ".pycharm_helpers/pydev/_pydev_bundle/pydev_umd.py", line 197, in runfile
    pydev_imports.execfile(filename, global_vars, local_vars)  # execute the script
  File ".pycharm_helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "/sparse_processing.py", line 352, in &lt;module&gt;
    jsegnet(input_data)
  File "/.conda/envs/mengdietao/lib/python3.7/site-packages/torch/nn/modules/module.py", line 547, in __call__
    result = self.forward(*input, **kwargs)
  File "jsegnet.py", line 199, in forward
    h = self.block1(x)   #[1, 32, 160, 160]
  File "/.conda/envs/mengdietao/lib/python3.7/site-packages/torch/nn/modules/module.py", line 547, in __call__
    result = self.forward(*input, **kwargs)
  File "/jsegnet.py", line 75, in forward
    h = self.conv_a(x)
  File "/.conda/envs/mengdietao/lib/python3.7/site-packages/torch/nn/modules/module.py", line 547, in __call__
    result = self.forward(*input, **kwargs)
  File "/tangshan/tangshan_data/image_segmentation/libs/models/jsegnet.py", line 51, in forward
    return super(_ConvBatchNormReLU, self).forward(x)
  File "/.conda/envs/mengdietao/lib/python3.7/site-packages/torch/nn/modules/container.py", line 92, in forward
    input = module(input)
  File "/.conda/envs/mengdietao/lib/python3.7/site-packages/torch/nn/modules/module.py", line 539, in __call__
    result = hook(self, input)
  File "/sparse_processing.py", line 229, in hook
    model.weight.cuda().data = model.weight.cuda().data * self.sparse_mask[self.type[model]]

RuntimeError: expected device cuda:0 and dtype Float but got device cpu and dtype Float

</code></pre>
<p>Both <code>dtype</code> of <code>model.weight.cuda().data</code> and <code>self.sparse_mask[self.type[model]]</code> are <code>torch.float32</code>, my <code>input_data</code> is <code>data = data.cuda().float()</code></p>
<pre><code class="lang-auto">data = data.cuda().float()
</code></pre>
<p>I’d like to keep it calculate on GPU but I can’t figure out how to fix that problem.</p>
<p>Any response will be really appreciated!!</p>

username: Mandy
cooked: <p>I GOT THE SOLUTION!</p>
<pre><code class="lang-auto"> def hook(self, model, inputs):
        with torch.no_grad():
       
            model.weight.cpu().data = model.weight.cpu().data * self.sparse_mask[self.type[model]]
</code></pre>
<p>or</p>
<pre><code class="lang-auto"> def hook(self, model, inputs):
        with torch.no_grad():
       
            model.weight.data = model.weight.data * self.sparse_mask[self.type[model]].cuda()
</code></pre>