-
Notifications
You must be signed in to change notification settings - Fork 7.5k
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
Add support for kwargs
to tensor()
when arg is an ndarray
#3797
Conversation
As discussed in the comments of #3781, _array2tensor() should be able to create a tensor from an ndarray using kwargs and this commit adds that feature.
Check out this pull request on See visual diffs & provide feedback on Jupyter Notebooks. Powered by ReviewNB |
I just found out that you have you to build the notebooks too (I should read the nbdev stuff in more detail). So I'll open this once I have done that. |
That's a clever approach. Would this work and be a bit cleaner?: def _array2tensor(x, requires_grad=False, pin_memory=False, **kwargs):
if x.dtype==np.uint16: x = x.astype(np.float32)
# windows default numpy int dtype is int32, while torch tensor default int dtype is int64
# https://github.com/numpy/numpy/issues/9464
if sys.platform == "win32" and x.dtype==np.int: x = x.astype(np.int64)
t = torch.as_tensor(x, requires_grad=requires_grad, pin_memory=pin_memory, **kwargs)
t.requires_grad_(requires_grad)
if pin_memory: t.pin_memory()
return t |
@jph00 yeah your approach is far cleaner but your code doesn't work because Just changed this from your code:
to this:
|
kwargs
to tensor()
when arg is an ndarray
As discussed in the comments of #3781,
You could not create a tensor using an ndarray with named arguments. The aforementioned function now accepts **kwargs and creates a tensor using torch.as_tensor() which shares memory with the ndarray.
In the following code snippet:
Previous behaviour:
Expected:
Output:
After commit:
Expected:
Output: