This error occurs because you are trying to call the numpy() method on a tensor that still has gradients attached to it. Since the gradients are needed for backpropagation during training, PyTorch prevents you from converting the tensor to a numpy array.
To fix this issue, you can detach the tensor from the computation graph using the detach() method before converting it to a numpy array. Here’s an example:
loss = dpl.my_KLDivLoss(out, y)
loss_value = loss.detach().numpy()
Alternatively, if you don’t need to compute gradients for this specific tensor, you can use the with torch.no_grad() context manager to temporarily disable gradient computation:
with torch.no_grad():
loss = dpl.my_KLDivLoss(out, y).numpy()