PyTorch之前向傳播函數自動調用forward


參考:1. pytorch學習筆記(九):PyTorch結構介紹

2.pytorch學習筆記(七):pytorch hook 和 關於pytorch backward過程的理解

3.Pytorch入門學習(三):Neural Networks

4.forward

神經網絡的典型處理如下所示:

1. 定義可學習參數的網絡結構(堆疊各層和層的設計);
2. 數據集輸入;
3. 對輸入進行處理(由定義的網絡層進行處理),主要體現在網絡的前向傳播;
4. 計算loss ,由Loss層計算;
5. 反向傳播求梯度;
6. 根據梯度改變參數值,最簡單的實現方式(SGD)為:
   weight = weight - learning_rate * gradient
下面是利用PyTorch定義深度網絡層(Op)示例:
 

  1.  
    class FeatureL2Norm(torch.nn.Module):
  2.  
    def __init__(self):
  3.  
    super(FeatureL2Norm, self).__init__()
  4.  
     
  5.  
    def forward(self, feature):
  6.  
    epsilon = 1e-6
  7.  
    # print(feature.size())
  8.  
    # print(torch.pow(torch.sum(torch.pow(feature,2),1)+epsilon,0.5).size())
  9.  
    norm = torch.pow(torch.sum(torch.pow(feature, 2),1)+epsilon,0.5).unsqueeze(1).expand_as(feature)
  10.  
    return torch.div(feature,norm)
  1.  
    class FeatureRegression(nn.Module):
  2.  
    def __init__(self, output_dim=6, use_cuda=True):
  3.  
    super(FeatureRegression, self).__init__()
  4.  
    self.conv = nn.Sequential(
  5.  
    nn.Conv2d( 225, 128, kernel_size=7, padding=0),
  6.  
    nn.BatchNorm2d( 128),
  7.  
    nn.ReLU(inplace=True),
  8.  
    nn.Conv2d( 128, 64, kernel_size=5, padding=0),
  9.  
    nn.BatchNorm2d( 64),
  10.  
    nn.ReLU(inplace=True),
  11.  
    )
  12.  
    self.linear = nn.Linear(64 * 5 * 5, output_dim)
  13.  
    if use_cuda:
  14.  
    self.conv.cuda()
  15.  
    self.linear.cuda()
  16.  
     
  17.  
    def forward(self, x):
  18.  
    x = self.conv(x)
  19.  
    x = x.view(x.size( 0), -1)
  20.  
    x = self.linear(x)
  21.  
    return x

由上例代碼可以看到,不論是在定義網絡結構還是定義網絡層的操作(Op),均需要定義forward函數,下面看一下PyTorch官網對PyTorch的forward方法的描述:

那么調用forward方法的具體流程是什么樣的呢?具體流程是這樣的:

以一個Module為例:
1. 調用module的call方法
2. module的call里面調用module的forward方法
3. forward里面如果碰到Module的子類,回到第1步,如果碰到的是Function的子類,繼續往下
4. 調用Function的call方法
5. Function的call方法調用了Function的forward方法。
6. Function的forward返回值
7. module的forward返回值
8. 在module的call進行forward_hook操作,然后返回值
上述中“調用module的call方法”是指nn.Module 的__call__方法。定義__call__方法的類可以當作函數調用,具體參考Python的面向對象編程。也就是說,當把定義的網絡模型model當作函數調用的時候就自動調用定義的網絡模型的forward方法。nn.Module 的__call__方法部分源碼如下所示:
 

  1.  
    def __call__(self, *input, **kwargs):
  2.  
    result = self.forward(*input, **kwargs)
  3.  
    for hook in self._forward_hooks.values():
  4.  
    #將注冊的hook拿出來用
  5.  
    hook_result = hook( self, input, result)
  6.  
    ...
  7.  
    return result

可以看到,當執行model(x)的時候,底層自動調用forward方法計算結果。具體示例如下:

  1.  
    class Function:
  2.  
    def __init__(self):
  3.  
    ...
  4.  
    def forward(self, inputs):
  5.  
    ...
  6.  
    return outputs
  7.  
    def backward(self, grad_outs):
  8.  
    ...
  9.  
    return grad_ins
  10.  
    def _backward(self, grad_outs):
  11.  
    hooked_grad_outs = grad_outs
  12.  
    for hook in hook_in_outputs:
  13.  
    hooked_grad_outs = hook(hooked_grad_outs)
  14.  
    grad_ins = self.backward(hooked_grad_outs)
  15.  
    hooked_grad_ins = grad_ins
  16.  
    for hook in hooks_in_module:
  17.  
    hooked_grad_ins = hook(hooked_grad_ins)
  18.  
    return hooked_grad_ins

model = LeNet()
y = model(x)
如上則調用網絡模型定義的forward方法。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM