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
pytorch - How to convert numpy array(float data) to torch tensor? - Stack Overflow
@Dwa I don't think this is the ... convert from NumPy to PyTorch. However, OP is concerned with - what seems to be - a lack of precision after conversion. ... The data precision is the same, it's just that the format used by PyTorch to print the values is different, it will round the floats down: >>> test_torch = ... More on stackoverflow.com
🌐 stackoverflow.com
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
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   mruberry
🌐
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   DmitryUlyanov
🌐
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.
🌐
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 ·
🌐
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....
Find elsewhere
🌐
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.
🌐
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.
🌐
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:
🌐
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?
🌐
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   Teriks
🌐
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 - Traceback (most recent call last): File "", line 1, in TypeError: can't convert np.ndarray of type numpy.int32. The only supported types are: float64, float32, float16, int64, int32, int16, int8, and uint8. 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: ... 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.
Author   llucid-97
🌐
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 ...