Reputation: 3128
Simple question, but is there a native way to create an upper triangular matrix from an existing matrix in Pytorch? I was thinking of using a mask, but even that requires creating the upper triangular matrix.
Upvotes: 4
Views: 6994
Reputation: 125
import torch
l = torch.tril(torch.ones(row, column))
This will return the lower triangular part of the matrix of size (row, column)
.
Upvotes: 2
Reputation: 96
import torch
upper_tri = torch.ones(rol, col).triu()
Eg:
>> mat = torch.ones(3, 3).triu()
>> print(mat)
tensor([[1., 1., 1.],
[0., 1., 1.],
[0., 0., 1.]])
Upvotes: 5