apply

paddle.Tensor. apply ( self, func )

Apply the python function to the tensor.

Returns

None

Examples

>>> import paddle

>>> x = paddle.to_tensor([[0.3, 0.5, 0.1],
>>>        [0.9, 0.9, 0.7],
>>>        [0.4, 0.8, 0.2]]).to("cpu", "float64")
>>> f = lambda x: 3*x+2
>>> y = x.apply(f)
>>> print(y)
Tensor(shape=[3, 3], dtype=float64, place=Place(cpu), stop_gradient=True,
       [[2.90000004, 3.50000000, 2.30000000],
        [4.69999993, 4.69999993, 4.09999996],
        [3.20000002, 4.40000004, 2.60000001]])


>>> x = paddle.to_tensor([[0.3, 0.5, 0.1],
>>>        [0.9, 0.9, 0.7],
>>>        [0.4, 0.8, 0.2]]).to("cpu", "float16")
>>> y = x.apply(f)


>>> x = paddle.to_tensor([[0.3, 0.5, 0.1],
>>>        [0.9, 0.9, 0.7],
>>>        [0.4, 0.8, 0.2]]).to("cpu", "bfloat16")
>>> y = x.apply(f)


>>> if paddle.is_compiled_with_cuda():
>>>     x = paddle.to_tensor([[0.3, 0.5, 0.1],
>>>        [0.9, 0.9, 0.7],
>>>        [0.4, 0.8, 0.2]]).to("gpu", "float32")
>>>     y = x.apply(f)