当前位置:网站首页>Pytoch: quickly find the main diagonal elements and non diagonal elements of NxN matrix
Pytoch: quickly find the main diagonal elements and non diagonal elements of NxN matrix
2022-07-28 19:25:00 【I'm Mr. rhubarb】
Antecedents
In the process of writing code today , To get one respectively N x NTensor Main diagonal elements and off diagonal elements of matrix , See a clever way of writing , So write it down .
Explore
torch.diagonal()
The main diagonal element is easy to get ,Pytorch Have a ready-made API Callable , by torch.diagonal, Details are as follows :
It's also easy to use , Examples are as follows :
x = torch.randn(4,4)
# tensor([[ 0.9148, 0.1396, -0.8974, 2.0014],
# [ 0.1129, -0.3656, 0.4371, 0.2618],
# [ 1.1049, -0.0774, -0.4160, -0.4922],
# [ 1.3197, -0.2022, -0.0031, -1.3811]])
torch.diagonal(x)
# tensor([ 0.9148, -0.3656, -0.4160, -1.3811])
Clever use of matrix transformation
About non diagonal elements , There is no specific API 了 , In the process of searching for information , See a more ingenious method , Go straight to the code :
Main reference :https://github1s.com/facebookresearch/barlowtwins/blob/e6f34a01c0cde6f05da6f431ef8a577b42e94e71/main.py#L207
n, m = x.shape
assert n == m
x.flatten()[:-1].view(n-1,n+1)[:,1:].flatten()
# tensor([ 0.1396, -0.8974, 2.0014, 0.1129, 0.4371, 0.2618, 1.1049, -0.0774,
# -0.4922, 1.3197, -0.2022, -0.0031])
The core code is on the last line , The following is mainly about decomposition . The first use of flatten() Straighten vector , Then remove the last element , obtain n 2 − 1 n^2-1 n2−1 Elements , Then construct a dimension of [N-1, N+1] Matrix . In this matrix , All previous diagonal elements appear in the 1 Column , As shown below :
Then get according to the index [:, 1:] Elements , The result is the non diagonal element of the original matrix .
边栏推荐
- [machine learning] support vector machine classification
- Qt: one signal binds multiple slots
- BM16 删除有序链表中重复的元素-II
- 【物理应用】大气吸收损耗附matlab代码
- Random finite set RFs self-study notes (6): an example of calculation with the formula of prediction step and update step
- SaltStack进阶
- Solve the critical path in FJSP - with Matlab source code
- Application of time series database in Hydropower Station
- ICLR21(classification) - 未来经典“ViT” 《AN IMAGE IS WORTH 16X16 WORDS》(含代码分析)
- 关于ASM冗余问题
猜你喜欢

Time waits for no man. The rise of TSDB is at the right time

一家芯片公司倒在了B轮

智能合约安全——溢出漏洞

软件测试开发基础|测开中的几个工具开发实战

Swing事件处理的过程是怎样的?

BM11 链表相加(二)

DevCon.exe 导出output至指定文件

Efficiency comparison of JS array splicing push() concat() methods

Qt: 一个SIGNAL绑定多个SLOT

Application of time series database in intelligent power consumption field
随机推荐
[image hiding] digital image information hiding system based on DCT, DWT, LHA, LSB, including various attacks and performance parameters, with matlab code
优麒麟系统安装BeyondComare
RFs self study notes (III): clutter model - first determine the number with Poisson distribution, and then use uniform distribution as probability distribution
Photoshop responsive web design tutorial
Application value of MES production management system to equipment
【雷达】基于核聚类实现雷达信号在线分选附matlab代码
FTM module of K60: configure motor, encoder and steering gear
Swing事件处理的过程是怎样的?
From Bayesian filter to Kalman filter (I)
R language text mining and natural language processing tutorial
Update of objects in ES6
Remember a uniapp experience
Application of time series database in museum environment detection
Application of time series database in bridge monitoring field
关于白盒测试,这些技巧你得游刃有余~
Asp net MVC web development tutorial
JS 批量添加事件监听onclick this 事件委托 target currentTarget onmouseenter onmouseover
Regular expressions related to face-to-face orders of major express companies in JS
C string to short[] method
SaltStack之salt-ssh