nn.Module、nn.Sequential和torch.nn.parameter学习笔记
nn.Module、nn.Sequential和torch.nn.parameter是利用pytorch構建神經(jīng)網(wǎng)絡最重要的三個函數(shù)。搞清他們的具體用法是學習pytorch的必經(jīng)之路。
目錄
- nn.Module
- nn.Sequential
- torch.nn.Parameter
nn.Module
nn.Module中,自定義層的步驟:
1.自定義一個Module的子類,實現(xiàn)兩個基本的函數(shù):
(1)構造 init 函數(shù) (2)層的邏輯運算函數(shù),即正向傳播的函數(shù)
2.在構造 init 函數(shù)中實現(xiàn)層的參數(shù)定義。 比如Linear層的權重和偏置,Conv2d層的in_channels, out_channels, kernel_size, stride=1,padding=0, dilation=1, groups=1,bias=True, padding_mode='zeros’這一系列參數(shù);
3.在forward函數(shù)里實現(xiàn)前向運算。 一般都是通過torch.nn.functional.***函數(shù)來實現(xiàn),如果該層含有權重,那么權重必須是nn.Parameter類型。
4.補充:一般情況下,我們定義的參數(shù)是可以求導的,但是自定義操作如不可導,需要實現(xiàn)backward函數(shù)。
例:
# 定義一個 my_layer.pyimport torchclass MyLayer(torch.nn.Module):'''因為這個層實現(xiàn)的功能是:y=weights*sqrt(x2+bias),所以有兩個參數(shù):權值矩陣weights偏置矩陣bias輸入 x 的維度是(in_features,)輸出 y 的維度是(out_features,) 故而bias 的維度是(in_fearures,),注意這里為什么是in_features,而不是out_features,注意體會這里和Linear層的區(qū)別所在weights 的維度是(in_features, out_features)注意這里為什么是(in_features, out_features),而不是(out_features, in_features),注意體會這里和Linear層的區(qū)別所在'''def __init__(self, in_features, out_features, bias=True):super(MyLayer, self).__init__() # 和自定義模型一樣,第一句話就是調(diào)用父類的構造函數(shù)self.in_features = in_featuresself.out_features = out_featuresself.weight = torch.nn.Parameter(torch.Tensor(in_features, out_features)) # 由于weights是可以訓練的,所以使用Parameter來定義if bias:self.bias = torch.nn.Parameter(torch.Tensor(in_features)) # 由于bias是可以訓練的,所以使用Parameter來定義else:self.register_parameter('bias', None)def forward(self, input):input_=torch.pow(input,2)+self.biasy=torch.matmul(input_,self.weight)return y自定義模型并訓練import torchfrom my_layer import MyLayer # 自定義層N, D_in, D_out = 10, 5, 3 # 一共10組樣本,輸入特征為5,輸出特征為3 # 先定義一個模型class MyNet(torch.nn.Module):def __init__(self):super(MyNet, self).__init__() # 第一句話,調(diào)用父類的構造函數(shù)self.mylayer1 = MyLayer(D_in,D_out)def forward(self, x):x = self.mylayer1(x)return xmodel = MyNet()print(model)'''運行結果為:MyNet((mylayer1): MyLayer() # 這就是自己定義的一個層)'''下面開始訓練# 創(chuàng)建輸入、輸出數(shù)據(jù)x = torch.randn(N, D_in) #(10,5)y = torch.randn(N, D_out) #(10,3)#定義損失函數(shù)loss_fn = torch.nn.MSELoss(reduction='sum')learning_rate = 1e-4#構造一個optimizer對象optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)for t in range(10): # # 第一步:數(shù)據(jù)的前向傳播,計算預測值p_predy_pred = model(x)# 第二步:計算計算預測值p_pred與真實值的誤差loss = loss_fn(y_pred, y)print(f"第 {t} 個epoch, 損失是 {loss.item()}")# 在反向傳播之前,將模型的梯度歸零,這optimizer.zero_grad()# 第三步:反向傳播誤差loss.backward()# 直接通過梯度一步到位,更新完整個網(wǎng)絡的訓練參數(shù)optimizer.step()nn.Sequential
torch.nn.Sequential是一個Sequential容器,模塊將按照構造函數(shù)中傳遞的順序添加到模塊中。
與nn.Module相同,nn.Sequential也是用來構建神經(jīng)網(wǎng)絡的,但nn.Sequential不需要像nn.Module那么多過程,可以快速構建神經(jīng)網(wǎng)絡。
使用nn.Module可以根據(jù)自己的需求改變傳播過程。
這個構建網(wǎng)絡的方法工作量略有減少,但有特殊需求的網(wǎng)絡還是要用nn.Module。
torch.nn.Parameter
torch.nn.Parameter是繼承自torch.Tensor的子類,其主要作用是作為nn.Module中的可訓練參數(shù)使用。它與torch.Tensor的區(qū)別就是nn.Parameter會自動被認為是module的可訓練參數(shù),即加入到parameter()這個迭代器中去;而module中非nn.Parameter()的普通tensor是不在parameter中的。
nn.Parameter的對象的requires_grad屬性的默認值是True,即是可被訓練的,這與torh.Tensor對象的默認值相反。
在nn.Module類中,pytorch也是使用nn.Parameter來對每一個module的參數(shù)進行初始化的。
例:
顯然,在__init__(self, in_features, out_features, bias=True)中,下面的代碼使用了nn.Parameter()對weights進行了初始化
參考:
https://blog.csdn.net/qq_27825451/article/details/90705328
https://blog.csdn.net/qq_28753373/article/details/104179354
總結
以上是生活随笔為你收集整理的nn.Module、nn.Sequential和torch.nn.parameter学习笔记的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ccd视觉定位教程_CCD视觉定位的激光
- 下一篇: 生产者消费者案例