pytorch中的各种计算

  对tensor矩阵的维度变换,加减乘除等是深度学习中的常用操作,本文对一些常用方法进行总结

矩阵乘法

  混合矩阵相乘,官方文档,有torch.matmul()、torch.mm()、@三种方法。

torch.matmul(input, other, *, out=None) → Tensor
# 举例
a = torch.tensor([1.,2.])
b = torch.tensor([2.,3.]).view(1,2)
print(torch.mm(a, b))
print(torch.matmul(a, b))
print(a @ b)
# 运行结果
>>>tensor([[2., 3.],
           [4., 6.]])
>>>tensor([[2., 3.],
           [4., 6.]])
>>>tensor([[2., 3.],
           [4., 6.]])

  这个方法执行矩阵相乘操作,需要第一个矩阵的最后一个维度和第二个矩阵的第一个维度相同,即:假设我们有两个矩阵 A 和 B,它们的 size 分别为 (m, n) 和 (n, p),那么 A x B 的 size 为 (m, p)。
  矩阵点乘,也叫哈达玛积,有torch.mul()、torch.dot()、*三种方法。官方文档

torch.mul(input, other, *, out=None) → Tensor
# 举例
a = torch.tensor([1,2])
b = torch.tensor([2,3])
print(a*b)
print(torch.mul(a,b))
运行结果
>>> tensor([2, 6])
>>> tensor([2, 6])

  这个方法对矩阵做点积运算(也可简写为*),要求第一个矩阵的第一个维度和第二个矩阵的第一个维度对应。torch.dot()类似于mul(),它是向量(即只能是一维的张量)的对应位相乘再求和,返回一个tensor。

矩阵除法

  矩阵的点乘与除法的运算规则相同,torch中的torch.div()其实就是/, 类似的:torch.add就是+,torch.sub()就是-,不过符号的运算更简单常用:

a = torch.tensor([1.,2.])
b = torch.tensor([2.,3.])
print(a/b)
print(torch.div(a/b))
>>> tensor([0.5000, 0.6667])
>>> tensor([0.5000, 0.6667])

矩阵维度变换

  tensor.view方法,用于调整矩阵的维度,这个方法要求矩阵在调整为度前后的元素个数必须是相同的,官网,例子:

>>> t = torch.rand(4, 4)
>>> b = t.view(2, 8)
>>> t.storage().data_ptr() == b.storage().data_ptr()  # `t` and `b` share the same underlying data.
True
# Modifying view tensor changes base tensor as well.
>>> b[0][0] = 3.14
>>> t[0][0]
tensor(3.14)

  torch中对矩阵的压缩和扩张维度操作:torch.squeeze和torch.unsqueeze,这两种方法的作用是压缩矩阵中的某一个维度或者增加一个维度,官网,两种方法的详解可以参考我之前的笔记pytorch中的torch.squeeze和torch.unsqueeze

矩阵填充

  torch.nn.functional.pad方法对矩阵进行填充或裁剪操作,官网torch.nn.functional.pad

torch.nn.functional.pad(input, pad, mode='constant', value=None) → Tensor
Args:
	"""
	input:四维或者五维的tensor Variabe
	pad:不同Tensor的填充方式
		1.四维Tensor:传入四元素tuple(pad_l, pad_r, pad_t, pad_b),
		指的是(左填充,右填充,上填充,下填充),其数值代表填充次数
		2.六维Tensor:传入六元素tuple(pleft, pright, ptop, pbottom, pfront, pback),
		指的是(左填充,右填充,上填充,下填充,前填充,后填充),其数值代表填充次数
	mode: ’constant‘, ‘reflect’ or ‘replicate’三种模式,指的是常量,反射,复制三种模式
	value:填充的数值,在"contant"模式下默认填充0,mode="reflect" or "replicate"时没有			

  如果给入的填充次数是负数,该函数可以实现从该方向对矩阵的裁剪操作。
  需要注意的是,本文中提到的所有方法都支持broadcast操作,也就是,除了参与操作的最后两个维度(矩阵),前面的所有维度都会被认为是batch,以torch,matmul为例,该方法使用两个tensor的后两个维度来计算,其他的维度都可以认为是batch。假设两个输入的维度分别是 i n p u t ( 1000 × 500 × 99 × 11 ) input(1000×500×99×11) input(1000×500×99×11), o t h e r ( 500 × 11 × 99 ) other(500×11×99) other(500×11×99),那么我们可以认为 t o r c h . m a t m u l ( i n p u t , o t h e r ) torch.matmul(input,other) torch.matmul(input,other) 首先是进行后两位矩阵乘法得到 ( 99 × 99 ) (99×99) (99×99) ,然后分析两个参数的batch size分别是 ( 1000 × 500 ) (1000×500) (1000×500) ( 500 ) (500) (500), 可以广播成为 ( 1000 × 500 ) (1000×500) (1000×500),因此最终输出的维度是 ( 1000 × 500 × 99 × 99 ) (1000×500×99×99) (1000×500×99×99)

  • 29
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一本糊涂张~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值