当前位置:网站首页>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 .
边栏推荐
- It is the best tool to evaluate six kinds of map visualization software in three categories
- Kotlin Android development novice tutorial
- From Bayesian filter to Kalman filter (zero)
- [深入研究4G/5G/6G专题-44]: URLLC-15-《3GPP URLLC相关协议、规范、技术原理深度解读》-9-低延时技术-3-非时隙调度Mini slot
- SQL审核工具自荐Owls
- Application of time series database in cigarette factory
- GPIO port configuration of K60
- Libgdx learning road 02: draw game map with tiled
- Jestson nano Object detection
- BM16 delete duplicate elements in the ordered linked list -ii
猜你喜欢

VIM learning manual

【图像隐藏】基于DCT、DWT、LHA、LSB的数字图像信息隐藏系统含各类攻击和性能参数附matlab代码

关于ASM冗余问题

优麒麟系统安装BeyondComare
![[machine learning] support vector machine classification](/img/6d/e4fb9b5bf82362edcebbf366f9b73e.png)
[machine learning] support vector machine classification

From Bayesian filter to Kalman filter (zero)

Fantasy 5 (ue5) game engine complete course 2022
![[image hiding] digital image information hiding system based on DCT, DWT, LHA, LSB, including various attacks and performance parameters, with matlab code](/img/a4/5c5a90508e2f9c6b4f8e234bdfdc9e.png)
[image hiding] digital image information hiding system based on DCT, DWT, LHA, LSB, including various attacks and performance parameters, with matlab code

Libgdx learning road 02: draw game map with tiled

SQL审核工具自荐Owls
随机推荐
BM14 链表的奇偶重排
vim学习手册
Pytorch:快速求得NxN矩阵的主对角线(diagonal)元素与非对角线元素
一家芯片公司倒在了B轮
BLDC 6步换相 simulink
Sudo rosdep init error: cannot download default
Photoshop responsive web design tutorial
剑指 Offer II 109. 开密码锁
Asp net MVC web development tutorial
优麒麟系统安装BeyondComare
Youqilin system installation beyondcomare
Leetcode skimming - super power 372 medium
Fundamentals of software testing and development | practical development of several tools in testing and development
6-20 vulnerability exploitation proftpd test
ES6 new - arrow function
OAI L3 and L2 interface analysis
Application of time series database in intelligent power consumption field
[深入研究4G/5G/6G专题-44]: URLLC-15-《3GPP URLLC相关协议、规范、技术原理深度解读》-9-低延时技术-3-非时隙调度Mini slot
Avoidance Adjusted Climbrate
ardupilot软件在环仿真与在线调试