mindspore.Tensor.triu
- Tensor.triu(diagonal) Tensor
Returns the upper triangle part of 'self' (elements that contain the diagonal and below), and set the other elements to zeros.
Warning
This is an experimental API that is subject to change or deletion.
- Parameters
diagonal (int, optional) – An optional attribute indicates the diagonal to consider, default:
0
, indicating the main diagonal.- Returns
Tensor, a tensor has the same shape and data type as self.
- Raises
TypeError – If diagonal is not an int.
ValueError – If the dimension of self is less than 2.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> import numpy as np >>> from mindspore import Tensor >>> x = Tensor(np.array([[ 1, 2, 3, 4], ... [ 5, 6, 7, 8], ... [10, 11, 12, 13], ... [14, 15, 16, 17]])) >>> result = x.triu() >>> 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]])) >>> result = x.triu(diagonal=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]])) >>> result = x.triu(diagonal=-1) >>> print(result) [[ 1 2 3 4] [ 5 6 7 8] [ 0 11 12 13] [ 0 0 16 17]]