-
Notifications
You must be signed in to change notification settings - Fork 27.4k
Converting NumPy dtype to Torch dtype when using as_tensor #40568
Copy link
Copy link
Open
Labels
module: numpyRelated to numpy support, and also numpy compatibility of our operatorsRelated to numpy support, and also numpy compatibility of our operatorstriagedThis issue has been looked at a team member, and triaged and prioritized into an appropriate moduleThis issue has been looked at a team member, and triaged and prioritized into an appropriate module
Description
🚀 Feature
Let the dtype keyword argument of torch.as_tensor be either a np.dtype or torch.dtype.
Motivation
Suppose I have two numpy arrays with different types and I want to convert one of them to a torch tensor with the type of the other array.
According to https://discuss.pytorch.org/t/converting-a-numpy-dtype-to-torch-dtype/52279/2, there's no convenient way to convert a numpy tensor to a torch tensor.
Pitch
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)Alternatives
- make this API public
- let the
dtypekeyword argument oftorch.Tensor.tobe either anp.dtypeortorch.dtype
Additional context
Similar issue: #541
cc @mruberry
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
module: numpyRelated to numpy support, and also numpy compatibility of our operatorsRelated to numpy support, and also numpy compatibility of our operatorstriagedThis issue has been looked at a team member, and triaged and prioritized into an appropriate moduleThis issue has been looked at a team member, and triaged and prioritized into an appropriate module