9951 explained code solutions for 126 technologies


python-pytorchHow do I convert a Python Torch tensor to a float?


To convert a Python Torch tensor to a float, use the item() method. This returns the value of the tensor as a standard Python number. For example:

import torch

x = torch.tensor([3.14])

x_float = x.item()

print(x_float)

# Output: 3.14

The item() method can be used on any tensor with one element, such as a scalar or a one-dimensional tensor. It will raise an error if the tensor has more than one element.

Code explanation

  1. import torch: Imports the torch module.
  2. x = torch.tensor([3.14]): Creates a tensor containing the value 3.14.
  3. x_float = x.item(): Calls the item() method on the tensor to convert it to a float.
  4. print(x_float): Prints the converted float.

Helpful links

Edit this code on GitHub