參考: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)示例:
-
class FeatureL2Norm(torch.nn.Module):
-
def __init__(self):
-
super(FeatureL2Norm, self).__init__()
-
-
def forward(self, feature):
-
epsilon = 1e-6
-
# print(feature.size())
-
# print(torch.pow(torch.sum(torch.pow(feature,2),1)+epsilon,0.5).size())
-
norm = torch.pow(torch.sum(torch.pow(feature, 2),1)+epsilon,0.5).unsqueeze(1).expand_as(feature)
-
return torch.div(feature,norm)
-
class FeatureRegression(nn.Module):
-
def __init__(self, output_dim=6, use_cuda=True):
-
super(FeatureRegression, self).__init__()
-
self.conv = nn.Sequential(
-
nn.Conv2d( 225, 128, kernel_size=7, padding=0),
-
nn.BatchNorm2d( 128),
-
nn.ReLU(inplace=True),
-
nn.Conv2d( 128, 64, kernel_size=5, padding=0),
-
nn.BatchNorm2d( 64),
-
nn.ReLU(inplace=True),
-
)
-
self.linear = nn.Linear(64 * 5 * 5, output_dim)
-
if use_cuda:
-
self.conv.cuda()
-
self.linear.cuda()
-
-
def forward(self, x):
-
x = self.conv(x)
-
x = x.view(x.size( 0), -1)
-
x = self.linear(x)
-
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__方法部分源碼如下所示:
-
def __call__(self, *input, **kwargs):
-
result = self.forward(*input, **kwargs)
-
for hook in self._forward_hooks.values():
-
#將注冊的hook拿出來用
-
hook_result = hook( self, input, result)
-
...
-
return result
可以看到,當執行model(x)的時候,底層自動調用forward方法計算結果。具體示例如下:
-
class Function:
-
def __init__(self):
-
...
-
def forward(self, inputs):
-
...
-
return outputs
-
def backward(self, grad_outs):
-
...
-
return grad_ins
-
def _backward(self, grad_outs):
-
hooked_grad_outs = grad_outs
-
for hook in hook_in_outputs:
-
hooked_grad_outs = hook(hooked_grad_outs)
-
grad_ins = self.backward(hooked_grad_outs)
-
hooked_grad_ins = grad_ins
-
for hook in hooks_in_module:
-
hooked_grad_ins = hook(hooked_grad_ins)
-
return hooked_grad_ins
model = LeNet()
y = model(x)
如上則調用網絡模型定義的forward方法。