torch.as_tensor
-
torch.as_tensor(data, dtype=None, device=None) → Tensor -
Convert the data into a
torch.Tensor. If the data is already aTensorwith the samedtypeanddevice, no copy will be performed, otherwise a newTensorwill be returned with computational graph retained if dataTensorhasrequires_grad=True. Similarly, if the data is anndarrayof the correspondingdtypeand thedeviceis the cpu, no copy will be performed.- Parameters
-
-
data (array_like) – Initial data for the tensor. Can be a list, tuple, NumPy
ndarray, scalar, and other types. -
dtype (
torch.dtype, optional) – the desired data type of returned tensor. Default: ifNone, infers data type fromdata. -
device (
torch.device, optional) – the desired device of returned tensor. Default: ifNone, uses the current device for the default tensor type (seetorch.set_default_tensor_type()).devicewill be the CPU for CPU tensor types and the current CUDA device for CUDA tensor types.
-
data (array_like) – Initial data for the tensor. Can be a list, tuple, NumPy
Example:
>>> a = numpy.array([1, 2, 3]) >>> t = torch.as_tensor(a) >>> t tensor([ 1, 2, 3]) >>> t[0] = -1 >>> a array([-1, 2, 3]) >>> a = numpy.array([1, 2, 3]) >>> t = torch.as_tensor(a, device=torch.device('cuda')) >>> t tensor([ 1, 2, 3]) >>> t[0] = -1 >>> a array([1, 2, 3])
© 2019 Torch Contributors
Licensed under the 3-clause BSD License.
https://pytorch.org/docs/1.8.0/generated/torch.as_tensor.html