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
🌐
GitHub
github.com › pytorch › pytorch › issues › 40568
Converting NumPy dtype to Torch dtype when using `as_tensor` · Issue #40568 · pytorch/pytorch
June 25, 2020 - import numpy as np import torch # currently raises the following: # TypeError: as_tensor(): argument 'dtype' must be # torch.dtype, not numpy.dtype mytensor = torch.tensor(48., dtype=np.float32)
Author   sumanthratna
🌐
NumPy
numpy.org › doc › stable › user › basics.interoperability.html
Interoperability with NumPy — NumPy v2.4 Manual
Dask is lazily evaluated, and the result from a computation isn’t computed until you ask for it by invoking compute(). See the Dask array documentation and the scope of Dask arrays interoperability with NumPy arrays for details. Several Python data science libraries implement the __dlpack__ protocol. Among them are PyTorch and CuPy. A full list of libraries that implement this protocol can be found on this page of DLPack documentation. ... >>> import torch >>> x_torch = torch.arange(5) >>> x_torch tensor([0, 1, 2, 3, 4]) >>> x_np = np.from_dlpack(x_torch) >>> x_np array([0, 1, 2, 3, 4]) >>> # note that x_np is a view of x_torch >>> x_torch[1] = 100 >>> x_torch tensor([ 0, 100, 2, 3, 4]) >>> x_np array([ 0, 100, 2, 3, 4])
🌐
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....
🌐
PyTorch
docs.pytorch.org › reference api › torch.as_tensor
torch.as_tensor — PyTorch 2.11 documentation
January 1, 2023 - If data is a NumPy array (an ndarray) with the same dtype and device then a tensor is constructed using torch.from_numpy().
🌐
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 - By default, the from_numpy() method retains the original dtype. If you want explicit casting, you can use the functions like .float(), .double(), or dtype= to control tensor types. import torch import numpy as np numpy_array = np.array([1, 2, ...
🌐
APXML
apxml.com › courses › advanced-pytorch › chapter-6-custom-extensions-interoperability › pytorch-numpy-interfacing
Interfacing PyTorch Tensors with NumPy Arrays
To create a PyTorch tensor from a NumPy array, the primary function is torch.from_numpy(). # Create a NumPy array numpy_array_orig = np.array([[1.5, 2.5], [3.5, 4.5]], dtype=np.float32) print(f"\nOriginal NumPy Array:\n{numpy_array_orig}") # Convert to PyTorch tensor pytorch_tensor = torch.from_numpy(numpy_array_orig) print(f"Converted PyTorch Tensor:\n{pytorch_tensor}") print(f"PyTorch Tensor Type: {pytorch_tensor.dtype}")
Find elsewhere
🌐
GitHub
github.com › pytorch › pytorch › issues › 541
Inferring NumPy array type when using `from_numpy` · Issue #541 · pytorch/pytorch
January 22, 2017 - Hi, I love the PyTorch library so far, but I wanted to note one little annoyance that I encountered -- something that could be improved in future. For instance, when I convert numpy arrays to torch tensors, it disregards the original NumPy type and creates torch.DoubleTensor /torch.cuda.DoubleTensors.
Author   rasbt
🌐
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 - 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: tensor_a: tensor([5, 7, 1, 2, 4, 4], dtype=torch.int32) tensor_b: tensor([5., 7., 1., 2., 4., 4.]) tensor_c: tensor([5, 7, 1, 2, 4, 4], dtype=torch.int32)
🌐
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 - The easiest and most common way to convert a NumPy array into a tensor is by using torch.from_numpy().
🌐
GitHub
github.com › pytorch › pytorch › issues › 22615
[Bug] torch.from_numpy fails to convert np.int32 when np.dtype.num=7 · Issue #22615 · pytorch/pytorch
July 9, 2019 - I was looking through the members/properties of the dtype in a debugger, and I notice np.dtype.num changed from 7 to 5 after the modulus: b.dtype , b.dtype.num · (dtype('int32'), 5) a.dtype, a.dtype.num · (dtype('int32'), 7) I can work around it by using b.astype(int) and this works fine, but it's just confusing that the error message in torch complains it's not an int32 when it is. windows10 Anaconda python 3.6 env torch 1.1.0 (installed via conda) numpy 1.16.2+mkl ·
Author   llucid-97
🌐
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 Doub…
🌐
GitHub
github.com › pytorch › pytorch › issues › 85606
Creating NumPy array with `dtype=object` of PyTorch tensors fails · Issue #85606 · pytorch/pytorch
September 25, 2022 - This way, we can create a NumPy array that stores references to CPU and CUDA Tensors, such as: >>> tensors = (torch.randn(2,2, device="cuda"), torch.randn(1,3,device="cpu")) >>> np.array(tensors, dtype=object) array([tensor([[ 1.9628, -0.2343], [ 0.1929, -1.1450]], device='cuda:0'), tensor([[ 0.7147, -1.3656, 0.8823]])], dtype=object)
Author   thomasbbrunner
🌐
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 - import torch import numpy as np x = np.eye(3) torch.from_numpy(x) # Expected result # tensor([[1., 0., 0.], # [0., 1., 0.], # [0., 0., 1.]], dtype=torch.float64)
🌐
Learning About Electronics
learningaboutelectronics.com › Articles › How-to-convert-a-numpy-array-to-a-PyTorch-tensor-Python.php
How to Convert a Numpy Array to a PyTorch Tensor in Python
So, in numpy, we can create arrays and PyTorch has a built-in function, torch.from_numpy(), which allows to convert numpy arrays into PyTorch tensors.
🌐
GeeksforGeeks
geeksforgeeks.org › python-pytorch-from_numpy
Python PyTorch from_numpy() | GeeksforGeeks
April 22, 2020 - # Importing the PyTorch library import torch import numpy # A numpy array of size 6 a = numpy.array([1.0, -0.5, 3.4, -2.1, 0.0, -6.5]) print(a) # Applying the from_numpy function and # storing the resulting tensor in 't' t = torch.from_numpy(a) print(t) Output: [ 1. -0.5 3.4 -2.1 0. -6.5] tensor([ 1.0000, -0.5000, 3.4000, -2.1000, 0.0000, -6.5000], dtype=torch.float64) Modifications to the tensor will be reflected in the ndarray and vice versa.
🌐
Abtinmy
abtinmy.github.io › CS-SBU-NeuralNetwork › lectures › pytorch › intro_to_pytorch
Intro to Pytorch | Neural Networks
arr = np.array([1,2,3,4,5]) print(arr) print(arr.dtype) print(type(arr)) [1 2 3 4 5] int64 <class 'numpy.ndarray'> x = torch.from_numpy(arr) # Equivalent to x = torch.as_tensor(arr) print(x) tensor([1, 2, 3, 4, 5]) # Print the type of data held by the tensor print(x.dtype) torch.int64 ·