诸城哪里做网站,ipv6网站建设东莞,wordpress淘宝客主题,手机免费制作ppt同学你好#xff01;本文章于2021年末编写#xff0c;获得广泛的好评#xff01;
故在2022年末对本系列进行填充与更新#xff0c;欢迎大家订阅最新的专栏#xff0c;获取基于Pytorch1.10版本的理论代码(2023版)实现#xff0c;
Pytorch深度学习理论篇(2023版)目录地址…同学你好本文章于2021年末编写获得广泛的好评
故在2022年末对本系列进行填充与更新欢迎大家订阅最新的专栏获取基于Pytorch1.10版本的理论代码(2023版)实现
Pytorch深度学习·理论篇(2023版)目录地址为
CSDN独家 | 全网首发 | Pytorch深度学习·理论篇(2023版)目录本专栏将通过系统的深度学习实例从可解释性的角度对深度学习的原理进行讲解与分析通过将深度学习知识与Pytorch的高效结合帮助各位新入门的读者理解深度学习各个模板之间的关系这些均是在Pytorch上实现的可以有效的结合当前各位研究生的研究方向设计人工智能的各个领域是经过一年时间打磨的精品专栏https://v9999.blog.csdn.net/article/details/127587345欢迎大家订阅(2023版)理论篇 以下为2021版原文~~~~ 1 Module类的使用方法
1.1 Module类的add_module()方法
1.1.1 概述 add_module()将XX层插入到模型结构中 1.1.2 add_module()----LogicNet_fun.py第1部分
import torch.nn as nn
import torch
import numpy as np
import matplotlib.pyplot as pltclass LogicNet(nn.Module):def __init__(self,inputdim,hiddendim,outputdim):super(LogicNet, self).__init__()### 方法①self.Linear1 nn.Linear(inputdim,hiddendim)self.Linear2 nn.Linear(hiddendim,outputdim)### 方法②self.add_module(Linear1,nn.Linear(inputdim,hiddendim))self.add_module(Linear2,nn.Linear(hiddendim,outputdim))self.criterion nn.CrossEntropyLoss()def forward(self,x):x self.Linear1(x)
model LogicNet(inputdim2,hiddendim3,outputdim2)
optimizer torch.optim.Adam(model.parameters(),lr0.01)
1.2 Module类的children()方法
1.2.1 children()概述 children()方法获取模型中的各层函数 1.2.2 children()代码实现----LogicNet_fun.py第2部分
### Module类的children()方法》获取模型中的各层函数
for sub_module in model.children():print(sub_module)# 输出 Linear(in_features2, out_features3, biasTrue)# Linear(in_features3, out_features2, biasTrue)# CrossEntropyLoss()
1.3 Module类的named_children()方法
1.3.1named_children()概述 named_children()获取模型中的各层名字与结构信息 1.3.2 named_children()代码实现----LogicNet_fun.py第3部分
### Module类的named_children获取模型中的各层名字与结构信息
for name,module in model.named_children():print(name,is:,module)# 输出 Linear1 is: Linear(in_features2, out_features3, biasTrue)# Linear2 is: Linear(in_features3, out_features2, biasTrue)# criterion is: CrossEntropyLoss()
1.4 Module类的modules()方法
1.4.1 modules()概述 modules()获取整个网络的结构信息 1.4.2 modules()()代码实现----LogicNet_fun.py第4部分
### Module类的modules()获取整个网络的结构信息
for module in model.modules():print(module)# 输出 LogicNet(# (Linear1): Linear(in_features2, out_features3, biasTrue)# (Linear2): Linear(in_features3, out_features2, biasTrue)# (criterion): CrossEntropyLoss()# )model.eval()# 输出 Linear(in_features2, out_features3, biasTrue)# Linear(in_features3, out_features2, biasTrue)# CrossEntropyLoss()
2 模型中的参数Parameters类
2.1 概述
2.1.2 模型与参数的关系 训练过程中模型通过公式的计算结果与目标值进行对比通过调整参数来实现误差的最小化。经过多次调整后的参数可以使得整个模型的结果高度接近于目标值进而得到有效的模型。 2.1.2 Parameter参数的属性 Parameters是Variable类的子类但是存在以下两点不同 ①将Parameter参数赋值给Module的属性时候会将其自动加到Module参数列表中 ②将Variable变量赋值给Module的属性时候不会将其加到Module参数列表中 2.2模型添加参数
2.2.1 register_parameter(name,param) 为模型添加parameter参数
class Example(nn.Module):def __init__(self):super(Example, self).__init__()print(看看我们的模型有哪些parameter:\t, self._parameters, end\n)# 输出 mymodel Example()# # 看看我们的模型有哪些parameter: OrderedDict()self.W1_params nn.Parameter(torch.rand(2,3))print(增加W1后看看,self._parameters, end\n)# 增加W1后看看 OrderedDict([(W1_params, Parameter containing:# tensor([[0.0479, 0.9264, 0.1193],# [0.5004, 0.7336, 0.6464]], requires_gradTrue))])self.register_parameter(W2_params , nn.Parameter(torch.rand(2,3)))print(增加W2后看看,self._parameters, end\n)# 增加W2后看看 OrderedDict([(W1_params, Parameter containing:# tensor([[0.0479, 0.9264, 0.1193],# [0.5004, 0.7336, 0.6464]], requires_gradTrue)), (W2_params, Parameter containing:# tensor([[0.1028, 0.2370, 0.8500],# [0.6116, 0.0463, 0.4229]], requires_gradTrue))])# def forward(self, x):return x 2.2.2 register_buffer(name,param) 增加状态参数
代码
import torch
import torch.nn as nn
torch.manual_seed(seed20200910)
class Model(torch.nn.Module):def __init__(self):super(Model,self).__init__()self.conv1torch.nn.Sequential( # 输入torch.Size([64, 1, 28, 28])torch.nn.Conv2d(1,64,kernel_size3,stride1,padding1),torch.nn.ReLU(), # 输出torch.Size([64, 64, 28, 28]))self.attribute_buffer_in torch.randn(3,5)register_buffer_in_temp torch.randn(4,6)self.register_buffer(register_buffer_in, register_buffer_in_temp)def forward(self,x): passprint(cuda(GPU)是否可用:,torch.cuda.is_available())
print(torch的版本:,torch.__version__)
model Model() #.cuda()print(初始化之后模型修改之前.center(100,-))
print(调用named_buffers().center(100,-))
for name, buf in model.named_buffers():print(name,--,buf.shape)print(调用named_parameters().center(100,-))
for name, param in model.named_parameters():print(name,--,param.shape)print(调用buffers().center(100,-))
for buf in model.buffers():print(buf.shape)print(调用parameters().center(100,-))
for param in model.parameters():print(param.shape)print(调用state_dict().center(100,-))
for k, v in model.state_dict().items():print(k, --, v.shape)model.attribute_buffer_out torch.randn(10,10)
register_buffer_out_temp torch.randn(15,15)
model.register_buffer(register_buffer_out, register_buffer_out_temp)
print(模型初始化以及修改之后.center(100,-))
print(调用named_buffers().center(100,-))
for name, buf in model.named_buffers():print(name,--,buf.shape)print(调用named_parameters().center(100,-))
for name, param in model.named_parameters():print(name,--,param.shape)print(调用buffers().center(100,-))
for buf in model.buffers():print(buf.shape)print(调用parameters().center(100,-))
for param in model.parameters():print(param.shape)print(调用state_dict().center(100,-))
for k, v in model.state_dict().items():print(k, --, v.shape)输出结果
cuda(GPU)是否可用: True
torch的版本: 1.10.0cu113
--------------------------------------------初始化之后模型修改之前---------------------------------------------
-----------------------------------------调用named_buffers()------------------------------------------
register_buffer_in -- torch.Size([4, 6])
----------------------------------------调用named_parameters()----------------------------------------
conv1.0.weight -- torch.Size([64, 1, 3, 3])
conv1.0.bias -- torch.Size([64])
--------------------------------------------调用buffers()---------------------------------------------
torch.Size([4, 6])
-------------------------------------------调用parameters()-------------------------------------------
torch.Size([64, 1, 3, 3])
torch.Size([64])
-------------------------------------------调用state_dict()-------------------------------------------
register_buffer_in -- torch.Size([4, 6])
conv1.0.weight -- torch.Size([64, 1, 3, 3])
conv1.0.bias -- torch.Size([64])
--------------------------------------------模型初始化以及修改之后---------------------------------------------
-----------------------------------------调用named_buffers()------------------------------------------
register_buffer_in -- torch.Size([4, 6])
register_buffer_out -- torch.Size([15, 15])
----------------------------------------调用named_parameters()----------------------------------------
conv1.0.weight -- torch.Size([64, 1, 3, 3])
conv1.0.bias -- torch.Size([64])
--------------------------------------------调用buffers()---------------------------------------------
torch.Size([4, 6])
torch.Size([15, 15])
-------------------------------------------调用parameters()-------------------------------------------
torch.Size([64, 1, 3, 3])
torch.Size([64])
-------------------------------------------调用state_dict()-------------------------------------------
register_buffer_in -- torch.Size([4, 6])
register_buffer_out -- torch.Size([15, 15])
conv1.0.weight -- torch.Size([64, 1, 3, 3])
conv1.0.bias -- torch.Size([64])
2.2.3 对比 缓冲buffer和参数Parameter的区别是前者不需要训练优化,而后者需要训练优化.在创建方法上也有区别,前者必须要将一个张量使用方法register_buffer()来登记注册,后者比较灵活,可以直接赋值给模块的属性,也可以使用方法register_parameter()来登记注册. 3 从模型中获取参数
3.1 使用parameters()方法获取模型的Parameter参数
3.1.1 parameters()方法获取模型的Parameter参数的代码----LogicNet_fun.py第5部分
### 使用parameters()方法获取模型的Parameter参数
for param in model.parameters():print(type(param.data),param.size())# 输出 class torch.Tensor torch.Size([3, 2])# class torch.Tensor torch.Size([3])# class torch.Tensor torch.Size([2, 3])# class torch.Tensor torch.Size([2])
3.2 使用named_parameters()获取模型中的参数和参数名字
3.2.1 使用named_parameters()获取模型中的参数和参数名字----LogicNet_fun.py第6部分
### 使用named_parameters()获取模型中的参数和参数名字
for name, param in model.named_parameters():print(type(param.data),param.size(),name)# 输出 class torch.Tensor torch.Size([3, 2]) Linear1.weight# class torch.Tensor torch.Size([3]) Linear1.bias# class torch.Tensor torch.Size([2, 3]) Linear2.weight# class torch.Tensor torch.Size([2]) Linear2.bias
3.3 state_dict()获取模型的全部参数
3.3.1 state_dict()概述 state_dict()可以将模型中的Parameter和buffer参数取出但不可取出Variable变量 3.3.2 tate_dict()代码实现
import torch
from torch.autograd import Variable
import torch.nn as nnclass ModelPar(nn.Module):def __init__(self):super(ModelPar, self).__init__()self.Line1 nn.Linear(1,2) # 定义全连接层self.var1 Variable(torch.rand([1])) # 定义Variable变量self.par nn.Parameter(torch.rand([1])) # 定义Parameter变量self.register_buffer(buffer,torch.randn([2,3])) # 定义buffer变量model ModelPar()
for par in model.state_dict():print(par,:,model.state_dict()[par])# 输出 par : tensor([0.0285])# buffer : tensor([[-0.7736, 0.7613, 0.5444],[ 1.0695, 0.7132, -0.3575]])# Line1.weight : tensor([[0.7708],[0.6926]])# Line1.bias : tensor([-0.0628, -0.6818])
3.4 为模型中的参数指定名称并查看权重 即通过指定名称的方式对该层的权重进行快速提取 3.4.1 代码实现
import torch
import torch.nn as nn
from collections import OrderedDictmodel nn.Sequential(OrderedDict([(conv1,nn.Conv2d(1,20,5)),(rule1,nn.ReLU()),(conv2,nn.Conv2d(20,64,5)),(relu2,nn.ReLU())])
)
print(model)
# 输出 Sequential(
# (conv1): Conv2d(1, 20, kernel_size(5, 5), stride(1, 1))
# (rule1): ReLU()
# (conv2): Conv2d(20, 64, kernel_size(5, 5), stride(1, 1))
# (relu2): ReLU()
# )params model.state_dict()
print(params[conv1.weight])
print(params[conv1.bias])
4 保存与载入模型
4.1 保存模型载入模型将模型载入指定的硬件设备
4.1.1 代码实现 ----LogicNet_fun.py第7部分
### 保存模型
torch.save(model.state_dict(),./model.path)
### 载入模型
model.load_state_dict(torch.load(./model.path))
### 将模型载入到指定的硬件设备中该方法并不常用
### 为实现细粒度控制 通常将其分解为 1、将模型载入内存。2、使用模型的to()方法将模型复制到指定的设备中
model.load_state_dict(torch.load(./model.path,map_location{cuda:1,cuda:0}))
5 模型结构中的钩子函数
5.1 正向钩子函数概述
5.1.1 模型正向结构中的钩子
模型正向结构中的钩子函数定义
register_forward_hook(hook)hook(module,input,output) #不可修改input和output的数值返回一个句柄
#调用handle的remove()方法可以将hook从module中去除
在module上注册一个forward_hook使得每次调用forward()计算输出的时候这个hook函数就会被调用。
5.1.2 正向结构中的钩子函数的代码实现
import torch
from torch import nn
from torch.autograd import Variabledef for_hook(module,input,output): # 定义钩子函数print(模型,module)for val in input:print(输入,val)for out_val in output :print(输出,out_val)class Model(nn.Module): #定义模型def __init__(self):super(Model, self).__init__()def forward(self,x):return x1model Model() #实例化模型
x Variable(torch.FloatTensor([1]),requires_gradTrue)
handle model.register_forward_hook(for_hook) # 注册钩子
print(模型结果,model(x)) # 运行模型
# 输出 模型 Model()
# 输入 tensor([1.], requires_gradTrue)
# 输出 tensor(2., grad_fnUnbindBackward0)
# 模型结果 tensor([2.], grad_fnAddBackward0)###删除钩子
handle.remove()
print(模型结果,model(x)) # 运行模型
# 输出 模型结果 tensor([2.], grad_fnAddBackward0)5.2 反向钩子函数概述
5.2.1 反向结构中的钩子函数
模型反向结构中的钩子函数定义
register_backward_hook(hook)
### 在module上注册一个backward_hook每次计算module的input的梯度时这个hook就会被调用hook(module,grad_input,grad_output) #不可修改grad_input和grad_outpu的数值返回一个句柄
# 但可以选择性的返回关于输入的梯度返回的梯度会在后续的计算中替换grad_input
#多个输入输出时grad_input,grad_output会是个元组#调用handle的remove()方法可以将hook从module中去除
在module上注册一个backward_hook使得每次计算module的input的梯度时调用hook() 6 LogicNet_fun.py汇总
import torch.nn as nn
import torchclass LogicNet(nn.Module):def __init__(self,inputdim,hiddendim,outputdim):super(LogicNet, self).__init__()### 方法①self.Linear1 nn.Linear(inputdim,hiddendim)self.Linear2 nn.Linear(hiddendim,outputdim)### 方法②self.add_module(Linear1,nn.Linear(inputdim,hiddendim))self.add_module(Linear2,nn.Linear(hiddendim,outputdim))self.criterion nn.CrossEntropyLoss()def forward(self,x):x self.Linear1(x)
model LogicNet(inputdim2,hiddendim3,outputdim2)
optimizer torch.optim.Adam(model.parameters(),lr0.01)### Module类的children()方法》获取模型中的各层函数
for sub_module in model.children():print(sub_module)# 输出 Linear(in_features2, out_features3, biasTrue)# Linear(in_features3, out_features2, biasTrue)# CrossEntropyLoss()### Module类的named_children获取模型中的各层名字与结构信息
for name,module in model.named_children():print(name,is:,module)# 输出 Linear1 is: Linear(in_features2, out_features3, biasTrue)# Linear2 is: Linear(in_features3, out_features2, biasTrue)# criterion is: CrossEntropyLoss()### Module类的modules()获取整个网络的结构信息
for module in model.modules():print(module)# 输出 LogicNet(# (Linear1): Linear(in_features2, out_features3, biasTrue)# (Linear2): Linear(in_features3, out_features2, biasTrue)# (criterion): CrossEntropyLoss()# )model.eval()# 输出 Linear(in_features2, out_features3, biasTrue)# Linear(in_features3, out_features2, biasTrue)# CrossEntropyLoss()### 使用parameters()方法获取模型的Parameter参数
for param in model.parameters():print(type(param.data),param.size())# 输出 class torch.Tensor torch.Size([3, 2])# class torch.Tensor torch.Size([3])# class torch.Tensor torch.Size([2, 3])# class torch.Tensor torch.Size([2])### 使用named_parameters()获取模型中的参数和参数名字
for name, param in model.named_parameters():print(type(param.data),param.size(),name)# 输出 class torch.Tensor torch.Size([3, 2]) Linear1.weight# class torch.Tensor torch.Size([3]) Linear1.bias# class torch.Tensor torch.Size([2, 3]) Linear2.weight# class torch.Tensor torch.Size([2]) Linear2.bias### 保存模型
torch.save(model.state_dict(),./model.path)
### 载入模型
model.load_state_dict(torch.load(./model.path))
### 将模型载入到指定的硬件设备中该方法并不常用
### 常将其分解为 1、将模型载入内存。2、使用模型的to()方法将模型复制到指定的设备中
model.load_state_dict(torch.load(./model.path,map_location{cuda:1,cuda:0}))