block_diag

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

Create a block diagonal matrix from provided tensors.

Parameters
  • inputs (list|tuple) – inputs is a Tensor list or Tensor tuple, 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(gpu:0), 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]])