当前位置: 首页 > news >正文

PyTorch 张量与自动微分操作

笔记

1 张量索引操作

import torch
​
# 下标从左到右从0开始(0->第一个值), 从右到左从-1开始
# data[行下标, 列下标]
# data[0轴下标, 1轴下标, 2轴下标]
​
def dm01():# 创建张量torch.manual_seed(0)data = torch.randint(low=0, high=10, size=(4, 5))print('data->', data)# 根据下标值获取对应位置的元素# 行数据 第一行print('data[0] ->', data[0])# 列数据 第一列print('data[:, 0]->', data[:, 0])# 根据下标列表取值# 第二行第三列的值和第四行第五列值print('data[[1, 3], [2, 4]]->', data[[1, 3], [2, 4]])# [[1], [3]: 第二行第三列 第二行第五列值   第四行第三列 第四行第五列值print('data[[[1], [3]], [2, 4]]->', data[[[1], [3]], [2, 4]])# 根据布尔值取值# 第二列大于6的所有行数据print(data[:, 1] > 6)print('data[data[:, 1] > 6]->', data[data[:, 1] > 6])# 第三行大于6的所有列数据print('data[:, data[2]>6]->', data[:, data[2] > 6])# 根据范围取值  切片  [起始下标:结束下标:步长]# 第一行第三行以及第二列第四列张量print('data[::2, 1::2]->', data[::2, 1::2])
​# 创建三维张量data2 = torch.randint(0, 10, (3, 4, 5))print("data2->", data2)# 0轴第一个值print(data2[0, :, :])# 1轴第一个值print(data2[:, 0, :])# 2轴第一个值print(data2[:, :, 0])
​
​
if __name__ == '__main__':dm01()

2 张量形状操作

2.1 reshape

import torch
​
​
# reshape(shape=(行,列)): 修改连续或非连续张量的形状, 不改数据
# -1: 表示自动计算行或列   例如:  (5, 6) -> (-1, 3) -1*3=5*6 -1=10  (10, 3)
def dm01():torch.manual_seed(0)t1 = torch.randint(0, 10, (5, 6))print('t1->', t1)print('t1的形状->', t1.shape)# 形状修改为 (2, 15)t2 = t1.reshape(shape=(2, 15))t3 = t1.reshape(shape=(2, -1))print('t2->', t2)print('t2的形状->', t2.shape)print('t3->', t3)print('t3的形状->', t3.shape)
​
​
​
if __name__ == '__main__':dm01()

2.2 squeeze和unsqueeze

# squeeze(dim=): 删除值为1的维度, dim->指定维度, 维度值不为1不生效  不设置dim,删除所有值为1的维度
# 例如: (3,1,2,1) -> squeeze()->(3,2)  squeeze(dim=1)->(3,2,1)
# unqueeze(dim=): 在指定维度上增加值为1的维度  dim=-1:最后维度
def dm02():torch.manual_seed(0)# 四维t1 = torch.randint(0, 10, (3, 1, 2, 1))print('t1->', t1)print('t1的形状->', t1.shape)# squeeze: 降维t2 = torch.squeeze(t1)print('t2->', t2)print('t2的形状->', t2.shape)# dim: 指定维度t3 = torch.squeeze(t1, dim=1)print('t3->', t3)print('t3的形状->', t3.shape)# unsqueeze: 升维# (3, 2)->(1, 3, 2)# t4 = t2.unsqueeze(dim=0)# 最后维度 (3, 2)->(3, 2, 1)t4 = t2.unsqueeze(dim=-1)print('t4->', t4)print('t4的形状->', t4.shape)
​
​
if __name__ == '__main__':dm02()

2.3 transpose和permute

# 调换维度
# torch.permute(input=,dims=): 改变张量任意维度顺序
# input: 张量对象
# dims: 改变后的维度顺序, 传入轴下标值 (1,2,3)->(3,1,2)
# torch.transpose(input=,dim0=,dim1=): 改变张量两个维度顺序
# dim0: 轴下标值, 第一个维度
# dim1: 轴下标值, 第二个维度
# (1,2,3)->(2,1,3) 一次只能交换两个维度
def dm03():torch.manual_seed(0)t1 = torch.randint(low=0, high=10, size=(3, 4, 5))print('t1->', t1)print('t1形状->', t1.shape)# 交换0维和1维数据# t2 = t1.transpose(dim0=1, dim1=0)t2 = t1.permute(dims=(1, 0, 2))print('t2->', t2)print('t2形状->', t2.shape)# t1形状修改为 (5, 3, 4)t3 = t1.permute(dims=(2, 0, 1))print('t3->', t3)print('t3形状->', t3.shape)
​
​
if __name__ == '__main__':dm03()

2.4 view和contiguous

# tensor.view(shape=): 修改连续张量的形状, 操作等同于reshape()
# tensor.is_contiugous(): 判断张量是否连续, 返回True/False  张量经过transpose/permute处理变成不连续
# tensor.contiugous(): 将张量转为连续张量
def dm04():torch.manual_seed(0)t1 = torch.randint(low=0, high=10, size=(3, 4))print('t1->', t1)print('t1形状->', t1.shape)print('t1是否连续->', t1.is_contiguous())# 修改张量形状t2 = t1.view((4, 3))print('t2->', t2)print('t2形状->', t2.shape)print('t2是否连续->', t2.is_contiguous())# 张量经过transpose操作t3 = t1.transpose(dim0=1, dim1=0)print('t3->', t3)print('t3形状->', t3.shape)print('t3是否连续->', t3.is_contiguous())# 修改张量形状# view# contiugous(): 转换成连续张量t4 = t3.contiguous().view((3, 4))print('t4->', t4)t5 = t3.reshape(shape=(3, 4))print('t5->', t5)print('t5是否连续->', t5.is_contiguous())
​
​
if __name__ == '__main__':dm04()

3 张量拼接操作

3.1 cat/concat

import torch
​
​
# torch.cat()/concat(tensors=, dim=): 在指定维度上进行拼接, 其他维度值必须相同, 不改变新张量的维度, 指定维度值相加
# tensors: 多个张量列表
# dim: 拼接维度
def dm01():torch.manual_seed(0)t1 = torch.randint(low=0, high=10, size=(2, 3))t2 = torch.randint(low=0, high=10, size=(2, 3))t3 = torch.cat(tensors=[t1, t2], dim=0)print('t3->', t3)print('t3形状->', t3.shape)t4 = torch.concat(tensors=[t1, t2], dim=1)print('t4->', t4)print('t4形状->', t4.shape)​
if __name__ == '__main__':# dm01()

3.2 stack

# torch.stack(tensors=, dim=): 根据指定维度进行堆叠, 在指定维度上新增一个维度(维度值张量个数), 新张量维度发生改变
# tensors: 多个张量列表
# dim: 拼接维度
def dm02():torch.manual_seed(0)t1 = torch.randint(low=0, high=10, size=(2, 3))t2 = torch.randint(low=0, high=10, size=(2, 3))t3 = torch.stack(tensors=[t1, t2], dim=0)# t3 = torch.stack(tensors=[t1, t2], dim=1)print('t3->', t3)print('t3形状->', t3.shape)
​
​
if __name__ == '__main__':dm02()

4 自动微分模块

4.1 梯度计算

"""
梯度: 求导,求微分 上山下山最快的方向
梯度下降法: W1=W0-lr*梯度   lr是可调整已知参数  W0:初始模型的权重,已知  计算出W0的梯度后更新到W1权重
pytorch中如何自动计算梯度 自动微分模块
注意点: ①loss标量和w向量进行微分  ②梯度默认累加,计算当前的梯度, 梯度值是上次和当前次求和  ③梯度存储.grad属性中
"""
import torch
​
​
def dm01():# 创建标量张量 w权重# requires_grad: 是否自动微分,默认False# dtype: 自动微分的张量元素类型必须是浮点类型# w = torch.tensor(data=10, requires_grad=True, dtype=torch.float32)# 创建向量张量 w权重w = torch.tensor(data=[10, 20], requires_grad=True, dtype=torch.float32)# 定义损失函数, 计算损失值loss = 2 * w ** 2print('loss->', loss)print('loss.sum()->', loss.sum())# 计算梯度 反向传播  loss必须是标量张量,否则无法计算梯度loss.sum().backward()# 获取w权重的梯度值print('w.grad->', w.grad)w.data = w.data - 0.01 * w.gradprint('w->', w)
​
​
if __name__ == '__main__':dm01()

4.2 梯度下降法求最优解

"""
① 创建自动微分w权重张量
② 自定义损失函数 loss=w**2+20  后续无需自定义,导入不同问题损失函数模块
③ 前向传播 -> 先根据上一版模型计算预测y值, 根据损失函数计算出损失值
④ 反向传播 -> 计算梯度
⑤ 梯度更新 -> 梯度下降法更新w权重
"""
import torch
​
​
def dm01():# ① 创建自动微分w权重张量w = torch.tensor(data=10, requires_grad=True, dtype=torch.float32)print('w->', w)# ② 自定义损失函数 后续无需自定义, 导入不同问题损失函数模块loss = w ** 2 + 20print('loss->', loss)# 0.01 -> 学习率print('开始 权重x初始值:%.6f (0.01 * w.grad):无 loss:%.6f' % (w, loss))for i in range(1, 1001):# ③ 前向传播 -> 先根据上一版模型计算预测y值, 根据损失函数计算出损失值loss = w ** 2 + 20# 梯度清零 -> 梯度累加, 没有梯度默认Noneif w.grad is not None:w.grad.zero_()# ④ 反向传播 -> 计算梯度loss.sum().backward()# ⑤ 梯度更新 -> 梯度下降法更新w权重# W = W - lr * W.grad# w.data -> 更新w张量对象的数据, 不能直接使用w(将结果重新保存到一个新的变量中)w.data = w.data - 0.01 * w.gradprint('w.grad->', w.grad)print('次数:%d 权重w: %.6f, (0.01 * w.grad):%.6f loss:%.6f' % (i, w, 0.01 * w.grad, loss))
​print('w->', w, w.grad, 'loss最小值', loss)
​
​
if __name__ == '__main__':dm01()

4.3 梯度计算注意点

# 自动微分的张量不能转换成numpy数组, 可以借助detach()方法生成新的不自动微分张量
import torch
​
​
def dm01():x1 = torch.tensor(data=10, requires_grad=True, dtype=torch.float32)print('x1->', x1)# 判断张量是否自动微分 返回True/Falseprint(x1.requires_grad)# 调用detach()方法对x1进行剥离, 得到新的张量,不能自动微分,数据和原张量共享x2 = x1.detach()print(x2.requires_grad)print(x1.data)print(x2.data)print(id(x1.data))print(id(x2.data))# 自动微分张量转换成numpy数组n1 = x2.numpy()print('n1->', n1)
​
​
if __name__ == '__main__':dm01()

4.4 自动微分模块应用

import torch
import torch.nn as nn  # 损失函数,优化器函数,模型函数
​
​
def dm01():# todo:1-定义样本的x和yx = torch.ones(size=(2, 5))y = torch.zeros(size=(2, 3))print('x->', x)print('y->', y)# todo:2-初始模型权重 w b 自动微分张量w = torch.randn(size=(5, 3), requires_grad=True)b = torch.randn(size=(3,), requires_grad=True)print('w->', w)print('b->', b)# todo:3-初始模型,计算预测y值y_pred = torch.matmul(x, w) + bprint('y_pred->', y_pred)# todo:4-根据MSE损失函数计算损失值# 创建MSE对象, 类创建对象criterion = nn.MSELoss()loss = criterion(y_pred, y)print('loss->', loss)# todo:5-反向传播,计算w和b梯度loss.sum().backward()print('w.grad->', w.grad)print('b.grad->', b.grad)
​
​
if __name__ == '__main__':dm01()

相关文章:

  • 全球化电商平台Azure云架构设计
  • 期末代码Python
  • iptables的基本选项及概念
  • 串 Part 1
  • 数据链路层(MAC 地址)
  • Gemini 解释蓝图节点的提示词
  • STC单片机与淘晶驰串口屏通讯例程之04【密码登录与修改】
  • 有哪些场景不适合使用Java反射机制
  • 基于C++实现的深度学习(cnn/svm)分类器Demo
  • H3C无线控制器自动信道功率调整典型配置实验
  • 数据结构小扫尾——栈
  • JAVA:使用 Maven Assembly 创建自定义打包的技术指南
  • Kubernetes(k8s)学习笔记(七)--KubeSphere 最小化安装
  • 音频感知动画新纪元:Sonic让你的作品更生动
  • 矩阵置零(中等)
  • 五一假期集训【补题】
  • 研0大模型学习(第12天)
  • 【C++】智能指针RALL实现shared_ptr
  • android-ndk开发(1): 搭建环境
  • 基于SpringBoot的漫画网站设计与实现
  • 云南禄丰一尾矿干堆场坍塌致5人被埋
  • 上千游客深夜滞留张家界大喊退票?景区:已采取措施限制人流量
  • 传奇落幕!波波维奇卸任马刺队主教练,转型全职球队总裁
  • 本周看啥|《乘风》迎来师姐们,《天赐》王蓉搭Ella
  • 贵州赤水丹霞大瀑布附近山体塌方车辆被埋,景区:无伤亡,道路已恢复
  • A股2024年年报披露收官,四分之三公司盈利