Pytorch Torch Tril
# PyTorch torch.tril Function
* * *
[ Pytorch torch Reference Manual](https://example.com/pytorch/pytorch-torch-ref.html)
`torch.tril` is a function in PyTorch used to extract the lower triangular part (including the main diagonal) of a matrix. The upper triangular part is set to 0.
### Function Definition
torch.tril(input, diagonal=0, out=None)
Parameter Description:
* `input`: Input tensor
* `diagonal`: Diagonal index, 0 represents the main diagonal
* `out`: Output tensor
* * *
## Usage Examples
## Example
import torch
# Create matrix
a = torch.tensor([[1,2,3],[4,5,6],[7,8,9]])
# Extract lower triangular part
y = torch.tril(a)
print(y)
Output:
tensor([[1, 0, 0], [4, 5, 0], [7, 8, 9]])
## Example
import torch
# Create matrix
a = torch.tensor([[1,2,3],[4,5,6],[7,8,9]])
# Extract lower triangular part below the first diagonal
y = torch.tril(a, diagonal=1)
print(y)
Output:
tensor([[1, 2, 0], [4, 5, 6], [7, 8, 9]])
## Example
import torch
# Create non-square matrix
a = torch.tensor([[1,2,3],[4,5,6]])
# Extract lower triangular part
y = torch.tril(a)
print(y)
Output:
tensor([[1, 0, 0], [4, 5, 0]])
* * *
[ Pytorch torch Reference Manual](https://example.com/pytorch/pytorch-torch-ref.html)
YouTip