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
yinto Tensorxalong the diagonal elements of Tensorx, with respect toaxis1andaxis2.This function returns a tensor with fresh storage.
The argument
offsetcontrols 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
yshould have the same shape as paddle.diagonal.The image below demonstrates the example: A 2D tensor with a shape of [2, 3] is
diagonal_scatteralong its main diagonal (offset = 0) withinaxis1 = 0andaxis2 = 1using a 1D tensor filled with ones.
- Parameters
-
x (Tensor) –
xis the original Tensor. Must be at least 2-dimensional.y (Tensor) –
yis the Tensor to embed intoxoffset (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 api_guide_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.]])
