torch.triu() - torch.triu_() - v1.5.0
torch.triu
(Python function, intorch
)torch.Tensor.triu
(Python method, intorch.Tensor
)torch.Tensor.triu_
(Python method, intorch.Tensor
)
torch.Tensor
https://pytorch.org/docs/stable/tensors.html
triu(k=0)
-> Tensor
See torch.triu()
triu_(k=0)
-> Tensor
In-place version of triu()
torch
https://pytorch.org/docs/stable/torch.html
torch.triu(input, diagonal=0, out=None)
-> Tensor
Returns the upper triangular part of a matrix (2-D tensor) or batch of matrices input, the other elements of the result tensor out are set to 0.
返回一个张量,包含输入矩阵 (2D 张量) 的上三角部分,其余部分被设为 0。上三角部分为矩阵指定对角线 diagonal 之上的元素。
The upper triangular part of the matrix is defined as the elements on and above the diagonal.
上三角部分为矩阵指定对角线 diagonal 之上的元素。
The argument diagonal
controls which diagonal to consider. If diagonal = 0, all elements on and above the main diagonal are retained. A positive value excludes just as many diagonals above the main diagonal, and similarly a negative value includes just as many diagonals below the main diagonal. The main diagonal are the set of indices
{
(
i
,
i
)
}
\{(i,i)\}
{(i,i)} for
i
∈
[
0
,
min
{
d
1
,
d
2
}
−
1
]
i \in [0, \min\{d_{1}, d_{2}\} - 1]
i∈[0,min{d1,d2}−1] where
d
1
d_{1}
d1,
d
2
d_{2}
d2 are the dimensions of the matrix.
参数 diagonal 控制要考虑的对角线。如果 diagonal = 0,则保留主对角线上和上方的所有元素。正值排除主对角线和对角线上方的部分元素,同样负值包括主对角线和主对角线下方的部分元素。主对角线是
{
(
i
,
i
)
}
\{(i,i)\}
{(i,i)} for
i
∈
[
0
,
min
{
d
1
,
d
2
}
−
1
]
i \in [0, \min\{d_{1}, d_{2}\} - 1]
i∈[0,min{d1,d2}−1] 的索引集,其中
d
1
d_{1}
d1,
d
2
d_{2}
d2 是矩阵的维数。
参数 k
控制对角线:
- k = 0,保留主对角线上和主对角线上方的所有元素
- k > 0,主对角线之上的相应元素
- k < 0,主对角线之下的相应元素
1. Parameters
input (Tensor)
– the input tensor.
diagonal (int, optional)
– the diagonal to consider. (指定对角线。)
out (Tensor, optional)
– the output tensor.
2. Example
(pt-1.4_py-3.6) yongqiang@yongqiang:~$ python
Python 3.6.10 |Anaconda, Inc.| (default, May 8 2020, 02:54:21)
[GCC 7.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
>>>
>>> a = torch.randn(4, 4)
>>> a
tensor([[ 0.5144, 0.5091, -0.3698, 0.3694],
[-1.1344, -0.2793, 1.6651, -1.3632],
[-0.3397, -0.1468, -0.0300, -1.1186],
[-2.1449, 1.3087, -0.1409, 2.4678]])
>>>
>>> torch.triu(a)
tensor([[ 0.5144, 0.5091, -0.3698, 0.3694],
[ 0.0000, -0.2793, 1.6651, -1.3632],
[ 0.0000, 0.0000, -0.0300, -1.1186],
[ 0.0000, 0.0000, 0.0000, 2.4678]])
>>>
>>> torch.triu(a, diagonal=1)
tensor([[ 0.0000, 0.5091, -0.3698, 0.3694],
[ 0.0000, 0.0000, 1.6651, -1.3632],
[ 0.0000, 0.0000, 0.0000, -1.1186],
[ 0.0000, 0.0000, 0.0000, 0.0000]])
>>>
>>> torch.triu(a, diagonal=-1)
tensor([[ 0.5144, 0.5091, -0.3698, 0.3694],
[-1.1344, -0.2793, 1.6651, -1.3632],
[ 0.0000, -0.1468, -0.0300, -1.1186],
[ 0.0000, 0.0000, -0.1409, 2.4678]])
>>>
>>> torch.triu(a, diagonal=-2)
tensor([[ 0.5144, 0.5091, -0.3698, 0.3694],
[-1.1344, -0.2793, 1.6651, -1.3632],
[-0.3397, -0.1468, -0.0300, -1.1186],
[ 0.0000, 1.3087, -0.1409, 2.4678]])
>>>
>>> torch.triu(a, diagonal=-3)
tensor([[ 0.5144, 0.5091, -0.3698, 0.3694],
[-1.1344, -0.2793, 1.6651, -1.3632],
[-0.3397, -0.1468, -0.0300, -1.1186],
[-2.1449, 1.3087, -0.1409, 2.4678]])
>>>
>>> exit()
(pt-1.4_py-3.6) yongqiang@yongqiang:~$
(pt-1.4_py-3.6) yongqiang@yongqiang:~$ python
Python 3.6.10 |Anaconda, Inc.| (default, May 8 2020, 02:54:21)
[GCC 7.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
>>>
>>> b = torch.randn(4, 6)
>>> b
tensor([[-1.3014, -1.2629, 0.7176, 1.2692, -0.1408, -0.9948],
[ 1.4856, -1.1522, 0.8107, 0.2437, 0.0965, -0.9363],
[-0.2229, -0.6405, -0.3730, 1.5058, 0.6841, 1.7821],
[ 0.1128, -0.2907, 0.1218, 1.1333, -0.2058, -0.0554]])
>>>
>>> torch.triu(b, diagonal=1)
tensor([[ 0.0000, -1.2629, 0.7176, 1.2692, -0.1408, -0.9948],
[ 0.0000, 0.0000, 0.8107, 0.2437, 0.0965, -0.9363],
[ 0.0000, 0.0000, 0.0000, 1.5058, 0.6841, 1.7821],
[ 0.0000, 0.0000, 0.0000, 0.0000, -0.2058, -0.0554]])
>>>
>>> torch.triu(b, diagonal=2)
tensor([[ 0.0000, 0.0000, 0.7176, 1.2692, -0.1408, -0.9948],
[ 0.0000, 0.0000, 0.0000, 0.2437, 0.0965, -0.9363],
[ 0.0000, 0.0000, 0.0000, 0.0000, 0.6841, 1.7821],
[ 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, -0.0554]])
>>>
>>> torch.triu(b, diagonal=-1)
tensor([[-1.3014, -1.2629, 0.7176, 1.2692, -0.1408, -0.9948],
[ 1.4856, -1.1522, 0.8107, 0.2437, 0.0965, -0.9363],
[ 0.0000, -0.6405, -0.3730, 1.5058, 0.6841, 1.7821],
[ 0.0000, 0.0000, 0.1218, 1.1333, -0.2058, -0.0554]])
>>>
>>> torch.triu(b, diagonal=-2)
tensor([[-1.3014, -1.2629, 0.7176, 1.2692, -0.1408, -0.9948],
[ 1.4856, -1.1522, 0.8107, 0.2437, 0.0965, -0.9363],
[-0.2229, -0.6405, -0.3730, 1.5058, 0.6841, 1.7821],
[ 0.0000, -0.2907, 0.1218, 1.1333, -0.2058, -0.0554]])
>>>
>>> exit()
(pt-1.4_py-3.6) yongqiang@yongqiang:~$