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

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

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

代码演示:

python 复制代码
#%%
# 加减乘除
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()是对于矩阵的相乘(矩阵相乘的不会的,建议复习一下线性代数)

代码演示:

python 复制代码
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)

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

python 复制代码
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维张量的。

代码演示:

python 复制代码
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次方运算

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

python 复制代码
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

python 复制代码
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():四舍五入

python 复制代码
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梯度裁剪

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

python 复制代码
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

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

相关推荐
风娜19 分钟前
IT 疑难杂症诊疗室:从现象到根因的系统排查方法论
人工智能·github
碧海银沙音频科技研究院21 分钟前
炬芯ATS3089X手表的HFP通话不自动路由到设备BT的问题解决
网络·人工智能·嵌入式硬件·算法
承渊政道25 分钟前
【从零开始大模型开发与微调:基于PyTorch与ChatGLM】(开源大模型ChatGLM高级定制化应用实战)
人工智能·pytorch·python·llm·模型微调·glm
howdoyoudo20260629 分钟前
AI审计手记 #28:从“暂停训练”到“切断联网”——AI安全控制手段的极限升级
大数据·人工智能·安全·ai·语言模型
colman wang1 小时前
人工智能课程(配套AIMA(Artificial Intelligence: A Modern Approach))
人工智能
LinXunFeng5 小时前
Draw Things 换装 LoRA 实战:用 Silo 秒装,同一提示词对比出图
人工智能·开源·aigc
xiaohaiAIgeo5 小时前
【2026年】通风柜自动门安全吗?联锁逻辑与防夹设计
人工智能·安全·科普知识
像风一样自由20205 小时前
52.Docker部署大模型应用从本地开发到服务化运行
运维·人工智能·docker·容器·大模型·智能体
牛战士coding5 小时前
AI Agent 如何接入真实业务系统?订单助手三层架构完整拆解
人工智能·架构