日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【Pytorch神经网络理论篇】 05 Module类的使用方法+参数Parameters类+定义训练模型的步骤与方法

發布時間:2024/7/5 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【Pytorch神经网络理论篇】 05 Module类的使用方法+参数Parameters类+定义训练模型的步骤与方法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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(inputdim=2,hiddendim=3,outputdim=2) optimizer = torch.optim.Adam(model.parameters(),lr=0.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_features=2, out_features=3, bias=True)# Linear(in_features=3, out_features=2, bias=True)# 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_features=2, out_features=3, bias=True)# Linear2 is: Linear(in_features=3, out_features=2, bias=True)# 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_features=2, out_features=3, bias=True)# (Linear2): Linear(in_features=3, out_features=2, bias=True)# (criterion): CrossEntropyLoss()# )model.eval()# 輸出 Linear(in_features=2, out_features=3, bias=True)# Linear(in_features=3, out_features=2, bias=True)# 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_grad=True))])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_grad=True)), ('W2_params', Parameter containing:# tensor([[0.1028, 0.2370, 0.8500],# [0.6116, 0.0463, 0.4229]], requires_grad=True))])# '''def forward(self, x):return x

2.2.2 register_buffer(name,param) 增加狀態參數

代碼:

import torch import torch.nn as nn torch.manual_seed(seed=20200910) class Model(torch.nn.Module):def __init__(self):super(Model,self).__init__()self.conv1=torch.nn.Sequential( # 輸入torch.Size([64, 1, 28, 28])torch.nn.Conv2d(1,64,kernel_size=3,stride=1,padding=1),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.0+cu113 --------------------------------------------初始化之后模型修改之前--------------------------------------------- -----------------------------------------調用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 x+1model = Model() #實例化模型 x = Variable(torch.FloatTensor([1]),requires_grad=True) handle = model.register_forward_hook(for_hook) # 注冊鉤子 print("模型結果",model(x)) # 運行模型 # 輸出 模型: Model() # 輸入: tensor([1.], requires_grad=True) # 輸出: tensor(2., grad_fn=<UnbindBackward0>) # 模型結果 tensor([2.], grad_fn=<AddBackward0>)###刪除鉤子 handle.remove() print("模型結果",model(x)) # 運行模型 # 輸出 模型結果 tensor([2.], grad_fn=<AddBackward0>)

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(inputdim=2,hiddendim=3,outputdim=2) optimizer = torch.optim.Adam(model.parameters(),lr=0.01)### Module類的children()方法===》獲取模型中的各層函數 for sub_module in model.children():print(sub_module)# 輸出 Linear(in_features=2, out_features=3, bias=True)# Linear(in_features=3, out_features=2, bias=True)# CrossEntropyLoss()### Module類的named_children()===>獲取模型中的各層名字與結構信息 for name,module in model.named_children():print(name,"is:",module)# 輸出 Linear1 is: Linear(in_features=2, out_features=3, bias=True)# Linear2 is: Linear(in_features=3, out_features=2, bias=True)# criterion is: CrossEntropyLoss()### Module類的modules()===>獲取整個網絡的結構信息 for module in model.modules():print(module)# 輸出 LogicNet(# (Linear1): Linear(in_features=2, out_features=3, bias=True)# (Linear2): Linear(in_features=3, out_features=2, bias=True)# (criterion): CrossEntropyLoss()# )model.eval()# 輸出 Linear(in_features=2, out_features=3, bias=True)# Linear(in_features=3, out_features=2, bias=True)# 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'}))

總結

以上是生活随笔為你收集整理的【Pytorch神经网络理论篇】 05 Module类的使用方法+参数Parameters类+定义训练模型的步骤与方法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。