当前位置:网站首页>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 .
边栏推荐
- Learn from Li Mu, deep learning - linear regression and basic optimization function
- Doxygen文档生成工具
- Module 8 of the construction camp
- Image processing web application development tutorial
- Dockler的基础用法
- 2022年最火的十大测试工具,你掌握了几个
- BM16 删除有序链表中重复的元素-II
- Time waits for no man. The rise of TSDB is at the right time
- 身份证号的奥秘
- Streamlit machine learning application development tutorial
猜你喜欢

Photoshop web design practical tutorial

Solve the critical path in FJSP - with Matlab source code

Application of time series database in monitoring operation and maintenance platform

JS modify table font and table border style

C language (high-level) character function and string function + Exercise

Gmoea code operation 2 -- establishment and operation of operation environment
![[radar] radar signal online sorting based on kernel clustering with matlab code](/img/56/1f8e8690b47fc4a1f101d4e530b87f.png)
[radar] radar signal online sorting based on kernel clustering with matlab code

As for the white box test, you have to be skillful in these skills~

SQL audit tool self introduction owls

SQL custom automatic calculation
随机推荐
IMU 加热
【图像隐藏】基于DCT、DWT、LHA、LSB的数字图像信息隐藏系统含各类攻击和性能参数附matlab代码
Jestson nano Object detection
VIM learning manual
Update of objects in ES6
智能合约安全——溢出漏洞
From Bayesian filter to Kalman filter (I)
SaltStack之salt-ssh
[physical application] Wake induced dynamic simulation of underwater floating wind turbine wind field with matlab code
It is the best tool to evaluate six kinds of map visualization software in three categories
FTM module of K60: configure motor, encoder and steering gear
sudo rosdep init 出现 ERROR: cannot download default
Learn from Li Mu, deep learning - linear regression and basic optimization function
[machine learning] support vector machine classification
用LEX(FLEX)生成PL语言的词法分析器
CVPR19 - 调参干货《Bag of Tricks for Image Classification with Convolutional Neural Network》
Self-adaptive multi-objective evolutionary algorithm for flexible job shop scheduling with fuzzy pro
ES6 new - arrow function
Server body 21: pre compilation processing by different compilers (a brief introduction to MSVC and GCC)
uwb模块实现人员精确定位,超宽带脉冲技术方案,实时厘米级定位应用