当前位置:网站首页>【详解】神经网络矩阵的点乘与叉乘(pytorch版)
【详解】神经网络矩阵的点乘与叉乘(pytorch版)
2022-06-22 08:37:00 【鬼扯子】
太长不看版本
- 点乘为两个矩阵对应元素相乘(逐元素级element-wise)
实现方式:可以通过*和torch.mul(x, y)函数实现(含广播机制)
模型符号:一个圆圈中有一个实心点 - 叉乘为传统的线性代数学的矩阵乘法
实现方式:可以通过torch.mm()和torch.matmul()实现(含广播机制)
模型符号:一个圆圈中有一个叉× - 逐元素相加
实现方式:可以通过+和torch.add(x, y)函数实现
模型符号:一个圆圈中有一个加号+
广播机制:如果PyTorch操作支持广播,则其Tensor参数可以自动扩展为相等大小(无需复制数据)
如果张量x和y可以符合广播的条件,那么:结果张量可以按照下面的方式计算:
1、如果x和y的维度不相同,用1来扩张维度少的那个,使两个张量维度一致。
2、对于每个维度,结果维度是x,y对应维度的最大值。
即:列向量按列扩充,行向量按行扩充,使两个tensor扩展为相同尺寸,进行对应元素相乘。
一、点乘
点乘就是对应位置元素相乘(element-wise),可以通过*和torch.mul()函数实现,在神经网络模型中的符号为:
1.1 标量的点乘
示例:
a = torch.randint(10,(3,3))
print(a)
print(a*2)
print(a.mul(a))
输出:
tensor([[9, 3, 1],
[3, 7, 3],
[4, 8, 3]])
tensor([[18, 6, 2],
[ 6, 14, 6],
[ 8, 16, 6]])
tensor([[81, 9, 1],
[ 9, 49, 9],
[16, 64, 9]]
1.2 向量的点乘(含广播机制BroadCasting)
广播机制:如果PyTorch操作支持广播,则其Tensor参数可以自动扩展为相等大小(无需复制数据)
如果张量x和y可以符合广播的条件,那么:结果张量可以按照下面的方式计算:
1、如果x和y的维度不相同,用1来扩张维度少的那个,使两个张量维度一致。
2、对于每个维度,结果维度是x,y对应维度的最大值。
即:列向量按列扩充,行向量按行扩充,使两个tensor扩展为相同尺寸,进行对应元素相乘。
示例:
b = torch.tensor([[1],[2],[3]]) #列向量
print(b)
c = b.T #转置为行向量
print(c)
print(a*b)
print(a*c)
输出:
tensor([[1],
[2],
[3]])
tensor([[1, 2, 3]])
tensor([[ 9, 3, 1], #按列向右广播的结果
[ 6, 14, 6],
[12, 24, 9]])
tensor([[ 9, 6, 3], #按行向下广播的结果
[ 3, 14, 9],
[ 4, 16, 9]])
1.3 矩阵点乘
示例:
a = torch.randint(5,(2,2))
print(a)
print(a.mul(a))
输出:
tensor([[0, 1], [0, 3]])
tensor([[0, 1], [0, 9]])
二、叉乘
叉乘就是传统的矩阵乘法,可以通过torch.mm()和torch.matmul()实现。在神经网络模型中的符号为:
2.1 矩阵相乘
示例:
x = torch.ones(3,4)
y = torch.eye(4) #对角线为1,其余元素为0
print(torch.mm(x,y))
输出:
tensor([[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.]])
2.2 torch.matmul(含广播机制BroadCasting)
示例:
z = torch.ones(5,4,3)
print(torch.matmul(z,x))
输出:
tensor([[[3., 3., 3., 3.],
[3., 3., 3., 3.],
[3., 3., 3., 3.],
[3., 3., 3., 3.]],
[[3., 3., 3., 3.],
[3., 3., 3., 3.],
[3., 3., 3., 3.],
[3., 3., 3., 3.]],
[[3., 3., 3., 3.],
[3., 3., 3., 3.],
[3., 3., 3., 3.],
[3., 3., 3., 3.]],
[[3., 3., 3., 3.],
[3., 3., 3., 3.],
[3., 3., 3., 3.],
[3., 3., 3., 3.]],
[[3., 3., 3., 3.],
[3., 3., 3., 3.],
[3., 3., 3., 3.],
[3., 3., 3., 3.]]])
边栏推荐
- 一文搞懂one-hot和embedding
- Flask博客实战 - 实现全站导航菜单及首页数据展示
- How to write high performance SQL statements?
- Nouvelle éclosion de Coronavirus
- Mysq index optimization and how to avoid deadlock
- Deeply analyze the usage of final keyword
- YOLOv5报错:AttributeError: ‘Upsample‘ object has no attribute ‘recompute_scale_factor‘ 的解决方案
- 学科融合对steam教育的作用
- Interpreting the technology group in maker Education
- Matlab内数据及数据类型转换
猜你喜欢

Summary of key knowledge of induction motor in Electrical Engineering (reflected in existing topics)

13 proxy mode

报告:在技术领域,男性更有可能获得工作面试的机会

Application of complex science in Maker Teaching Research

08 bridging mode
![Luogu p4292 [wc2010] reconstruction plan](/img/09/5417e0b62e5205c4dabf4571823492.png)
Luogu p4292 [wc2010] reconstruction plan

面试突击59:一个表中可以有多个自增列吗?

一文彻底搞懂My SQL索引知识点

19 备忘录模式

Deep learning - (1) RESNET implementation
随机推荐
Basic knowledge of DDL operation database and operation table and explanation of use format
Flask博客实战 - 实现用户管理
Carry out effective maker education courses and activities
Flask博客实战 - 实现全站导航菜单及首页数据展示
Use record of rabbit nest
邮件巨头曝严重漏洞,用户数据被窃取
The third-party libraries commonly used in golang development are not the most complete, but more complete
Flask blog practice - realize the article list page and details page
Flask博客实战 - 实现文章列表页及详情页
Synchronized
矩阵分解
11 appearance mode
Basic knowledge and practical application of redis
Why can MySQL indexes improve query efficiency so much?
19 memo mode
My first go program
The challenge of image based voice processing in real-time audio and video -- RTC dev Meetup
17 iterator mode
新型冠状病毒疫情
Analyzing the role of cognitive theory in maker teacher training