Machine learning engineers and software developers have to pay attention to details. When you create a PyTorch binary classifier, for example to predict the sex (male = 0, female = 1) of a person based on their age, income, and so on, somewhat unusually the target data labels must be read as type torch.float32 rather than type torch.int64. Later, when using the target data to compute accuracy or act as an index into an array, it’s useful or necessary to convert the float32 targets to type int64.
OK, no problem. I can never remember the exact syntax for PyTorch tensor type conversion so I Google for it — and see several different ways to convert float32 to int64. There are at least four ways to convert: type(), long(), to(), and tensor(). Just to torture myself, I put together a demo. Here is the key code without the print() statements:
# type_conversions.py
import numpy as np
import torch as T
device = T.device('cpu')
y_data = T.tensor(np.array([1.0]), dtype=T.float32).to(device)
print(y_data.dtype) # float
# 1. use type()
y_data = T.tensor(np.array([1.0]), dtype=T.float32)
y_data = y_data.type(T.int64)
# 2. use int()
y_data = T.tensor(np.array([1.0]), dtype=T.float32)
y_data = y_data.long() # note: int() is int32
# 3. use to()
y_data = T.tensor(np.array([1.0]), dtype=T.float32)
y_data = y_data.to(T.int64)
# 4. use tensor()
y_data = T.tensor(np.array([1.0]), dtype=T.float32)
y_data = T.tensor(y_data, dtype=T.int64)
# 5. use numpy() + tensor()
y_data = T.tensor(np.array([1.0]), dtype=T.float32)
y_numpy = y_data.numpy()
y_data = T.tensor(y_numpy, dtype=T.int64)
Using the tensor() constructor — technique 4 — gives a warning message, so in technique #5 I convert the source tensor to a numpy array and then feed it to the tensor() constructor to avoid the warning message.
Why are there so many ways to convert a PyTorch float32 tensor to type int64? I don’t know. I suspect that because PyTorch is an open source project, it has evolved somewhat chaotically, as opposed to a project with a single guiding force/person. But regardless, paying attention to details like type conversions is important.

The game of chess is the epitome of paying attention to detail. Here are three memorable (to me) chess sets. Left: A gothic design from the early 1960s by famous artist Peter Ganine (1900-1974). I learned to play chess on this set. It belonged to the family of my next door neighbors, the Curetons. Kenny Cureton, Tommy Cureton, my brother Roger, and I learned the rules from the Encyclopedia Britannica. Center: The famous (to chess players) Drueke Player’s Choice model 36. I used this set when I played on my Servite High School chess team with Bob Smith, Tom Law, Tom Quackenbush, Dan Musser, and Ed Hernandez. We won the Orange County (California) high school chess championship two years in a row. Right: This is the Classic Games Series ancient Roman set I got for Christmas one year. It was beautiful.
Demo code with print() statements:
# type_conversions.py
import numpy as np
import torch as T
device = T.device('cpu')
print("\nPyTorch tensor type float32 to int64 conversion ")
y_data = T.tensor(np.array([1.0]), dtype=T.float32).to(device)
print("\nSource tensor type: ")
print(y_data.dtype) # float
# 1. use type()
print("\nConversion using type(): ")
y_data = T.tensor(np.array([1.0]), dtype=T.float32)
y_data = y_data.type(T.int64)
print(y_data.dtype) # int64
# 2. use int()
print("\nConversion using long(): ")
y_data = T.tensor(np.array([1.0]), dtype=T.float32)
y_data = y_data.long() # note: int() is int32
print(y_data.dtype) # int64
# 3. use to()
print("\nConversion using to(): ")
y_data = T.tensor(np.array([1.0]), dtype=T.float32)
y_data = y_data.to(T.int64)
print(y_data.dtype) # int64
# 4. use tensor()
print("\nConversion using torch.tensor(): ")
y_data = T.tensor(np.array([1.0]), dtype=T.float32)
y_data = T.tensor(y_data, dtype=T.int64)
print(y_data.dtype) # int64
# 5. use numpy() + tensor()
print("\nConversion using numpy() + torch.tensor(): ")
y_data = T.tensor(np.array([1.0]), dtype=T.float32)
y_numpy = y_data.numpy()
y_data = T.tensor(y_numpy, dtype=T.int64)
print(y_data.dtype)
print("\nEnd demo ")

.NET Test Automation Recipes
Software Testing
SciPy Programming Succinctly
Keras Succinctly
R Programming
2026 Visual Studio Live
2025 Summer MLADS Conference
2026 DevIntersection Conference
2025 Machine Learning Week
2025 Ai4 Conference
2026 G2E Conference
2026 iSC West Conference
You must be logged in to post a comment.