Now it turns out that if you use annotated code, you get an error, while unannotated code works fine
  # for image,mask in train_dl:
        for i in range(5):
                 data=torch.randn(8,3,128,128)
                 target=torch.randn(8,4,128,128)
                 optimizer.zero_grad()
                 data=data.to(device).float()
                 output=model(data)
                 output=torch.sigmoid(output)
                 output=output.to('cpu')
                 loss=criterion(output,target)
                 epoch_loss += loss
                 loss.backward()
                 optimizer.step()
                 scheduler.step()
                 # image = image.to(device).float()
                 # optimizer.zero_grad()
                 # pre_mask=model(image)
                 # pre_mask=pre_mask.to('cpu')
                 # pre_mask=torch.sigmoid(pre_mask)
                 # loss=criterion(pre_mask,mask)
                 # epoch_loss+=loss
                 # loss.backward()
                 # optimizer.step()
                 # scheduler.step()

I really don’t know why, is it the data problem that caused the error