0
model.train()
for epoch in range(3):

  running_loss = 0.0
  for i,data in enumerate(train_loader,0):
    inputs,labels,_ = data
    inputs = inputs[:,None,:]
    #labels = labels[:,None,:]
    print(inputs.shape)
    inputs = inputs.to(device=device)
    labels = labels.to(device=device)

    optimizer.zero_grad()
    #print("Input: ", inputs.shape)
    #print("Labels: ",labels.shape)
    outputs = model(inputs)
    #thresh_output = threshold(outputs,0.5)
    loss = criterion(outputs,labels)
    #dice_score = dice_loss(thresh_output,labels).item()
    loss.backward()
    optimizer.step()
    #wandb.log({"Train_Loss":loss})
    if(i%500 == 0):
      scheduler.step()
    running_loss += loss.item()
    # if(i == 1000):
    #   break
    print("Epoch : {}, iteration : {} and Loss : {}".format(epoch+1,i+1,loss.item()))

The code is given above. I get the following error when I try to run it:

torch.Size([1, 1, 256, 256]) /usr/local/lib/python3.7/dist-packages/torch/nn/functional.py:1944: UserWarning: nn.functional.sigmoid is deprecated. Use torch.sigmoid instead. warnings.warn("nn.functional.sigmoid is deprecated. Use torch.sigmoid instead.")
AttributeError Traceback (most recent call last) in () 16 outputs = model(inputs) 17 #thresh_output = threshold(outputs,0.5) ---> 18 loss = criterion(outputs,labels) 19 #dice_score = dice_loss(thresh_output,labels).item() 20 loss.backward()

2 frames /usr/local/lib/python3.7/dist-packages/torch/nn/functional.py in binary_cross_entropy(input, target, weight, size_average, reduce, reduction) 3053 else: 3054 reduction_enum = _Reduction.get_enum(reduction) -> 3055 if target.size() != input.size(): 3056 raise ValueError( 3057 "Using a target size ({}) that is different to the input size ({}) is deprecated. "

AttributeError: 'tuple' object has no attribute 'size'
desertnaut
  • 57,590
  • 26
  • 140
  • 166
  • Could you print the shape of the output and the labels of your model? It is probably a tuple and you need to select the first object. I guess this would work ``` loss = criterion(outputs[0],labels) ``` but more info would be helpful. – Hatem Jul 09 '22 at 22:22
  • The output is a tuple so I just wrote print(output) and this is what I have: (tensor([[[[0.5195, 0.5195, 0.5195, ..., 0.5407, 0.5407, 0.5407], [0.5596, 0.5596, 0.5596, ..., 0.5975, 0.5975, 0.5975]]]], device='cuda:0', grad_fn=), tensor([[[[0.5616, 0.5616, 0.5616, ..., 0.6373, 0.6373, 0.6373] ..]]]]. .The length of the tuple is 3 – Alvi Rahman Jul 10 '22 at 15:59
  • I also tried your fix @Hatem. It gave me this error: 3056 raise ValueError( 3057 "Using a target size ({}) that is different to the input size ({}) is deprecated. " -> 3058 "Please ensure they have the same size.".format(target.size(), input.size()) 3059 ) 3060 ValueError: Using a target size (torch.Size([1, 256, 256])) that is different to the input size (torch.Size([1, 1, 256, 256])) is deprecated. Please ensure they have the same size. – Alvi Rahman Jul 10 '22 at 16:04

0 Answers0