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)
.There are three ways to call to:to(dtype, blocking=True)
to(device, dtype=None, blocking=True)
to(other, blocking=True)
- Notes:
-
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.
- 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])