To do the binary class classification. I use binary cross entropy to be the loss function(nn.BCEloss()), and the units of last layer is one.

Before I put (input, target) into loss function, I cast target from Long to float. Only the final step of the DataLoader comes the error messages, and the error message is as bellows. "RuntimeError: Expected object of scalar type Float but got scalar type Long for argument #2 'target'" The DataLoader(I drop the last batch if the batch size is not match) is defined in the code, I'm not sure if there is a correlation with the error.

I have tried to print the type of the target and input(output of the Neural Network), and the type of both variable is float. I put the "type result" and the code bellow.

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'

1 Answers

1
modesitt On Best Solutions

It appears that the type is correctly being changed, as you state that you observer the change when printing the types and from Pytorch:

Returns a Tensor with the specified device and (optional) dtype. If dtype is None it is inferred to be self.dtype. When non_blocking, tries to convert asynchronously with respect to the host if possible, e.g., converting a CPU Tensor with pinned memory to a CUDA Tensor. When copy is set, a new Tensor is created even when the Tensor already matches the desired conversion.

and other methods like

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

should not be measurably different as , again, .float() is equivalent to .to(..., torch.float32). and .float is equivalent to .float32. Can you verify the type of b_y right before the error is thrown and edit the question? (I would have made this a comment - but I wanted to add more detail. I will try to help when that is provided)