当前位置:网站首页>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
- After several twists and turns, how long can the TSDB C-bit of influxdb last?
- Photoshop responsive web design tutorial
- Pointer learning of C language -- the consolidation of pointer knowledge and the relationship with functions, arrays and structures
- Get to know nodejs for the first time (with cases)
- PyG搭建异质图注意力网络HAN实现DBLP节点预测
- Avoidance Adjusted Climbrate
- Server body 21: pre compilation processing by different compilers (a brief introduction to MSVC and GCC)
- Self-adaptive multi-objective evolutionary algorithm for flexible job shop scheduling with fuzzy pro
- SQL custom automatic calculation
猜你喜欢

Application of time series database in museum environment detection

Pytest custom hook function

Understanding of PID

Tikz draw Gantt chart in FJSP -trans necessary

pytest 自定义HOOK函数

SQL audit tool self introduction owls
![[physical application] atmospheric absorption loss with matlab code](/img/72/e6ac23012a59ac48a37bcbb068890b.png)
[physical application] atmospheric absorption loss with matlab code

Cv5200 wireless WiFi communication module, wireless video image transmission, real-time wireless communication technology

优麒麟系统安装BeyondComare

【图像隐藏】基于DCT、DWT、LHA、LSB的数字图像信息隐藏系统含各类攻击和性能参数附matlab代码
随机推荐
IMU 加热
ES6 conversion of new data type set and arr set map
IMU heating
Update of objects in ES6
Lookup - lookup of sequential table and ordered table
Leetcode skimming - super power 372 medium
关于ASM冗余问题
Avoidance Adjusted Climbrate
2022年最火的十大测试工具,你掌握了几个
From Bayesian filter to Kalman filter (zero)
Application of time series database in museum environment detection
Self-adaptive multi-objective evolutionary algorithm for flexible job shop scheduling with fuzzy pro
机器学习 --- 模型评估、选择与验证
[physical application] atmospheric absorption loss with matlab code
Regular expressions related to face-to-face orders of major express companies in JS
[data analysis] realize SVDD decision boundary visualization based on MATLAB
Learn from Li Mu in depth -softmax return
【已解决】AC86U ML改版固件虚拟内存创建失败,提示USB磁盘读写速度不满足要求
QT function optimization: QT 3D gallery
From Bayesian filter to Kalman filter (2)