New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[feature request] Make from_numpy method externally visible #8611
Comments
I think this is an easy fix indeed, but I wonder if |
Yeah I think we are deprecating |
Wouldn't that be a breaking change? I don't quite understand the need to deprecate |
@varunagrawal Eventually, yes. But we won't suddenly do it. It will be a deprecation warning for a couple versions before we drop it. |
I don't remember the exact reasoning for deprecating it though. Maybe @fmassa knows? |
All the functionality of |
Summary: In the warning box on https://pytorch.org/docs/stable/tensors.html#torch.Tensor.new_tensor it says: > new_tensor() always copies data. [...] If you have a numpy array and want to avoid a copy, use **torch.from_numpy()**. But then further up the page we have another warning box with the message: > torch.tensor() always copies data. [...] If you have a numpy array and want to avoid a copy, use **torch.as_tensor()**. Now I believe this is just a small oversight, since from_numpy is to be deprecated in favour of as_tensor. See for example #6885 and #8611. I suggest to just use **torch.as_tensor()** in both of the warning boxes. cc gchanan Pull Request resolved: #16587 Differential Revision: D13897038 Pulled By: gchanan fbshipit-source-id: 2eb3cd47d2c0b5bf4350f980de3be9fe59b4a846
Instead of a numpy array, I have a cupy array (cupy is a gpu enabled drop-in replacement for numpy). I tried both from_numpy and as_tensor and get an error -
from_numpy error:
as_tensor error:
|
I fixed it --> When working with #cupy and #pytorch, you might have to do a couple things diff to turn a #numpy / cupy array into a tensor. First you need to copy the numpy (cupy )array, this will allocate new memory for numpy array . This is to prevent negative strides within Ndarray which are the number of locations in memory between beginnings of successive array elements. Next, we flip it with the Axis in the array, which tells which entries are reversed. Lastly, we can finally convert the copied array to a tensor.
|
For a near 100% of computations on GPU, use (optimized version):
|
Issue description
Currently, most Python linters complain that
torch
has no methodfrom_numpy
. Since converting from a numpy array to a Pytorch tensor is pretty common, it would be nice to addfrom_numpy
to the__all__
list in thetorch
module to remedy this.I believe this to be an easy fix and not break anything, but rather make production code linters work better with Pytorch.
Code example
System Info
PyTorch version: 0.4.0
Is debug build: No
CUDA used to build PyTorch: 8.0.61
OS: Ubuntu 16.04.4 LTS
GCC version: (Ubuntu 5.4.0-6ubuntu1~16.04.9) 5.4.0 20160609
CMake version: version 3.5.1
Python version: 3.6
Is CUDA available: Yes
CUDA runtime version: 8.0.61
GPU models and configuration: GPU 0: Quadro P5000
Nvidia driver version: 384.111
cuDNN version: Probably one of the following:
/usr/lib/x86_64-linux-gnu/libcudnn.so.5.1.10
/usr/lib/x86_64-linux-gnu/libcudnn.so.6.0.21
/usr/lib/x86_64-linux-gnu/libcudnn_static.a
Versions of relevant libraries:
[pip3] msgpack-numpy (0.4.1)
[pip3] numpy (1.14.2)
[pip3] tensorboard-pytorch (0.7.1)
[pip3] torch (0.4.0)
[pip3] torchfile (0.1.0)
[pip3] torchvision (0.2.1)
[conda] tensorboard-pytorch 0.7.1
[conda] torch 0.4.0
[conda] torchfile 0.1.0
[conda] torchvision 0.2.1
The text was updated successfully, but these errors were encountered: