Rate this Page

torch.tril#

torch.tril(input,diagonal=0,*,out=None)Tensor#

Returns the lower triangular part of the matrix (2-D tensor) or batch of matricesinput, the other elements of the result tensorout are set to 0.

The lower triangular part of the matrix is defined as the elements on andbelow the diagonal.

The argumentdiagonal controls which diagonal to consider. Ifdiagonal = 0, all elements on and below the main diagonal areretained. A positive value includes just as many diagonals above the maindiagonal, and similarly a negative value excludes just as many diagonals belowthe main diagonal. The main diagonal are the set of indices{(i,i)}\lbrace (i, i) \rbrace fori[0,min{d1,d2}1]i \in [0, \min\{d_{1}, d_{2}\} - 1] whered1,d2d_{1}, d_{2} are the dimensions of the matrix.

Parameters
  • input (Tensor) – the input tensor.

  • diagonal (int,optional) – the diagonal to consider

Keyword Arguments

out (Tensor,optional) – the output tensor.

Example:

>>>a=torch.randn(3,3)>>>atensor([[-1.0813, -0.8619,  0.7105],        [ 0.0935,  0.1380,  2.2112],        [-0.3409, -0.9828,  0.0289]])>>>torch.tril(a)tensor([[-1.0813,  0.0000,  0.0000],        [ 0.0935,  0.1380,  0.0000],        [-0.3409, -0.9828,  0.0289]])>>>b=torch.randn(4,6)>>>btensor([[ 1.2219,  0.5653, -0.2521, -0.2345,  1.2544,  0.3461],        [ 0.4785, -0.4477,  0.6049,  0.6368,  0.8775,  0.7145],        [ 1.1502,  3.2716, -1.1243, -0.5413,  0.3615,  0.6864],        [-0.0614, -0.7344, -1.3164, -0.7648, -1.4024,  0.0978]])>>>torch.tril(b,diagonal=1)tensor([[ 1.2219,  0.5653,  0.0000,  0.0000,  0.0000,  0.0000],        [ 0.4785, -0.4477,  0.6049,  0.0000,  0.0000,  0.0000],        [ 1.1502,  3.2716, -1.1243, -0.5413,  0.0000,  0.0000],        [-0.0614, -0.7344, -1.3164, -0.7648, -1.4024,  0.0000]])>>>torch.tril(b,diagonal=-1)tensor([[ 0.0000,  0.0000,  0.0000,  0.0000,  0.0000,  0.0000],        [ 0.4785,  0.0000,  0.0000,  0.0000,  0.0000,  0.0000],        [ 1.1502,  3.2716,  0.0000,  0.0000,  0.0000,  0.0000],        [-0.0614, -0.7344, -1.3164,  0.0000,  0.0000,  0.0000]])