clone

paddle. clone ( x, name=None ) [source]

Returns a copy of input Tensor. It will always have a Tensor copy.

In addition, This function is derivable, so gradients will flow back from the output to input.

Parameters
  • x (Tensor) – The input Tensor.

  • name (str, optional) – For details, please refer to Name. Generally, no setting is required. Default: None.

Returns

Tensor, A Tensor copied from input.

Examples

>>> import paddle
>>> import numpy as np

>>> x = paddle.ones([2])
>>> x.stop_gradient = False
>>> x.retain_grads()
>>> clone_x = paddle.clone(x)
>>> clone_x.retain_grads()

>>> y = clone_x**3
>>> y.backward()
>>> print(clone_x.grad.numpy())
[3. 3.]
>>> print(x.grad.numpy())
[3. 3.]