Fastest way to convert tensor to float?

What’s the shortest way to convert a tensor with a single element to a float?

Right now I’m doing:

x_inp.min().cpu().detach().numpy().tolist()

which works, but it’s a lot. If it doesn’t already exist, it might be nice to have something like:

x_inp.to_python_number()

which would throw an error if x_inp doesn’t have just a single element.

Hi,

If your tensor contains a single element, you can do x_inp.item().

19 Likes