to

paddle.Tensor. to ( self, *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:

  1. to(dtype, blocking=True)

  2. to(device, dtype=None, blocking=True)

  3. to(other, blocking=True)

Returns

self

Return type

Tensor

Examples

>>> import paddle
>>> tensorx = paddle.to_tensor([1,2,3])
>>> print(tensorx)
Tensor(shape=[3], dtype=int64, place=Place(gpu:0), stop_gradient=True,
    [1, 2, 3])

>>> tensorx = tensorx.to("cpu")
>>> print(tensorx.place)
Place(cpu)

>>> tensorx = tensorx.to("float32")
>>> print(tensorx.dtype)
paddle.float32

>>> tensorx = tensorx.to("gpu", "int16")
>>> print(tensorx)
Tensor(shape=[3], dtype=int16, place=Place(gpu:0), stop_gradient=True,
    [1, 2, 3])
>>> tensor2 = paddle.to_tensor([4,5,6])
>>> tensor2
Tensor(shape=[3], dtype=int64, place=Place(gpu:0), stop_gradient=True,
    [4, 5, 6])
>>> tensor2 = tensor2.to(tensorx)
>>> print(tensor2)
Tensor(shape=[3], dtype=int16, place=Place(gpu:0), stop_gradient=True,
    [4, 5, 6])