Загрузка
Converts obj to a tensor.
obj can be one of:
a tensor
a NumPy array or a NumPy scalar
a DLPack capsule
an object that implements Python’s buffer protocol
a scalar
a sequence of scalars
When obj is a tensor, NumPy array, or DLPack capsule the returned tensor will,
by default, have the same requires_grad as obj (defaulting to False), have the
same datatype, be on the same device, and share memory with it. These properties can be
controlled with the dtype, device, copy, and
requires_grad keyword arguments. If the returned tensor is of a different
datatype, on a different device, or a copy is requested then it will not share its
memory with obj. If requires_grad is True (or None, and
obj was a tensor with requires_grad set), then the returned tensor will require
a gradient, and if obj is also a tensor with an autograd history then the
returned tensor will have the same history.
When obj is not a tensor, NumPy array, or DLPack capsule but implements Python’s
buffer protocol then the buffer is interpreted as an array of bytes grouped according to
the size of the datatype passed to the dtype keyword argument. (If no datatype is
passed then the default floating point datatype is used, instead.) The returned tensor
will have the specified datatype (or default floating point datatype if none is specified)
and, by default, be on the CPU device and share memory with the buffer.
When obj is a NumPy scalar, the returned tensor will be a 0-dimensional tensor on
the CPU and that doesn’t share its memory (i.e. copy=True). By default datatype will
be the PyTorch datatype corresponding to the NumPy’s scalar’s datatype.
When obj is none of the above but a scalar, or a sequence of scalars then the
returned tensor will, by default, infer its datatype from the scalar values, be on the
current default device, and not share its memory.
obj (object) – a tensor, NumPy array, DLPack Capsule, object that implements Python’s buffer protocol, scalar, or sequence of scalars.
dtype (torch.dtype, optional) – the datatype of the returned tensor.
Default: None, which causes the datatype of the returned tensor to be
inferred from obj.
copy (bool, optional) – controls whether the returned tensor shares memory with .
Default: , which causes the returned tensor to share memory with
whenever possible. If then the returned tensor does not share its memory.
If then the returned tensor shares its memory with and an
error is thrown if it cannot.
Example:
>>> a = torch.tensor([1, 2, 3])
>>> # Shares memory with tensor 'a'
>>> b = torch.asarray(a)
>>> a.data_ptr() == b.data_ptr()
True
>>>
NoneobjTrueFalseobjdevice (torch.device, optional) – the device of the returned tensor.
Default: None, which causes the device of obj to be used. Or, if
obj is a Python sequence, the current default device will be used.
requires_grad (bool, optional) – whether the returned tensor requires grad.
Default: None, which causes requires_grad for the returned tensor to be
inferred from obj. If True, then the returned tensor will require
a gradient, and if obj is also a tensor with an autograd history then
the returned tensor will have the same history.