TypeError: a float is required
l have a pytorch variable : 

preds[4,4]
Out[305]: 
Variable containing:
-96.7809
[torch.cuda.FloatTensor of size 1 (GPU 0)]


l want to do the following : 

 import math
 x=preds[4,4]
 y=maths.exp(x)
 z= y / (y+1)


However when l do :

y=maths.exp(x)


l get the following error :

   math.exp(preds[4,4])
TypeError: a float is required


How can l transform a torch variable to a float in order to be able to do these operations ?

Thanks 

Indexing a Variable object doesnt convert it into a scalar. Its still a Variable object. However indexing a numpy array does. So converting the Variable object into a numpy and then indexing the way you want it should do the trick.
But there are some small pitfalls when converting a Variable to numpy.
If preds is a Variable stored in cpu memory, you can simply do this.
nparr = preds.data.numpy()
x = nparr[4, 4] 

However, if preds is in gpu memory, you will have to first transfer the Variable into cpu memory before you convert it into a numpy object, like so:
preds = preds.cpu()

and then do the same as above.
nparr = preds.data.numpy()
x = nparr[4, 4]

In both cases x is a scalar (a float in your case) and you can use it in any math operations you choose.
Edit:
yes, @mexmex is right, you could also directly index the tensor that is wrapped in the Variable to extract the scalar value at any given index.
Like so:
 x = preds.data[4, 4]

