深度学习pytorch——基本运算(持续更新)

基本运算——加、减、乘、除

建议直接使用运算符,函数和运算符的效果相同

代码演示:

#%%
# 加减乘除
a = torch.rand(3,4)
b = torch.rand(4)
# 这里a、b可以相加,别忘了pytorch的broadcast机制
print(a+b)
print(torch.add(a,b))
print(torch.all(torch.eq(a-b,torch.sub(a,b))))  # 减
print(torch.all(torch.eq(a*b,torch.mul(a,b))))  # 乘
print(torch.all(torch.eq(a/b,torch.div(a,b))))  # 除

matmul乘 

1、Torch.mm()                # 仅仅适用于2维的

2、Torch.matmul()

3、@                               # 和上一种的效果相同

但是这里应该注意  * 和 .matmul()的区别,* 是对于每个元素对应相乘,.matmul()是对于矩阵的相乘(矩阵相乘的不会的,建议复习一下线性代数)

代码演示:

a = torch.full([2,2],3.)         # 还记得张量的创建吗,没事,我也忘了,现查以前的blog
b = torch.ones(2,2)
print(torch.mm(a,b))            # 矩阵乘法
print(torch.matmul(a,b))        # 矩阵乘法,推荐使用
print(a@b)

可以使用矩阵相乘进行降维处理(用于神经网络的全连接计算):

a = torch.rand(4,784)
x = torch.rand(4,784)
w = torch.rand(512,784)
print((x@w.t()).shape)      # torch.Size([4, 512])

让我们一起探索一下matmul是如何应对4维张量的。

代码演示:

a = torch.rand(4,3,28,64)
b = torch.rand(4,3,64,32)
print(torch.matmul(a,b).shape)      # 只取后2个维度进行计算,torch.Size([4, 3, 28, 32])
b = torch.rand(4,1,64,32)
print(torch.matmul(a,b).shape)      # torch.Size([4, 3, 28, 32]),知道结果为什么这样吗?不要忘记broadcast机制哦
# 但是如果b在变身一下呢?
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
# 为啥?


















# 不符合broadcast机制

power次方运算

接下来我们继续前进,探索次方运算。

a = torch.full([2,2],3)
print(a.pow(2))         # 通过pow()方法进行平方根的计算,推荐这种
print(a**2)             # 也可以通过这种方式进行平方根计算
# 当然,它们都可以扩展到多次方
aa = a**2
print(aa.sqrt())        # 开根号
print(aa.rsqrt())       # 开根号后的倒数
print(aa**(0.5))

 exp log

a = torch.exp(torch.ones(2,2))  # tensor([[2.7183, 2.7183],[2.7183, 2.7183]]) e是多少?
print(torch.log(a))             # tensor([[1., 1.],[1., 1.]]) log默认以e为底

 approximation近似值

floor():floor地板的意思,就是向下取最大的整数

ceil():ceil天花板的意思,就是向上取最小的整数

trunc():裁剪整数部分

frac():裁剪小数部分

round():四舍五入

a = torch.tensor(3.14)
print(a.floor(),a.ceil(),a.trunc(),a.frac())        # tensor(3.) tensor(4.) tensor(3.) tensor(0.1400)
a = torch.tensor(3.499)
print(a.round())                                    # tensor(3.)
a = torch.tensor(3.5)
print(a.round())                                    # tensor(4.)

 clamp梯度裁剪

梯度裁剪适用于梯度零散、和梯度爆炸

grad = torch.rand(2,3)*15
print(grad.max())       # 取最大值
print(grad.median())    # 取中间值
print(grad.clamp(10))   # 小于10的值都变为10
print(grad.clamp(0,10)) # 在0和10之外的值取10

 

 无善无恶心之体,有善有恶意之动,知善知恶是良知,为善去恶是格物。                    ——王阳明

 

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-18 05:36:05       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-18 05:36:05       100 阅读
  3. 在Django里面运行非项目文件

    2024-03-18 05:36:05       82 阅读
  4. Python语言-面向对象

    2024-03-18 05:36:05       91 阅读

热门阅读

  1. HJ31 单词倒排

    2024-03-18 05:36:05       38 阅读
  2. cache的58问,您能回答上几个

    2024-03-18 05:36:05       41 阅读
  3. Python sorted()函数

    2024-03-18 05:36:05       34 阅读
  4. 前端项目构建过程中的思考落地以及拓展

    2024-03-18 05:36:05       35 阅读
  5. MySQL基础知识

    2024-03-18 05:36:05       33 阅读
  6. 基于Python的股票市场分析:趋势预测与策略制定

    2024-03-18 05:36:05       45 阅读
  7. 解决 sh 和 bash 在执行脚本时的差异:双括号问题

    2024-03-18 05:36:05       34 阅读
  8. AJAX-XMLHttpRequest

    2024-03-18 05:36:05       41 阅读
  9. 【前端】CSS常见的选择器

    2024-03-18 05:36:05       36 阅读
  10. K8s 集群高可用master节点ETCD挂掉如何恢复?

    2024-03-18 05:36:05       36 阅读
  11. css常用选择器(一)

    2024-03-18 05:36:05       39 阅读
  12. 机器视觉学习(五)—— 图像的几何

    2024-03-18 05:36:05       37 阅读
  13. 【Unity入门】详解Unity中的射线与射线检测

    2024-03-18 05:36:05       31 阅读