mindspore.ops.block_diag

View Source On Gitee
mindspore.ops.block_diag(*inputs)[source]

Creates a block diagonal matrix from the provided Tensor.

Parameters

inputs (Tensor) – One or more tensors, the dimension of Tensor should be 0, 1 or 2.

Returns

Tensor, two-dimensional with all input tensors arranged in order so that their top left and bottom right corners are diagonally adjacent. All other elements are set to 0.

Raises
  • TypeError – If the input is not a Tensor.

  • ValueError – If the dimension of Tensor is not 0, 1 or 2.

Supported Platforms:

Ascend GPU CPU

Examples

>>> from mindspore import Tensor, ops
>>> from mindspore import dtype as mstype
>>> x1 = Tensor([[4], [3], [2]], mstype.int32)
>>> x2 = Tensor([7, 6, 5], mstype.int32)
>>> x3 = Tensor(1, mstype.int32)
>>> x4 = Tensor([[5, 4, 3], [2, 1, 0]], mstype.int32)
>>> x5 = Tensor([[8, 7], [7, 8]], mstype.int32)
>>> out = ops.block_diag(x1, x2, x3, x4, x5)
>>> print(out.asnumpy())
[[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]]