The Python math module doesn’t work on torch objects. You could get the Python float out of your variable with x.data[0]. However you probably just want to use torch.exp instead of math.exp (torch.exp works on Tensors and Variables). You could also just use x.exp() for the same result, if you prefer that syntax. Generally you want to find the PyTorch function for any mathematical operations you want to perform.

I think it is because the data is in form of Tensor. Meanwhile you doing processing with python math library. My solution is you must convert it to numpy first or you just using Tensor operation in pytorch Tensor Operation . If you want to just calculate using python function you can cast it as numpy array. It is so simple, you can just doing preds.data.numpy() . I also found that your tensor is in GPU so don’t forget to copy it to GPU, so you can use preds.data.cpu().numpy(). Here is the example in figure for clearer.