block_diag

paddle. block_diag ( inputs, name=None ) [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.

  • name (str, 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]])