I got the following issue. Kindly help

ibrahim_khan: <p>File “/home/cyz/anaconda3/envs/ibrahim/lib/python3.8/site-packages/torch/autograd/<strong>init</strong>.py”, line 154, in backward<br>
Variable._execution_engine.run_backward(<br>
RuntimeError: element 0 of tensors does not require grad and does not have a grad_fn</p>

ptrblck: <p>This error is raised if you are trying to call <code>backward</code> on a tensor, which isn’t attached to any computation graph.<br>
Common issues are:</p>
<ul>
<li>explicit detaching the tensor via <code>tensor.detach()</code> from the computation graph</li>
<li>disabling gradient calculation e.g. via <code>torch.no_grad()</code> or by globally disabling Autograd</li>
<li>using 3rd party libs (e.g. numpy) without writing a custom <code>atograd.Function</code> and its <code>backward</code> method</li>
<li>implicitly detaching by rewrapping a tensor via <code>x = torch.tensor(x)</code>
</li>
</ul>

ibrahim_khan: <p><a class="mention" href="/u/ptrblck">@ptrblck</a>  Below is my training loop.<br>
with torch.enable_grad():<br>
classifier = classifier.train()</p>
<pre><code>        for i, (points, target) in tqdm(enumerate(trainDataLoader), total=len(trainDataLoader), smoothing=0.9):
            optimizer.zero_grad()

            if i == 6862:
                break

            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)
            print(seg_pred.shape)

            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]
            print(target.shape)

            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)
            print(loss)
            # loss.backward()
            #

            loss.backward()
            optimizer.step()
</code></pre>
<p>IoU loss I am using as below:</p>
<p>class IoULoss(torch.nn.Module):<br>
def <strong>init</strong>(self, weight=None, size_average=True):<br>
super(IoULoss, self).<strong>init</strong>()</p>
<pre><code>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 &amp; predictions
    intersection = (inputs * targets).sum()
    total = (inputs + targets).sum()
    union = total - intersection

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

    return 1 - IoU
</code></pre>
<p>classifier = MODEL.segmodel(NUM_CLASSES).cuda()<br>
# criterion = MODEL.get_loss().cuda()<br>
criterion = IoULoss().cuda()<br>
classifier.apply(inplace_relu)</p>

ptrblck: <p><code>torch.argmax</code> is detaching the tensor, as this operation is not differentiable:</p>
<pre><code class="lang-python">seg_pred =torch.argmax(seg_pred,1)
...
loss = criterion(seg_pred, target.cuda())
</code></pre>
<p>so you would have to avoid it.</p>

ibrahim_khan: <p><a class="mention" href="/u/ptrblck">@ptrblck</a>  What should I use instead of this?</p>

ibrahim_khan: <p>After removing torch.argmax, now I got another errror. Please help me out. I will be very thankful to you.</p>
<p>Variable._execution_engine.run_backward(<br>
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).</p>

ibrahim_khan: <p>Thanks. Solved the issue.</p>