侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

Pytorch Tensor基本数学运算详解

Python  /  管理员 发布于 5年前   577

1. 加法运算

示例代码:

import torch # 这两个Tensor加减乘除会对b自动进行Broadcastinga = torch.rand(3, 4)b = torch.rand(4) c1 = a + bc2 = torch.add(a, b)print(c1.shape, c2.shape)print(torch.all(torch.eq(c1, c2)))

输出结果:

torch.Size([3, 4]) torch.Size([3, 4])tensor(1, dtype=torch.uint8)

2. 减法运算

示例代码:

a = torch.rand(3, 4)b = torch.rand(4) c1 = a - bc2 = torch.sub(a, b)print(c1.shape, c2.shape)print(torch.all(torch.eq(c1, c2)))

输出结果:

torch.Size([3, 4]) torch.Size([3, 4])tensor(1, dtype=torch.uint8)

3. 哈达玛积(element wise,对应元素相乘)

示例代码:

c1 = a * bc2 = torch.mul(a, b)print(c1.shape, c2.shape)print(torch.all(torch.eq(c1, c2)))

输出结果:

torch.Size([3, 4]) torch.Size([3, 4])tensor(1, dtype=torch.uint8)

4. 除法运算

示例代码:

c1 = a / bc2 = torch.div(a, b)print(c1.shape, c2.shape)print(torch.all(torch.eq(c1, c2)))

输出结果:

torch.Size([3, 4]) torch.Size([3, 4])tensor(1, dtype=torch.uint8)

5. 矩阵乘法

(1)二维矩阵相乘

二维矩阵乘法运算操作包括torch.mm()、torch.matmul()、@,

示例代码:

import torch a = torch.ones(2, 1)b = torch.ones(1, 2)print(torch.mm(a, b).shape)print(torch.matmul(a, b).shape)print((a @ b).shape)

输出结果:

torch.Size([2, 2])torch.Size([2, 2])torch.Size([2, 2])

(2)多维矩阵相乘

对于高维的Tensor(dim>2),定义其矩阵乘法仅在最后的两个维度上,要求前面的维度必须保持一致,就像矩阵的索引一样并且运算操只有torch.matmul()。

示例代码:

c = torch.rand(4, 3, 28, 64)d = torch.rand(4, 3, 64, 32)print(torch.matmul(c, d).shape)

输出结果:

torch.Size([4, 3, 28, 32])

注意,在这种情形下的矩阵相乘,前面的"矩阵索引维度"如果符合Broadcasting机制,也会自动做广播,然后相乘。

示例代码:

c = torch.rand(4, 3, 28, 64)d = torch.rand(4, 1, 64, 32)print(torch.matmul(c, d).shape)

输出结果:

torch.Size([4, 3, 28, 32])

6. 幂运算

示例代码:

import torch a = torch.full([2, 2], 3) b = a.pow(2) # 也可以a**2print(b)

输出结果:

tensor([[9., 9.],    [9., 9.]])

7. 开方运算

示例代码:

c = b.sqrt() # 也可以a**(0.5)print(c) d = b.rsqrt() # 平方根的倒数print(d)

输出结果:

tensor([[3., 3.],    [3., 3.]])tensor([[0.3333, 0.3333],    [0.3333, 0.3333]])

8.指数与对数运算

注意log是以自然对数为底数的,以2为底的用log2,以10为底的用log10

示例代码:

import torch a = torch.exp(torch.ones(2, 2)) # 得到2*2的全是e的Tensorprint(a)print(torch.log(a)) # 取自然对数

输出结果:

tensor([[2.7183, 2.7183],    [2.7183, 2.7183]])tensor([[1., 1.],    [1., 1.]])

9.近似值运算

示例代码:

import torch a = torch.tensor(3.14)print(a.floor(), a.ceil(), a.trunc(), a.frac()) # 取下,取上,取整数,取小数b = torch.tensor(3.49)c = torch.tensor(3.5)print(b.round(), c.round()) # 四舍五入

输出结果:

tensor(3.) tensor(4.) tensor(3.) tensor(0.1400)tensor(3.) tensor(4.)

10. 裁剪运算

即对Tensor中的元素进行范围过滤,不符合条件的可以把它变换到范围内部(边界)上,常用于梯度裁剪(gradient clipping),即在发生梯度离散或者梯度爆炸时对梯度的处理,实际使用时可以查看梯度的(L2范数)模来看看需不需要做处理:w.grad.norm(2)。

示例代码:

import torch grad = torch.rand(2, 3) * 15 # 0~15随机生成print(grad.max(), grad.min(), grad.median()) # 最大值最小值平均值 print(grad)print(grad.clamp(10)) # 最小是10,小于10的都变成10print(grad.clamp(3, 10)) # 最小是3,小于3的都变成3;最大是10,大于10的都变成10

输出结果:

tensor(14.7400) tensor(1.8522) tensor(10.5734)tensor([[ 1.8522, 14.7400, 8.2445],    [13.5520, 10.5734, 12.9756]])tensor([[10.0000, 14.7400, 10.0000],    [13.5520, 10.5734, 12.9756]])tensor([[ 3.0000, 10.0000, 8.2445],    [10.0000, 10.0000, 10.0000]])

以上这篇Pytorch Tensor基本数学运算详解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。


  • 上一条:
    PyTorch中permute的用法详解
    下一条:
    如何利用pygame实现简单的五子棋游戏
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 在python语言中Flask框架的学习及简单功能示例(0个评论)
    • 在Python语言中实现GUI全屏倒计时代码示例(0个评论)
    • Python + zipfile库实现zip文件解压自动化脚本示例(0个评论)
    • python爬虫BeautifulSoup快速抓取网站图片(1个评论)
    • vscode 配置 python3开发环境的方法(0个评论)
    • 近期文章
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 在go + gin中gorm实现指定搜索/区间搜索分页列表功能接口实例(0个评论)
    • 在go语言中实现IP/CIDR的ip和netmask互转及IP段形式互转及ip是否存在IP/CIDR(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2016-11
    • 2018-04
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2022-01
    • 2023-07
    • 2023-10
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客