from_numpy() automatically inherits input array dtype. On the other hand, torch.Tensor is an alias for torch.FloatTensor.

Therefore, if you pass int64 array to torch.Tensor, output tensor is float tensor and they wouldn't share the storage. torch.from_numpy gives you torch.LongTensor as expected.

a = np.arange(10)
ft = torch.Tensor(a)  # same as torch.FloatTensor
it = torch.from_numpy(a)

a.dtype  # == dtype('int64')
ft.dtype  # == torch.float32
it.dtype  # == torch.int64
Answer from Viacheslav Kroilov on Stack Overflow
🌐
PyTorch Forums
discuss.pytorch.org › t › issue-with-conversion-from-numpy › 6345
Issue with conversion from Numpy - PyTorch Forums
August 17, 2017 - I created a very simple autoencoder and have been looking to feed in data that I have converted from Numpy. I found that when I convert from numpy “float” array using torch.from_numpy it gives me an array of torch DoubleTensor. When I feed this into my model it causes problems with the linear layer, giving the message: torch.addmm received an invalid combination of arguments… when I convert the torch double tensor to type float this error goes away.
Discussions

Error when converting np.float32 array to torch.cuda.FloatTensor
Hi, for some reason it fails, only float64 can be converted. torch.cuda.FloatTensor(np.random.rand(10,2).astype(np.float32)) gives RuntimeError: tried to construct a tensor from a nested float sequence, but found an item of type numpy.fl... More on github.com
🌐 github.com
10
July 30, 2017
python - Is torch.float32 different from numpy's float32? - Stack Overflow
>>> np.set_printoptions(precision=30) ... -0.8809434 , -1.3528217 ]], dtype=float32)e ... By default, if it takes less digits than the configured value of precision to distinguish a floating-point value from other values of the same dtype, NumPy will only print as many digits as necessary ... More on stackoverflow.com
🌐 stackoverflow.com
Why PyTorch tensors gives preference to float32 element datatype
GPUs are optimized for fp32. More on reddit.com
🌐 r/pytorch
10
7
August 13, 2020
EulerDiscreteScheduler.set_timesteps() torch.from_numpy misuse error.
Describe the bug In version 0.24.0, on line 283 of schedulers\scheduling_euler_discrete.py diffusers/src/diffusers/schedulers/scheduling_euler_discrete.py Line 283 in 76c645d sigmas = torch.from_nu... More on github.com
🌐 github.com
11
December 5, 2023
🌐
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...
Author   pytorch
🌐
Sprintchase
sprintchase.com › home › torch.from_numpy(): converting numpy array to pytorch tensor
torch.from_numpy(): Converting Numpy Array to PyTorch Tensor
May 14, 2025 - import torch import numpy as np numpy_array = np.array([1, 2, 3]) print(numpy_array) # Output: [1 2 3] print(numpy_array.dtype) # Output: int64 # Converting to float32 tensor tensor = torch.from_numpy(numpy_array).float() print(tensor) # Output: tensor([1., 2., 3.]) print(tensor.dtype) # Output: torch.float32 Let’s convert the output tensor to dtype=int16.
🌐
GitHub
github.com › pytorch › pytorch › issues › 2246
Error when converting np.float32 array to torch.cuda.FloatTensor · Issue #2246 · pytorch/pytorch
July 30, 2017 - torch.cuda.FloatTensor(np.random.rand(10,2).astype(np.float32)) ... RuntimeError: tried to construct a tensor from a nested float sequence, but found an item of type numpy.float32 at index (0, 0)
Author   pytorch
🌐
GitHub
gist.github.com › xvdp › 149e8c7f532ffb58f29344e5d2a1bee0
numpy uint8 to pytorch float32; how to do it efficiently · GitHub
numpy uint8 to pytorch float32; how to do it efficiently · Raw · npuint8_torchfloat32.py · This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
🌐
Data Science Weekly
datascienceweekly.org › tutorials › pytorch-numpy-to-tensor-convert-a-numpy-array-to-a-pytorch-tensor
PyTorch NumPy to tensor: Convert A NumPy Array To A PyTorch Tensor
... What we want to do is use PyTorch ... To do that, we're going to define a variable torch_ex_float_tensor and use the PyTorch from NumPy functionality and pass in our variable numpy_ex_array....
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-pytorch-from_numpy
Python PyTorch from_numpy() - GeeksforGeeks
April 22, 2020 - The function torch.from_numpy() provides support for the conversion of a numpy array into a tensor in PyTorch. It expects the input as a numpy array (numpy.ndarray). The output type is tensor.
Find elsewhere
🌐
Snyk
snyk.io › advisor › torch › functions › torch.from_numpy
How to use the torch.from_numpy function in torch | Snyk
def __getitem_dev(self, index): series, bounds = self.imgs[index] (zs, ze), (ys, ye), (xs, xe) = bounds target = load_label(self.targets, series) target = target[zs:ze, ys:ye, xs:xe] target = torch.from_numpy(target.astype(np.int64)) image = load_image(self.images, series) image = image[0, zs:ze, ys:ye, xs:xe] # optionally mask out lung volume from image if self.masks is not None: mask = load_mask(self.masks, series) mask = mask[zs:ze, ys:ye, xs:xe] image -= MIN_BOUND image = np.multiply(mask, image) image += MIN_BOUND image = image.reshape((1, ze-zs, ye-ys, xe-xs)) img = image.astype(np.float32) if self.transform is not None: img = self.transform(img) if self.target_transform is not None: target = self.target_transform(target) lzrobots / LearningToCompare_ZSL / CUB_RN.py View on Github ·
🌐
Sparrow Computing
sparrow.dev › home › blog › pytorch tensor to numpy array and back
PyTorch Tensor to NumPy Array and Back - Sparrow Computing
October 14, 2021 - x = np.eye(3) torch.from_numpy(x).type(torch.float32) # Expected result # tensor([[1, 0, 0], # [0, 1, 0], # [0, 0, 1]])
🌐
Medium
medium.com › @heyamit10 › converting-a-numpy-array-to-a-tensor-step-by-step-df329c44b035
Converting a NumPy Array to a Tensor (Step-by-Step) | by Hey Amit | Medium
February 8, 2025 - Here’s where things can get a little tricky. By default, torch.from_numpy() retains the data type of the NumPy array. If the array has integers, the resulting tensor will too. But what if your model expects float32 values?
🌐
Stack Abuse
stackabuse.com › numpy-array-to-tensor-and-tensor-to-numpy-array-with-pytorch
Convert Numpy Array to Tensor and Tensor to Numpy Array with PyTorch
May 22, 2023 - The from_numpy() and tensor() functions are dtype-aware! Since we've created a Numpy array of integers, the dtype of the underlying elements will naturally be int32: ... tensor_a and tensor_c retain the data type used within the np_array, cast into PyTorch's variant (torch.int32), while tensor_b automatically assigns the values to floats:
🌐
PyTorch
docs.pytorch.org › python api › torch.from_numpy
torch.from_numpy — PyTorch 2.8 documentation
torch.from_numpy(ndarray) → Tensor# Creates a Tensor from a numpy.ndarray. The returned tensor and ndarray share the same memory. Modifications to the tensor will be reflected in the ndarray and vice versa.
🌐
GitHub
github.com › huggingface › diffusers › issues › 6054
EulerDiscreteScheduler.set_timesteps() torch.from_numpy misuse error. · Issue #6054 · huggingface/diffusers
December 5, 2023 - sigmas = torch.from_numpy(sigmas).to(dtype=torch.float32, device=device) An exception occurs when sigmas is a Tensor object and not a numpy array, when self.config.interpolation_type == "log_linear" from this setup code directly above ·
Author   huggingface
🌐
GeeksforGeeks
geeksforgeeks.org › how-to-fix-cant-assign-a-numpyndarray-to-a-torchfloattensor
How to Fix "Can't Assign a numpy.ndarray to a torch.FloatTensor"? - GeeksforGeeks
July 3, 2024 - The error "Can't assign a numpy.ndarray to a torch.FloatTensor" occurs when you attempt to assign a NumPy array to a PyTorch tensor directly, which is not allowed. PyTorch and NumPy are different libraries with different data structures, so ...
🌐
DEV Community
dev.to › hyperkai › device-conversion-with-to-fromnumpy-and-numpy-in-pytorch-4j22
Device conversion with to() and from_numpy() and numpy() in PyTorch - DEV Community
November 5, 2024 - The type of a PyTorch tensor is also inherited to a NumPy array. import torch my_tensor = torch.tensor([0., 1., 2.]) my_tensor.dtype # torch.float32 my_array = my_tensor.numpy() my_array # array([0., 1., 2.], dtype=float32) my_tensor = torch.tensor([0., 1., 2.], device='cuda:0') my_tensor.numpy(force=True) # array([0., 1., 2.], dtype=float32) my_tensor = torch.tensor([0., 1., 2.], device='cuda:0') my_tensor.numpy() my_tensor.numpy(force=False) # Error ·
🌐
Medium
medium.com › @ravina.lad01 › pytorch-fundamentals-075178c815eb
Everything You Need to Know About Tensors in PyTorch | by Ravina Lad | Medium
July 29, 2024 - # NumPy array to tensor import torch import numpy as np array = np.arange(1.0, 8.0) tensor = torch.from_numpy(array) array, tensor # Tensor to NumPy array tensor = torch.ones(7) # create a tensor of ones with dtype=float32 numpy_tensor = tensor.numpy() # will be dtype=float32 unless changed tensor, numpy_tensor ·