首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >pytorch基础知识-运算(上)

pytorch基础知识-运算(上)

作者头像
用户6719124
发布2019-11-17 23:03:25
1.8K0
发布2019-11-17 23:03:25
举报

本节主要讲解pytorch中,tensor和矩阵的各类数学运算。

首先从最基本的加减乘除开始

.add (相加)

.sub (相减)

.mul (相乘)

.div (相除)

import torch
a = torch.rand(2, 3)
b = torch.rand(3)
c = torch.add(a, b)
d = torch.sub(a, b)
e = torch.mul(a, b)
f = torch.div(a, b)
print('c, d, e, f=', c, '\n', d, '\n', e, '\n', f)

输出为

c, d, e, f= tensor([[0.8616, 0.8815, 0.5835],
        [1.3956, 1.0337, 0.9400]]) 
 tensor([[-0.7527,  0.0045,  0.2063],
        [-0.2186,  0.1567,  0.5628]]) 
 tensor([[0.0440, 0.1943, 0.0745],
        [0.4750, 0.2610, 0.1417]]) 
 tensor([[0.0675, 1.0102, 2.0935],
        [0.7291, 1.3573, 3.9837]])

而在tensor的乘法运算中,*又分为element_wise(元素相乘) 和 martix_matmul(矩阵形式相乘)两种。而按矩阵形式相乘有三种表达形式:

(1)torch.mm (只适用于 2D矩阵,不推荐使用)

(2)torch.matmal (适用于2D和3D矩阵相乘,推荐使用)

(3)@ 与torch.matmal原理相同,是matmal的另外一种写法

举例

a = torch.rand(2, 2)
# print(a)
b = torch.rand(2, 3)
c = torch.mm(a, b)
d = torch.matmul(a, b)
e = a@b
print('c, b, e = ', c, '\n', d, '\n', e)

输出

c, b, e =  tensor([[1.0027, 0.2992, 0.3707],
        [1.0420, 0.2906, 0.3730]]) 
 tensor([[1.0027, 0.2992, 0.3707],
        [1.0420, 0.2906, 0.3730]]) 
 tensor([[1.0027, 0.2992, 0.3707],
        [1.0420, 0.2906, 0.3730]])

为更直观的了解其用法,下面以一个具体的例子进行讲解

a = torch.rand(4, 784)
# 物理意义为4张图片,全部打平为28*28的数据
# 在图片处理中经常需要使用降维操作,本例考虑将[4, 784]降维成[4, 512]
# 因此按照以上思路,考虑需要构建一个新矩阵,使得[4, 784] * [784, 512] = [4, 512]
# 上面的[784, 512]即为考虑构建的新矩阵
# 下面开始构建
w = torch.rand(784, 512)
out = torch.matmul(a, w)
print(out.shape)

输出为

torch.Size([4, 512])

而在对高于2维的矩阵进行运算时,使用torch.mm时会报错

# 对于4维矩阵运算,在使用torch.mm时会报错
a = torch.rand(4, 1, 64, 32)
b = torch.rand(4, 3, 32, 64)
print(torch.mm(a, b).shape)

输出

RuntimeError: matrices expected, got 4D, 4D tensors at ..\aten\src\TH/generic/THTensorMath.cpp:956

因此该改用torch.matmul进行计算

# 高于2维的矩阵运算,应使用torch.matmul函数
a = torch.rand(4, 1, 64, 32)
b = torch.rand(4, 3, 32, 64)
print(torch.matmul(a, b).shape)

输出

torch.Size([4, 3, 64, 64])

这里注意,四维计算时前两个维度和后两个维度分别进行计算,因此(4, 1)被broadcasting成(4, 3),32*32被消掉,后面变成了(64, 64)。

在此之上,我们更改b,再进行计算

a = torch.rand(4, 3, 64, 32)
b = torch.rand(4, 64, 32)
print(torch.matmul(a, b).shape)

输出会报错

RuntimeError: The size of tensor a (3) must match the size of tensor b (4) at non-singleton dimension 1

报错原因在于b会broadcasting成[1, 4, 64, 32],但与a相乘时,dim=1位置上的3和4不相同,无法进行运算。因此矩阵在进行乘法运算时,要么就相同dim位置上的数值相同,要么就为1,以方便进行broadcasting。

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-10-11,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 python pytorch AI机器学习实践 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
图片处理
图片处理(Image Processing,IP)是由腾讯云数据万象提供的丰富的图片处理服务,广泛应用于腾讯内部各产品。支持对腾讯云对象存储 COS 或第三方源的图片进行处理,提供基础处理能力(图片裁剪、转格式、缩放、打水印等)、图片瘦身能力(Guetzli 压缩、AVIF 转码压缩)、盲水印版权保护能力,同时支持先进的图像 AI 功能(图像增强、图像标签、图像评分、图像修复、商品抠图等),满足多种业务场景下的图片处理需求。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档