I got the following issue. Kindly help

File “/home/cyz/anaconda3/envs/ibrahim/lib/python3.8/site-packages/torch/autograd/init.py”, line 154, in backward
RuntimeError: element 0 of tensors does not require grad and does not have a grad_fn

This error is raised if you are trying to call backward on a tensor, which isn’t attached to any computation graph.
Common issues are:

  • explicit detaching the tensor via tensor.detach() from the computation graph
  • disabling gradient calculation e.g. via torch.no_grad() or by globally disabling Autograd
  • using 3rd party libs (e.g. numpy) without writing a custom atograd.Function and its backward method
  • implicitly detaching by rewrapping a tensor via x = torch.tensor(x)

@ptrblck Below is my training loop.
with torch.enable_grad():
classifier = classifier.train()

        for i, (points, target) in tqdm(enumerate(trainDataLoader), total=len(trainDataLoader), smoothing=0.9):

            if i == 6862:

            points = points.data.numpy()
            target = preprocessed(target)
            # points[:, :, :3] = provider.rotate_point_cloud_z(points[:, :, :3])
            points = torch.Tensor(points)
            points, target = points.float().cuda(), target.cuda()
            points = torch.reshape(points, (BATCH_SIZE, 36, NUM_POINT, 3))
            points = points.transpose(3, 2)
            # target1 =target

            seg_pred = classifier(points)

            seg_pred = seg_pred.contiguous().view(-1, NUM_CLASSES)
            seg_pred =torch.argmax(seg_pred,1)
            # print(seg_pred.shape)

            target = target.view(-1, 1)[:, 0]

            loss = criterion(seg_pred, target.cuda())

            seg_pred =seg_pred.cpu().detach().numpy()
            # target1 = target.clone().detach()
            # seg_pred1 = seg_pred.clone().detach()

            # loss = lovasz_softmax(seg_pred, target, ignore=None) + loss_func(seg_pred, target)
            # loss.backward()


IoU loss I am using as below:

class IoULoss(torch.nn.Module):
def init(self, weight=None, size_average=True):
super(IoULoss, self).init()

def forward(self, inputs, targets, smooth=1):
    import torch.nn.functional as F
    # comment out if your model contains a sigmoid or equivalent activation layer
    inputs = F.sigmoid(inputs)

    # flatten label and prediction tensors
    inputs = inputs.view(-1)
    targets = targets.view(-1)

    # intersection is equivalent to True Positive count
    # union is the mutually inclusive area of all labels & predictions
    intersection = (inputs * targets).sum()
    total = (inputs + targets).sum()
    union = total - intersection

    IoU = (intersection + smooth) / (union + smooth)

    return 1 - IoU

classifier = MODEL.segmodel(NUM_CLASSES).cuda()
# criterion = MODEL.get_loss().cuda()
criterion = IoULoss().cuda()

torch.argmax is detaching the tensor, as this operation is not differentiable:

seg_pred =torch.argmax(seg_pred,1)
loss = criterion(seg_pred, target.cuda())

so you would have to avoid it.

@ptrblck What should I use instead of this?

After removing torch.argmax, now I got another errror. Please help me out. I will be very thankful to you.

RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation: [torch.cuda.FloatTensor [8, 36, 2048]], which is output 0 of ReluBackward0, is at version 1; expected version 0 instead. Hint: enable anomaly detection to find the operation that failed to compute its gradient, with torch.autograd.set_detect_anomaly(True).

Thanks. Solved the issue.