当前位置:网站首页>Pytorch:快速求得NxN矩阵的主对角线(diagonal)元素与非对角线元素
Pytorch:快速求得NxN矩阵的主对角线(diagonal)元素与非对角线元素
2022-07-28 17:23:00 【我是大黄同学呀】
前因
今天写代码过程中,要分别求得一个N x NTensor矩阵的主对角元素与非对角元素,看到一个巧妙的写法,故记录下来。
探索
torch.diagonal()
主对角元素很好得到,Pytorch有现成的API可调用,为torch.diagonal,详情如下:
使用起来也很方便,示例如下:
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])
妙用矩阵变换
关于非对角线元素,就没有特定的API了,找寻资料的过程中,看到一个比较巧妙的方法,直接上代码:
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])
核心代码就在最后一行,下面主要分解一下来介绍。首先利用flatten()拉直向量,然后去掉最后一个元素,得到 n 2 − 1 n^2-1 n2−1个元素,然后构造为一个维度为[N-1, N+1]的矩阵。在这个矩阵中,之前所有的对角线元素全部出现在第1列,如下所示:
然后根据索引获取[:, 1:]元素,得到的就是原矩阵的非对角线元素了。
边栏推荐
- Swiftui swift forward geocoding and reverse geocoding (tutorial with source code)
- Can zero basis software testing work?
- Application of time series database in cigarette factory
- 这种动态规划你见过吗——状态机动态规划之股票问题(下)
- 【雷达】基于核聚类实现雷达信号在线分选附matlab代码
- [R language - basic drawing]
- Accumulation and development -- the way of commercialization of open source companies
- DevCon. Exe export output to the specified file
- Applet applet jump to official account page
- C language (high-level) character function and string function + Exercise
猜你喜欢

1、 My first wechat applet

The login interface of modern personal blog system modstartblog v5.4.0 has been revised and the contact information has been added

2022年最火的十大测试工具,你掌握了几个

cv5200无线WiFi通信模块,视频图像传输无线化,实时无线通信技术

Efficiency comparison of JS array splicing push() concat() methods
![[image segmentation] vein segmentation based on directional valley detection with matlab code](/img/82/7b7b761c975cd5c2f5b8f3e43592d2.png)
[image segmentation] vein segmentation based on directional valley detection with matlab code

How to obtain data on mobile phones and web pages after the SCM data is uploaded to Alibaba cloud Internet of things platform?

Fundamentals of software testing and development | practical development of several tools in testing and development

ICLR21(classification) - 未来经典“ViT” 《AN IMAGE IS WORTH 16X16 WORDS》(含代码分析)

QT user defined control user guide (flying Qingyun)
随机推荐
More loading in applets (i.e. list paging)
As for the white box test, you have to be skillful in these skills~
Is zero basic software testing training reliable?
DevCon. Exe export output to the specified file
What does real HTAP mean to users and developers?
ICLR21(classification) - 未来经典“ViT” 《AN IMAGE IS WORTH 16X16 WORDS》(含代码分析)
Kali doesn't have an eth0 network card? What if you don't connect to the Internet
这种动态规划你见过吗——状态机动态规划之股票问题(下)
Libgdx learning road 02: draw game map with tiled
关于白盒测试,这些技巧你得游刃有余~
Remember a uniapp experience
Regular expressions related to face-to-face orders of major express companies in JS
Application of time series database in monitoring operation and maintenance platform
QT user defined control user guide (flying Qingyun)
Mid 2022 summary
Application value of MES production management system to equipment
Getting started with QT & OpenGL
[physical application] atmospheric absorption loss with matlab code
How much is software testing training generally?
[filter tracking] target tracking based on EKF, TDOA and frequency difference positioning with matlab code