mindspore.nn.Triu

class mindspore.nn.Triu[source]

Returns a tensor with elements below the kth diagonal zeroed.

The upper triangular part of the matrix is defined as the elements on and above the diagonal.

The parameter k controls the diagonal to be considered. If k = 0, all elements on and above the main diagonal are retained. Positive values do not include as many diagonals above the main diagonal, and similarly, negative values include as many diagonals below the main diagonal.

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

  • k (Int) - The index of diagonal. Default: 0

Outputs:

Tensor, has the same type and shape as input x.

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 = nn.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 = nn.Triu()
>>> result = triu(x, 1)
>>> 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 = nn.Triu()
>>> result = triu(x, 2)
>>> print(result)
[[ 0  0  3  4]
 [ 0  0  0  8]
 [ 0  0  0  0]
 [ 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 = nn.Triu()
>>> result = triu(x, -1)
>>> print(result)
[[ 1  2  3  4]
 [ 5  6  7  8]
 [ 0 11 12 13]
 [ 0  0 16 17]]