Re-wrapping a tensor in another tensor as well as calling item() on it will detach it from the computation graph:
prob = torch.tensor([item.item() for sublist in prob for item in sublist],dtype = torch.float32)

and the subsequent backward() call would raise an issue.
However, since you’ve also assigned loss.requires_grad = True, the error is masked.
I’m unsure what shape prob has before and after, but you would need to use e.g. tensor.view in case you need to change its shape.