mindspore.ops.Triu

class mindspore.ops.Triu(diagonal=0)[source]

Returns the upper triangular portion of the 2-D matrix or the set of matrices in a batch. The remaining elements of the resulting Tensor are assigned a value of 0. The upper triangular section of the matrix comprises of the elements present on and above the main diagonal.

Warning

This is an experimental API that is subject to change or deletion.

Parameters

diagonal (int, optional) – The index of diagonal. Default: 0, indicating the main diagonal.

Inputs:
  • x (Tensor) - The input tensor with shape \((M, N, *)\) where \(*\) means any number of additional dimensions. The data type is Number.

Outputs:
  • y (Tensor) - A tensor has the same shape and data type as input.

Raises
Supported Platforms:

Ascend GPU CPU

Examples

>>> x = Tensor(np.array([[ 1,  2,  3,  4],
...                      [ 5,  6,  7,  8],
...                      [10, 11, 12, 13],
...                      [14, 15, 16, 17]]))
>>> triu = ops.Triu()
>>> result = triu(x)
>>> print(result)
[[ 1  2  3  4]
 [ 0  6  7  8]
 [ 0  0 12 13]
 [ 0  0  0 17]]
>>> x = Tensor(np.array([[ 1,  2,  3,  4],
...                      [ 5,  6,  7,  8],
...                      [10, 11, 12, 13],
...                      [14, 15, 16, 17]]))
>>> triu = ops.Triu(diagonal=1)
>>> result = triu(x)
>>> print(result)
[[ 0  2  3  4]
 [ 0  0  7  8]
 [ 0  0  0 13]
 [ 0  0  0  0]]
>>> x = Tensor(np.array([[ 1,  2,  3,  4],
...                      [ 5,  6,  7,  8],
...                      [10, 11, 12, 13],
...                      [14, 15, 16, 17]]))
>>> triu = ops.Triu(diagonal=-1)
>>> result = triu(x)
>>> print(result)
[[ 1  2  3  4]
 [ 5  6  7  8]
 [ 0 11 12 13]
 [ 0  0 16 17]]