mindspore.nn.Triu
- class mindspore.nn.Triu[source]
Returns a tensor with elements below the kth diagonal zeroed.
- 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
TypeError – If k is not an int.
ValueError – If length of shape of x is less than 1.
- 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]]