What should I do with the weight type

username: Quintia_Zhao
cooked: <p>Hi, I’m using the gdl loss function from <a href="https://github.com/ginobilinie/medSynthesisV1/blob/master/nnBuildUnits.py" rel="noopener nofollow ugc">https://github.com/ginobilinie/medSynthesisV1/blob/master/nnBuildUnits.py</a>, when I call and run the code on my own, it shows the problem:</p>
<p><strong>RuntimeError: Input type (torch.FloatTensor) and weight type (torch.cuda.FloatTensor) should be the same</strong><br>
I’m new in pytroch, and have no idea what’s wrong with anywhere and what should I do for the next step. Any help will be highly appreciated!!</p>
<p>I call it in this way:</p>
<blockquote>
<p>from model.unet2 import UNet<br>
device = torch.device(“cuda” if torch.cuda.is_available() else “cpu”)<br>
net = UNet(in_channels=1, out_channels=1, init_features=32)<br>
net.to(device)<br>
ctimgs= read_img(“k1/train/ct_data/patient_002_ct.gipl”)<br>
mrimgs = read_img(k1/train/mri_data/patient_002_mr_T1.gipl")<br>
ctimgs1 = ctimgs[0,:,:]<br>
mrimgs1 = mrimgs[0,:,:]<br>
ctimgs1 = ctimgs1[np.newaxis,np.newaxis,:,:]<br>
mrimgs1 = mrimgs1[np.newaxis,np.newaxis,:,:]<br>
ctimgs1 = torch.from_numpy(ctimgs1)<br>
mrimgs1 = torch.from_numpy(mrimgs1)<br>
out = net(mrimgs1)<br>
gdlloss = gdl_loss()<br>
loss = gdlloss(out,ctimgs1)</p>
</blockquote>
<p>this is the gdl_loss</p>
<pre><code>class gdl_loss(nn.Module):
def __init__(self, pNorm=2):
    super(gdl_loss, self).__init__()
    self.convX = nn.Conv2d(1, 1, kernel_size=(1, 2), stride=1, padding=(0, 1), bias=False)
    self.convY = nn.Conv2d(1, 1, kernel_size=(2, 1), stride=1, padding=(1, 0), bias=False)
    filterX = torch.FloatTensor([[[[-1, 1]]]])  # 1x2
    filterY = torch.FloatTensor([[[[1], [-1]]]])  # 2x1
    self.convX.weight = torch.nn.Parameter(filterX,requires_grad=False)
    self.convY.weight = torch.nn.Parameter(filterY,requires_grad=False)
    self.pNorm = pNorm

def forward(self, pct, ct):
    assert not ct.requires_grad
    assert pct.dim() == 4
    assert ct.dim() == 4
    assert pct.size() == ct.size(), "{0} vs {1} ".format(pct.size(), ct.size())
    pred_dx = torch.abs(self.convX(pct))
    pred_dy = torch.abs(self.convY(pct))
    gt_dx = torch.abs(self.convX(ct))
    gt_dy = torch.abs(self.convY(ct))
    grad_diff_x = torch.abs(gt_dx - pred_dx)
    grad_diff_y = torch.abs(gt_dy - pred_dy)
    mat_loss_x = grad_diff_x ** self.pNorm
    mat_loss_y = grad_diff_y ** self.pNorm  # Batch x Channel x width x height
    shape = ct.shape
    mean_loss = (torch.sum(mat_loss_x) + torch.sum(mat_loss_y)) / (shape[0] * shape[1] * shape[2] * shape[3])
    return mean_loss
</code></pre>

username: DAlolicorn
cooked: <p>You specified <code>net.to(device)</code>, so the weights are in GPU memory , and the data type will be <code>torch.cuda.FloatTensor</code>.</p>
<p>However, you did not load your input to GPU, it’s still in CPU, which is type <code>torch.FloatTensor</code>.<br>
So you should call something like:<br>
<code>mrimgs1 = mrimgs1.to(device)</code></p>

username: Quintia_Zhao
cooked: <p>Hi! thanks for your reply, I add it before the net,<br>
it’s now:</p>
<blockquote>
<p>mrimgs1 = mrimgs1.to(device)<br>
ctimgs1 = ctimgs1. to(device)<br>
out = net(mrimgs1)<br>
gdlloss = gdl_loss()<br>
gg = gdlloss(out,ctimgs1)</p>
</blockquote>
<p>but the result is now:<br>
<strong>RuntimeError: Input type (torch.cuda.FloatTensor) and weight type (torch.FloatTensor) should be the same</strong></p>

username: DAlolicorn
cooked: <p>That is because you did not add <code>gdlloss</code> to GPU.<br>
<code>gdlloss = gdlloss.to(device)</code></p>

username: Quintia_Zhao
cooked: <p>thanks sooooo much!!! it works now!!!<br>
really appreciated it !!!</p>
<p>and now I know that the loss should also to Device <img src="https://discuss.pytorch.org/images/emoji/apple/joy.png?v=9" title=":joy:" class="emoji" alt=":joy:"></p>

username: DAlolicorn
cooked: <p>No problem.</p>
<p>To clarify, it’s because <code>gdl_loss</code> contains weights (<code>torch.nn.Parameter</code>), so you need to load them into GPU by calling <code>to(device)</code> before interacting them with inputs that are in GPU memory, just like what you did to <code>net</code>.</p>
<p>If you are using losses that does not define new weights, you don’t need to do so.</p>