torch.matmul的前后两个矩阵维度不同的小结

系列文章目录

本系列记录自己的代码学习知识



前言


一、torch.matmul()

torch.matmul()也是一种类似于矩阵相乘操作的tensor联乘操作。但是它可以利用python 中的广播机制,处理一些维度不同的tensor结构进行相乘操作。这也是该函数与torch.bmm()区别所在。

torch.matmul(input,other)#我们现以不同的名字区分前后两个矩阵

来源
作者:top_小酱油
链接:https://www.jianshu.com/p/e277f7fc67b3
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

二、详解解释

1.input维度比other大

例如:

n = ([1,2,3],[1,2,3])
n=torch.tensor(n)
print('n.size',n.size())

n.size torch.Size([2, 3])

m = ([1,2,3])
m=torch.tensor(m)
print('m.size',m.size())

m.size torch.Size([3])

b=torch.matmul(n,m)
print('b',b)
print('b.shape',b.shape)

b tensor([14, 14])
b.shape torch.Size([2])

解释:
在这里插入图片描述

2.input维度比other小

例如:

import torch
x = ([1,2])
x=torch.tensor(x)
print('x.size',x.size())

x.size torch.Size([2])

y = ([1,2,3],[1,2,3])
y=torch.tensor(y)
print('y.size',y.size())

y.size torch.Size([2, 3])

a=torch.matmul(x,y)
print('a',a)
print('a.shape',a.shape)

a tensor([3, 6, 9])
a.shape torch.Size([3])

解释:

在这里插入图片描述


总结

结合看到的知识,自己重新整理。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值