From Torch.Tensor.item docs:

x = torch.tensor([1.0])

print((x.item())

output:

1.0

type check:

print(type(x.item())

output:

float
Answer from ImSo3K on Stack Overflow
🌐
GitHub
github.com › pytorch › pytorch › issues › 136350
Python float to torch.float32 · Issue #136350 · pytorch/pytorch
September 20, 2024 - python -c "import torch;import time;offset=1726274430;torch.set_printoptions(precision=10);l1=[time.time()-offset,time.time()-offset];print('list:', l1, type(l1[0]));t1=torch.tensor(l1);print('cpu:', t1, t1.dtype);t1=t1.to('cuda'); print('gpu:', t1, t1.dtype);" list: [521468.6812365055, 521468.6812376976] <class 'float'> cpu: tensor([521468.6875000000, 521468.6875000000]) torch.float32 gpu: tensor([521468.6875000000, 521468.6875000000], device='cuda:0') torch.float32
🌐
GitHub
github.com › pytorch › pytorch › issues › 20755
Type conversion from float64 to float32 (cpu) sometimes crashes · Issue #20755 · pytorch/pytorch
May 21, 2019 - import torch a = torch.rand(3, 3, dtype = torch.float64) print(a.dtype, a.device) # torch.float64 cpu c = a.to(torch.float32) #works b = torch.load('bug.pt') print(b.dtype, b.device) # torc
Author: pytorch
🌐
Sprintchase
sprintchase.com › home › torch.tensor.to: converting data types and/or changing device
torch.Tensor.to: Converting Data Types and/or Changing Device
May 12, 2025 - import torch # Create a tensor tensor = torch.tensor([1, 2, 3], dtype=torch.int32) # Move to GPU and convert to float32 tensor_float_gpu = tensor.to(device="cuda", dtype=torch.float32) print(tensor_float_gpu) # Output: tensor([1., 2., 3.], ...
🌐
Data Science Weekly
datascienceweekly.org › tutorials › tensor-to-float-convert-a-pytorch-tensor-to-a-floating-number-data-type
Tensor To Float: Convert a PyTorch Tensor To A Floating Number Data Type
We convert the tensor to a Float tensor using the PyTorch float() method. ... Now that the tensor has been converted to a floating point tensor, let's double check the new tensor's data type to make sure it's a float tensor ...
🌐
PyTorch Forums
discuss.pytorch.org › t › how-to-cast-a-tensor-to-a-new-type › 8929
How to cast a tensor to a new type? - PyTorch Forums
October 21, 2017 - How can I cast a tensor to the float32 type in pytorch? intersection = (torch.mul(height_inter, width_inter) I want the intersection tensor to be float32 type.
🌐
GitHub
github.com › pytorch › pytorch › issues › 27754
torch.tensor(numpy.float64()) creates a float32 tensor · Issue #27754 · pytorch/pytorch
October 11, 2019 - 🐛 Bug >>> import torch >>> import numpy >>> t = torch.tensor(numpy.float64()) >>> t.dtype torch.float32 Should be torch.float64. test_dataloader.py has test_numpy_scalars which was supposed to test for this, but that test historically ra...
🌐
PyTorch Forums
discuss.pytorch.org › t › efficiently-converting-np-uint8-to-torch-float32 › 55989
Efficiently converting np.uint8 to torch.float32 - PyTorch Forums
September 15, 2019 - I was running some data ffmpeg to torch (thru pipes) and noticed that I was doing something very naive. So I profiled, with single process, conversion between npuint to float32 The difference in CPU can be almost one order of magnitude. GPU tightens everything. If anyone is interested.
Find elsewhere
🌐
PyTorch Forums
discuss.pytorch.org › mixed-precision
Tensor in float16 is transformed into float32 after torch.norm - mixed-precision - PyTorch Forums
February 4, 2021 - I trained the mode in mixed precision. in certain function, a input tensor in float16 is transformed into float32 after torch.norm dis_vec (float16) dis (float32) and if I use dis=torch.sqrt((dis_vec**2).sum(-1)), dis is also float32
🌐
ProjectPro
projectpro.io › recipes › typecast-float-tensor-integer-tensor-and-vice-versa-pytorch
How to typecast a float tensor to integer tensor and vice versa in pytorch -
December 16, 2022 - PyTorch vs Tensorflow - Which One Should You Choose For Your Next Deep Learning Project ? ... tensor = torch.tensor([1., 3.4, 5.5]) print("This is a Sample tensor with its data type:", tensor, tensor.dtype) This is a Sample tensor: tensor([1.0000, 3.4000, 5.5000]) torch.float32
🌐
YouTube
youtube.com › watch
Tensor To Float: Convert a PyTorch Tensor To A Floating Number Data Type - YouTube
PyTorch Tensor To Float: Convert a PyTorch Tensor To A Floating Number Data Type
Published: May 5, 2023
🌐
DEV Community
dev.to › hyperkai › type-conversion-with-type-to-and-a-tensor-in-pytorch-2a0g
Type conversion with type(), to() and a tensor in PyTorch - DEV Community
November 5, 2024 - You can check long(), float(), cfloat() and bool() in PyTorch. ... import torch tensor1 = torch.tensor([7, 0, 4]) tensor1, tensor1.dtype, tensor1.dtype # (tensor([7, 0, 4]), torch.int64, torch.int64) tensor2 = tensor1.type(dtype=torch.float32) tensor2 = tensor1.type(dtype=torch.float) tensor2 = tensor1.type(dtype='torch.FloatTensor') tensor2 = tensor1.to(dtype=torch.float32) tensor2 = tensor1.to(dtype=torch.float) tensor2 = tensor1.float() tensor2, tensor2.dtype, tensor2.type() # (tensor([7., 0., 4.]), torch.float32, 'torch.FloatTensor') tensor2 = tensor1.to(dtype=float) tensor2, tensor2.dtype
🌐
PyTorch Forums
discuss.pytorch.org › t › turn-a-model-from-double-into-float › 84773
Turn a model from double into float - PyTorch Forums
June 9, 2020 - Dear all, Hi! I am recently managing to use float32 as the precision of my model. The pytorch program on my computer seems to use “double” precision by default. I find it straight forward to cast a tensor from double to…
🌐
Stack Overflow
stackoverflow.com › questions › 78625587 › fail-to-convert-float-64-to-float-32-in-python
pandas - Fail to convert float 64 to float 32 in python - Stack Overflow
I used both pandas method and pytorch but no avail. Dataset are from kaggle titanic project. The code is the following: Copyfrom torch import nn import torch import pandas as pd import numpy as np df = pd.DataFrame([[1.1, 2.1], [3.1, 4.1],[5.1,6.1]], columns=['col1', 'col2']) df.astype('float32') print(df.dtypes) df.astype(np.float32) print(df.dtypes) a=torch.tensor(df.loc[1][['col1']]) a.to(torch.float32) print(a) and get the result ·
🌐
Data Science Weekly
datascienceweekly.org › tutorials › pytorch-change-tensor-type-cast-a-pytorch-tensor-to-another-type
PyTorch Change Tensor Type: Cast A PyTorch Tensor To Another Type
So when you’re casting or converting between PyTorch tensor types, it’s always important to remember what kind of precision you are losing when you are doing this type of conversion.
🌐
Codecademy
codecademy.com › docs › pytorch › tensor operations › specifying data types
PyTorch | Tensor Operations | Specifying Data Types | Codecademy
January 13, 2025 - In the example below a tensor is created with a specified data type, another with a different type, and one tensor is converted to a new data type: import torch · # Creating a float32 tensor · float_tensor = torch.tensor([1.0, 2.0, 3.0], ...
🌐
PyTorch
docs.pytorch.org › vision › main › generated › torchvision.transforms.functional.convert_image_dtype.html
convert_image_dtype — Torchvision main documentation
torchvision.transforms.functional.convert_image_dtype(image: Tensor, dtype: dtype = torch.float32) → Tensor[source]¶ · Convert a tensor image to the given dtype and scale the values accordingly This function does not support PIL Image. Parameters: image (torch.Tensor) – Image to be converted ...
🌐
PyTorch Forums
discuss.pytorch.org › autograd
Torch acos cast to float32 - autograd - PyTorch Forums
October 11, 2022 - Hey all. I have a very weird issue that I can’t reproduce outside of my training loop so I apologize in advance for not providing a minimal reproducible example. I have a tensor in float 16 on cuda, I passed it to torch.acos in my training loop and the return is a float32 …
🌐
GitHub
github.com › pytorch › pytorch › issues › 63951
Apparent different between `.to(dtype=torch.float32)` and `.float()` for TF32 · Issue #63951 · pytorch/pytorch
August 25, 2021 - For some reason, if we convert tensor to float32 with .float(), calculations are performed with FP32 rather than TF32, even if the latter is enabled. ... import torch import time a_full = torch.randn(10240, 10240, dtype=torch.double, device='cuda') b_full = torch.randn(10240, 10240, dtype=torch.double, device='cuda') ab_full = a_full @ b_full mean = ab_full.abs().mean() # 80.7277 a = a_full.float() b = b_full.float() # The flag below controls whether to allow TF32 on matmul.
Author: pytorch