block_diag

paddle. block_diag ( inputs: Sequence[Tensor], name: str | None = None ) → Tensor [source]

This API has two signatures:

  1. paddle.block_diag(inputs, name=None) (Paddle-style):

    Create a block diagonal matrix from a sequence of tensors.

  2. paddle.block_diag(*tensors) (PyTorch-style):

    Create a block diagonal matrix from variadic tensor arguments.

Parameters
  • inputs (list|tuple) – Sequence of input tensors, one or more tensors with 0, 1, or 2 dimensions. The data type: bool, float16, float32, float64, uint8, int8, int16, int32, int64, bfloat16, complex64, complex128.

  • name (str|None, optional) – Name for the operation (optional, default is None).

Returns

Tensor, A Tensor. The data type is same as inputs.

Examples

>>> import paddle

>>> A = paddle.to_tensor([[4], [3], [2]])
>>> B = paddle.to_tensor([7, 6, 5])
>>> C = paddle.to_tensor(1)
>>> D = paddle.to_tensor([[5, 4, 3], [2, 1, 0]])
>>> E = paddle.to_tensor([[8, 7], [7, 8]])
>>> out = paddle.block_diag([A, B, C, D, E])
>>> print(out)
Tensor(shape=[9, 10], dtype=int64, place=Place(cpu), stop_gradient=True,
[[4, 0, 0, 0, 0, 0, 0, 0, 0, 0],
 [3, 0, 0, 0, 0, 0, 0, 0, 0, 0],
 [2, 0, 0, 0, 0, 0, 0, 0, 0, 0],
 [0, 7, 6, 5, 0, 0, 0, 0, 0, 0],
 [0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
 [0, 0, 0, 0, 0, 5, 4, 3, 0, 0],
 [0, 0, 0, 0, 0, 2, 1, 0, 0, 0],
 [0, 0, 0, 0, 0, 0, 0, 0, 8, 7],
 [0, 0, 0, 0, 0, 0, 0, 0, 7, 8]])