torch.diagonal(input, offset=0, dim1=0, dim2=1)

torch.diagonal(input, offset=0, dim1=0, dim2=1) 解释

官方解释:

返回输入值的局部视图,并将其相对于 dim1 和 dim2 的对角线元素作为一个维度添加到形状的末尾。

参数偏移量 控制着要考虑的对角线:

如果偏移 = 0,则为主对角线。

如果偏移量 > 0,则在主对角线之上。

如果偏移量 < 0,则在主对角线的下方。

使用相同参数对该函数的输出应用 torch.diag_embed(),可以得到一个对角矩阵,其中包含输入的对角条目。不过,torrent.diag_embed() 有不同的默认维数,因此需要明确指定。

参数 input (Tensor) - 输入张量。必须至少为 2 维。

offset(int,可选)- 要考虑的对角线。默认值:0(主对角线)。

dim1(int,可选项)- 取对角线的第一个维度。默认值:0。

dim2 (int,可选项)- 对角线的第二个维度。默认值:1。

举例

二维矩阵

x,维度为(h,w),其offset=0,dim1=0,dim2 =1。
则对角阵为
o u t p u t = [ ( 0 , 0 ) ,( 1 , 1 ), … … ,( i , i ) ] ,其中 i = m i n ( m , n ) − 1 output = [(0,0),(1,1),……,(i,i)],其中i=min(m,n)-1 output=[(00),(11),……,(ii],其中i=minm,n1
其offset=1,
o u t p u t = [ ( 0 , 1 ) ,( 1 , 2 ), … … ,( i , i + 1 ) ] , i = m i n ( m , n ) − 1 − o f f s e t output = [(0,1),(1,2),……,(i,i+1)],i=min(m,n)-1-offset output=[(01),(12),……,(ii+1],i=minm,n1offset
其offset=-1,
o u t p u t = [ ( 1 , 0 ) ,( 2 , 1 ), … … ,( i + 1 , i ) ] ,其中 i = m i n ( m , n ) − 1 − o f f s e t output = [(1,0),(2,1),……,(i+1,i)],其中i=min(m,n)-1-offset output=[(10),(21),……,(i+1i],其中i=minm,n1offset
在这里插入图片描述
其中浅绿色为offset=-1,黄色为offset=0,浅蓝色offset=1

多维矩阵

若x维度为(a,b,c,d,e)

output = torch.diagonal(x, offset=0, dim1=2, dim2=4),

输出结果形状为(a, b, d, min(c, e))
其中
o u t p u t [ : , : , : , 0 ] = x [ : , : , 0 , : , 0 ] o u t p u t [ : , : , : , 1 ] = x [ : , : , 1 , : , 1 ] o u t p u t [ : , : , : , 2 ] = x [ : , : , 2 , : , 2 ] o u t p u t [ : , : , : , i ] = x [ : , : , i , : , i ] , i = m i n ( c , e ) − 1 output[:, :, :, 0] = x[:, :, 0, :, 0]\\ output[:, :, :, 1] = x[:, :, 1, :, 1]\\ output[:, :, :, 2] = x[:, :, 2, :, 2]\\ output[:, :, :, i] = x[:, :, i, :, i], i=min(c, e)-1 output[:,:,:,0]=x[:,:,0,:,0]output[:,:,:,1]=x[:,:,1,:,1]output[:,:,:,2]=x[:,:,2,:,2]output[:,:,:,i]=x[:,:,i,:,i],i=min(c,e)1

output = torch.diagonal(x, offset=1, dim1=2, dim2=4)

输出为
o u t p u t [ : , : , : , 0 ] = x [ a , b , 0 , d , 1 ] o u t p u t [ : , : , : , 1 ] = x [ : , : , 1 , : , 2 ] o u t p u t [ : , : , : , 2 ] = x [ : , : , 2 , : , 3 ] o u t p u t [ : , : , : , i ] = x [ : , : , i , : , i + 1 ] , i = m i n ( c , e ) − 1 − o f f s e t output[:, :, :, 0] = x[a, b, 0, d, 1]\\ output[:, :, :, 1] = x[:, :, 1, :, 2]\\ output[:, :, :, 2] = x[:, :, 2, :, 3]\\ output[:, :, :, i] = x[:, :, i, :, i+1], i=min(c, e)-1-offset output[:,:,:,0]=x[a,b,0,d,1]output[:,:,:,1]=x[:,:,1,:,2]output[:,:,:,2]=x[:,,2,,3]output[:,:,:,i]=x[,,i,,i+1],i=min(c,e)1offset

output = torch.diagonal(x, offset=-1, dim1=2, dim2=4)

输出为
o u t p u t [ : , : , : , 0 ] = x [ : , : , 1 , : , 0 ] o u t p u t [ : , : , : , 1 ] = x [ : , : , 2 , : , 1 ] o u t p u t [ : , : , : , 2 ] = x [ : , : , 3 , : , 2 ] o u t p u t [ : , : , : , i ] = x [ : , : , i + 1 , : , i ] , i = m i n ( c , e ) − 1 − o f f s e t output[:, :, :, 0] = x[:, :, 1, :, 0]\\ output[:, :, :, 1] = x[:, :, 2, :, 1]\\ output[:, :, :, 2] = x[:, :, 3, :, 2]\\ output[:, :, :, i] = x[:, :, i+1, :, i],i=min(c, e)-1-offset output[:,:,:,0]=x[,,1,,0]output[:,:,:,1]=x[,,2,,1]output[:,:,:,2]=x[,,3,,2]output[:,:,:,i]=x[,,i+1,,i],i=min(c,e)1offset

  • 5
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值