Hi, I tend to run my binary segmentation codes with loss function nn.BCEWithLogitsLoss(). The positive and negative target is not balanced so I tend to use pos_weight. However, it returns the runtime error which I don’t know why.
Code:
model = model.cuda()
w_pos = torch.tensor([4.538])
criterion = nn.BCEWithLogitsLoss(pos_weight = w_pos)
optimizer = torch.optim.Adam(model.parameters(), lr=0.001)
…
im = Variable(data[0].cuda())
labels = Variable(data[1].cuda())
out = model(im)
out1 = torch.squeeze(out)
loss = criterion(out1, labels.float())
Outputs:
Traceback (most recent call last):
File “D:/DSC/Planet_test/train_200.py”, line 317, in
loss = criterion(out1, labels.float())
File “C:\Users\he425\AppData\Local\Programs\Python\Python37\lib\site-packages\torch\nn\modules\module.py”, line 547, in call
result = self.forward(*input, **kwargs)
File “C:\Users\he425\AppData\Local\Programs\Python\Python37\lib\site-packages\torch\nn\modules\loss.py”, line 601, in forward
reduction=self.reduction)
File “C:\Users\he425\AppData\Local\Programs\Python\Python37\lib\site-packages\torch\nn\functional.py”, line 2100, in binary_cross_entropy_with_logits
return torch.binary_cross_entropy_with_logits(input, target, weight, pos_weight, reduction_enum)
RuntimeError: expected device cpu and dtype Float but got device cuda:0 and dtype Float
Is there any solutions for this issue?