Pytorch Tensor基本数学运算详解

作者:洪流之源 时间:2022-12-09 03:26:50 

1. 加法运算

示例代码:


import torch

# 这两个Tensor加减乘除会对b自动进行Broadcasting
a = torch.rand(3, 4)
b = torch.rand(4)

c1 = a + b
c2 = 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 - b
c2 = 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 * b
c2 = 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 / b
c2 = 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**2
print(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的Tensor
print(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的都变成10
print(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]])

来源:https://blog.csdn.net/weicao1990/article/details/93738722

标签:Pytorch,Tensor
0
投稿

猜你喜欢

  • 百度首席设计师 用户体验部总监郭宇演讲

    2008-09-03 12:41:00
  • Python实现微信自动好友验证,自动回复,发送群聊链接方法

    2021-10-22 00:11:23
  • Oracle学习笔记(一)

    2012-01-05 18:51:44
  • Python闭包和装饰器用法实例详解

    2021-04-07 10:05:02
  • Python修改列表值问题解决方案

    2021-08-02 12:52:39
  • 使用Python和Prometheus跟踪天气的使用方法

    2021-05-15 14:31:05
  • VBScript WeekdayName 函数语法

    2008-01-25 19:25:00
  • Python 蚁群算法详解

    2023-01-03 03:46:25
  • vscode配置与python虚拟环境切换的几种方式总结

    2023-09-04 07:09:28
  • Pytorch上下采样函数之F.interpolate数组采样操作详解

    2022-01-19 08:13:51
  • 在PyCharm中三步完成PyPy解释器的配置的方法

    2021-02-20 04:12:17
  • sql基本查询语句介绍

    2008-05-21 13:58:00
  • 不通过数据源名DSN也能访问Access数据库吗?

    2009-10-29 12:22:00
  • python 快速排序代码

    2022-04-15 00:00:21
  • python模拟预测一下新型冠状病毒肺炎的数据

    2023-12-17 05:09:15
  • php连接不上mysql但mysql命令行操作正常的解决方法

    2023-11-18 20:02:43
  • 利用Python自带PIL库扩展图片大小给图片加文字描述的方法示例

    2022-11-30 20:40:01
  • Python编写单元测试代码实例

    2022-11-02 12:27:09
  • Golang利用自定义模板发送邮件的方法详解

    2023-06-29 07:07:16
  • 如何做一个优秀的设计?

    2009-02-04 15:38:00
  • asp之家 网络编程 m.aspxhome.com