diagonal_scatter

paddle. diagonal_scatter ( x: Tensor, y: Tensor, offset: int = 0, axis1: int = 0, axis2: int = 1, name: str | None = None ) Tensor [source]

Embed the values of Tensor y into Tensor x along the diagonal elements of Tensor x, with respect to axis1 and axis2.

This function returns a tensor with fresh storage.

The argument offset controls which diagonal to consider:

  • If offset = 0, it is the main diagonal.

  • If offset > 0, it is above the main diagonal.

  • If offset < 0, it is below the main diagonal.

Note

y should have the same shape as paddle.diagonal.

The image below demonstrates the example: A 2D tensor with a shape of [2, 3] is diagonal_scatter along its main diagonal (offset = 0) within axis1 = 0 and axis2 = 1 using a 1D tensor filled with ones.

legend of diagonal_scatter API
Parameters
  • x (Tensor) – x is the original Tensor. Must be at least 2-dimensional.

  • y (Tensor) – y is the Tensor to embed into x

  • offset (int, optional) – which diagonal to consider. Default: 0 (main diagonal).

  • axis1 (int, optional) – first axis with respect to which to take diagonal. Default: 0.

  • axis2 (int, optional) – second axis with respect to which to take diagonal. Default: 1.

  • name (str|None, optional) – Name for the operation (optional, default is None). For more information, please refer to Name.

Returns

Tensor, Tensor with diagonal embedded with y.

Examples

>>> import paddle
>>> x = paddle.arange(6.0).reshape((2, 3))
>>> y = paddle.ones((2,))
>>> out = x.diagonal_scatter(y)
>>> print(out)
Tensor(shape=[2, 3], dtype=float32, place=Place(gpu:0), stop_gradient=True,
       [[1., 1., 2.],
        [3., 1., 5.]])