当前位置:网站首页>【详解】神经网络矩阵的点乘与叉乘(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.]]])
边栏推荐
- Detailed sorting of Oracle and MySQL pages
- Deep learning - (1) RESNET implementation
- Summary of key knowledge of induction motor in Electrical Engineering (reflected in existing topics)
- 年度十强!赛宁网安再次入围《中国数字安全百强报告》
- steam教育文化传承的必要性
- Interview shock 59: can there be multiple auto increment columns in a table?
- 开展有效的创客教育课程与活动
- The third-party libraries commonly used in golang development are not the most complete, but more complete
- Flask博客实战 - 实现全站导航菜单及首页数据展示
- 我的第一个Go程序
猜你喜欢
随机推荐
Chapter II exercise | MNIST dataset | Titanic dataset | image enhancement
Web knowledge 3 (cookie+session)
读取jar包里面文件夹下的所有文件
10.File/IO流-bite
Summary of sub database and sub table 1
Installation and use of Jupiter notebook
Any to Any 实时变声的实现与落地丨RTC Dev Meetup
[conda]conda switch to source of China University of science and technology
Epidemic situation of novel coronavirus
How to write high performance SQL statements?
Basic knowledge of DDL operation database and operation table and explanation of use format
面试突击59:一个表中可以有多个自增列吗?
Deep learning - (1) RESNET implementation
How to troubleshoot OOM
Coding complexity C (n)
15 command mode
Bee framework, an ORM framework
luogu P5406 [THUPC2019]找树
【自适应控制】最小二乘法离线辨识
The role of subject integration in steam Education
![[path planning] auxiliary points and multi segment Bessel smoothing RRT](/img/0c/74055c93a11b9be18dfec1058796c2.jpg)







