当前位置:网站首页>矩阵相乘
矩阵相乘
2022-08-08 20:32:00 【JakeSun】
矩阵相乘详解

已知三个矩阵
数学上的矩阵相乘 C = A B
数学表示
程序表示
多维矩阵:torch.matmul(A,B)
if:
then: torch.matmul(A, B)
二维矩阵相乘:torch.mm(A,B)
# 矩阵相乘
x = tensor([[1, 2, 3],
[3, 3, 4],
[3, 3, 3]])
# torch.matmul表示矩阵的乘法
torch.matmul(x,x)
Out[1]:
tensor([[16, 17, 20],
[24, 27, 33],
[21, 24, 30]])
# 两个维度对上就可以进行运算
x = tensor([[1, 2, 3],
[3, 3, 4],
[3, 3, 3]])
y = tensor([[1, 2],
[3, 3],
[4, 4]])
torch.matmul(x, y)
Out[2]:
tensor([[19, 20],
[28, 31],
[24, 27]])
数学上的矩阵对位相乘
数学表示
程序表示
torch.mul(A,B)
# 表示矩阵对位相乘
x = tensor([[1, 2, 3],
[3, 3, 4],
[3, 3, 3]])
# 方法1
x * x
Out[3]:
tensor([[ 1, 4, 9],
[ 9, 9, 16],
[ 9, 9, 9]])
# 方法2
torch.mul(x,x)
Out[4]:
tensor([[ 1, 4, 9],
[ 9, 9, 16],
[ 9, 9, 9]])
带有batch的三维就一阵相乘
torch.bmm(A, B)
,
torch.bmm(A, B)
t = tensor([[[1, 2, 3],
[3, 3, 4],
[3, 3, 3]],
[[1, 2, 3],
[3, 3, 4],
[3, 3, 3]]])
T = torch.bmm(t, t)
T.shape
Out[5]: torch.Size([2, 3, 3])
T
Out[6]:
tensor([[[16, 17, 20],
[24, 27, 33],
[21, 24, 30]],
[[16, 17, 20],
[24, 27, 33],
[21, 24, 30]]])
# 两个维度不同
u = tensor([[[1, 2],
[3, 3],
[4, 4]],
[[1, 2],
[3, 3],
[4, 4]]])
t = tensor([[[1, 2, 3],
[3, 3, 4],
[3, 3, 3]],
[[1, 2, 3],
[3, 3, 4],
[3, 3, 3]]])
u.shape
Out[7]: torch.Size([2, 3, 2])
t.shape
Out[8]: torch.Size([2, 3, 3])
torch.bmm(t, u)
Out[9]:
tensor([[[19, 20],
[28, 31],
[24, 27]],
[[19, 20],
[28, 31],
[24, 27]]])
torch.bmm(t, u).shape
Out[10]: torch.Size([2, 3, 2])
边栏推荐
- 基于opencv的图片人像移除
- 自然堂品牌焕新升级,携手代言人王一博彰显美妆年轻新态度
- Little knowledge about KotlinAndroid encounters
- fillder4 keeps prompting the system proxy was changed, watch me solve it
- WPF主窗体调用 User32的SetWindowPos 设置窗体置顶会导致与其他窗体抢夺焦点的问题
- 网络工程师怎么系统性学习?这份网工资料包帮你解决
- Kotlin委托属性知识点
- 如何用精益敏捷组合管理,提升研发效能?软件研发团队必看!
- fillder4不间断提示the system proxy was change,看我解决
- 深度学习初步认知
猜你喜欢
学习笔记:2.3 静态链表 循环链表 双向链表
fillder4不间断提示the system proxy was change,看我解决
rk3588使用npu进行模型转换和推理,加速AI应用落地
莅临GOPS大会龙智展位,获取Forrester最新报告:《Forrester Wave:2021年第四季度企业服务管理报告》
接口测试经典面试题:Session、cookie、token有什么区别?
学习笔记:线性表的顺序表示和实现(二级指针实现)
Experience Sharing | A low-cost and fast-paced approach to building an enterprise knowledge management system
com.alibaba.fastjson.JSONException: default constructor not found. class
使用fontforge修改字体,只保留数字
JSP第二篇 -----JSP浅聊EL表达式第二篇:EL表达式中的运算符
随机推荐
Kotlin基础稳固第一天
学习笔记:第二部分 队列
Float.parseFloat()的作用
Gartner:2022年全球半导体收入增长预计将放缓至7%,远低于2021年26.3%
Swoole 健康检查
Kotlin委托属性知识点
Kotlin反射
图的几种存储方式
莅临GOPS大会龙智展位,获取Forrester最新报告:《Forrester Wave:2021年第四季度企业服务管理报告》
深度学习初步认知
实践篇2:深度学习之----LetNet之tensorflow2的实现
Use fontforge to modify font, keep only numbers
解决gradle导包速度慢问题
PHP传递任意数量的函数参数
学习笔记:2.3 静态链表 循环链表 双向链表
有幸与美团大佬共同探讨单节点连接数超1.5W的问题
Superman is coming!Flutter realizes full-screen power animation!
Mysql管理指令
买股票安全吗 资金能取出来吗
【无标题】