mindspore.Tensor.tril

Tensor.tril(diagonal=0) Tensor

Returns the lower triangle part of input (elements that contain the diagonal and below), and set the other elements to zeros.

Parameters

diagonal (int, optional) – An optional attribute indicates the diagonal to consider, default: 0, indicating the main diagonal.

Returns

Tensor, the same shape and data type as the input.

Raises
  • TypeError – If diagonal is not an int.

  • TypeError – If the type of input is neither number nor bool.

  • ValueError – If the rank of input 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 = Tensor.tril(x)
>>> print(result)
[[ 1  0  0  0]
 [ 5  6  0  0]
 [10 11 12  0]
 [14 15 16 17]]
>>> x = Tensor(np.array([[ 1,  2,  3,  4],
...                      [ 5,  6,  7,  8],
...                      [10, 11, 12, 13],
...                      [14, 15, 16, 17]]))
>>> result = Tensor.tril(x, diagonal=1)
>>> print(result)
[[ 1  2  0  0]
 [ 5  6  7  0]
 [10 11 12 13]
 [14 15 16 17]]
>>> x = Tensor(np.array([[ 1,  2,  3,  4],
...                      [ 5,  6,  7,  8],
...                      [10, 11, 12, 13],
...                      [14, 15, 16, 17]]))
>>> result = Tensor.tril(x, diagonal=-1)
>>> print(result)
[[ 0  0  0  0]
 [ 5  0  0  0]
 [10 11  0  0]
 [14 15 16  0]]