torch.triu() - torch.triu_() - v1.5.0

torch.triu() - torch.triu_() - v1.5.0

  • torch.triu (Python function, in torch)
  • torch.Tensor.triu (Python method, in torch.Tensor)
  • torch.Tensor.triu_ (Python method, in torch.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:~$
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Yongqiang Cheng

梦想不是浮躁,而是沉淀和积累。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值