我更改了标量类型float的预期对象,但在Pytorch中仍然是Long

问题描述 投票:0回答:1

做二进制类分类。我使用二元交叉熵作为损失函数(nn.BCEloss()),最后一层的单位是一。

在我将(输入,目标)放入损失函数之前,我将目标从Long转换为浮动。只有DataLoader的最后一步出现错误消息,错误消息如下所示。 "RuntimeError: Expected object of scalar type Float but got scalar type Long for argument #2 'target'" DataLoader(如果批量大小不匹配,我丢弃最后一批)在代码中定义,我不确定是否与错误有关联。

我试图打印目标和输入的类型(神经网络的输出),并且两个变量的类型都是浮点数。我把“类型结果”和代码放在下面。

trainloader = torch.utils.data.DataLoader(trainset, batch_size=BATCH_SIZE,
                                          shuffle=True, drop_last=True)
loss_func = nn.BCELoss() 

# training 
for epoch in range(EPOCH):
    test_loss = 0
    train_loss = 0

    for step, (b_x, b_y) in enumerate(trainloader):        # gives batch data
        b_x = b_x.view(-1, TIME_STEP, 1)              # reshape x to (batch, time_step, input_size)
        print("step: ", step)
        b_x = b_x.to(device) 
        print("BEFORE|b_y type: ",b_y.type())
        b_y = b_y.to(device, dtype=torch.float)
        print("AFTER|b_y type: ",b_y.type())
        output = rnn(b_x)                               # rnn output
        print("output type:", output.type())
        loss = loss_func(output, b_y)  # !!!error occurs when trainloader enumerate the final step!!!                 

        train_loss = train_loss + loss

        optimizer.zero_grad()                           
        loss.backward()                                 
        optimizer.step()  
#### type result and the error message####
... 
step:  6
BEFORE|b_y type:  torch.LongTensor
AFTER|b_y type:  torch.cuda.FloatTensor
output type: torch.cuda.FloatTensor
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-18-e028fcb6b840> in <module>
     30         b_y = b_y.to(device)
     31         output = rnn(b_x)
---> 32         loss = loss_func(output, b_y)
     33         test_loss = test_loss + loss
     34         rnn.train()

~/venvs/tf1.12/lib/python3.5/site-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
    487             result = self._slow_forward(*input, **kwargs)
    488         else:
--> 489             result = self.forward(*input, **kwargs)
    490         for hook in self._forward_hooks.values():
    491             hook_result = hook(self, input, result)

~/venvs/tf1.12/lib/python3.5/site-packages/torch/nn/modules/loss.py in forward(self, input, target)
    502     @weak_script_method
    503     def forward(self, input, target):
--> 504         return F.binary_cross_entropy(input, target, weight=self.weight, reduction=self.reduction)
    505 
    506 

~/venvs/tf1.12/lib/python3.5/site-packages/torch/nn/functional.py in binary_cross_entropy(input, target, weight, size_average, reduce, reduction)
   2025 
   2026     return torch._C._nn.binary_cross_entropy(
-> 2027         input, target, weight, reduction_enum)
   2028 
   2029 

RuntimeError: Expected object of scalar type Float but got scalar type Long for argument #2 'target'
python pytorch
1个回答
2
投票

似乎正确地更改了类型,因为您声明在打印类型时从Pyyerch观察到更改:

返回带有指定设备的Tensor和(可选)dtype。如果dtype为None,则推断为self.dtype。当non_blocking尝试尽可能地相对于主机进行异步转换时,例如,将带有固定内存的CPU Tensor转换为CUDA Tensor。设置复制后,即使Tensor已匹配所需的转换,也会创建新的Tensor。

和其他方法一样

b_y = b_y.to(device).float()

不应该有显着差异,因为.float()相当于.to(..., torch.float32)。和.float相当于.float32。你可以在抛出错误之前验证b_y的类型并编辑问题吗? (我本来是一个评论 - 但我想添加更多细节。我会尽力提供帮助)

© www.soinside.com 2019 - 2024. All rights reserved.