information_interchange
information_interchange

Reputation: 3128

How to create upper triangular matrix in Pytorch?

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

Answers (2)

Ahmed Bhna
Ahmed Bhna

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

ABD
ABD

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

Related Questions