to
- paddle.Tensor. to ( self: Tensor, *args, **kwargs )
-
Performs Tensor dtype and/or device conversion. A paddle.dtype and place are inferred from the arguments of
self.to(*args, **kwargs).This API has three calling conventions:
-
-
to(device=None, dtype=None, blocking=True, copy=False, *, non_blocking=False): -
Moves and/or casts the Tensor.
-
-
-
to(dtype, blocking=True, copy=False, *, non_blocking=False): -
Equivalent to
self.to(device=None, dtype=dtype, ...).
-
-
-
to(other, blocking=True, copy=False, *, non_blocking=False): -
Equivalent to
self.to(device=other.place, dtype=other.dtype, ...).
-
Note
If the self Tensor already has the correct dtype and device, then self is returned. Otherwise, the returned tensor is a copy of self with the desired dtype and device.
- Parameters
-
device (str|paddle.CPUPlace()|paddle.CUDAPlace()|paddle.CUDAPinnedPlace()|paddle.XPUPlace()|None, optional) – The device to move to. Default:
None.dtype (str|numpy.dtype|paddle.dtype|None, optional) – The desired data type. Default:
None.blocking (bool, optional) – If
Falseand the source is in pinned memory, the copy will be asynchronous with respect to the host. Default:True.copy (bool, optional) – If
True, a new Tensor is created even when the Tensor already matches the desired conversion. Default:False.
- Keyword Arguments
-
non_blocking (bool, optional) – If
Trueand the source is in pinned memory, the copy will be asynchronous with respect to the host. Default:False.non_blockingandblockingare mutually exclusive and cannot both be set at the same time. - Returns
-
self
- Return type
-
Tensor
Examples
>>> >>> import paddle >>> x = paddle.to_tensor([1, 2, 3]) >>> print(x) Tensor(shape=[3], dtype=int64, place=Place(gpu:0), stop_gradient=True, [1, 2, 3]) >>> x = x.to("cpu") >>> print(x.place) Place(cpu) >>> x = x.to("float32") >>> print(x.dtype) paddle.float32 >>> x = x.to("gpu", "int16") >>> print(x) Tensor(shape=[3], dtype=int16, place=Place(gpu:0), stop_gradient=True, [1, 2, 3]) >>> y = paddle.to_tensor([4, 5, 6]) >>> y Tensor(shape=[3], dtype=int64, place=Place(gpu:0), stop_gradient=True, [4, 5, 6]) >>> y = y.to(x) >>> print(y) Tensor(shape=[3], dtype=int16, place=Place(gpu:0), stop_gradient=True, [4, 5, 6])
-
