PyTorch之前向传播函数自动调用forward
參考:1.?pytorch學(xué)習(xí)筆記(九):PyTorch結(jié)構(gòu)介紹
2.pytorch學(xué)習(xí)筆記(七):pytorch hook 和 關(guān)于pytorch backward過程的理解
3.Pytorch入門學(xué)習(xí)(三):Neural Networks
4.forward
神經(jīng)網(wǎng)絡(luò)的典型處理如下所示:
1. 定義可學(xué)習(xí)參數(shù)的網(wǎng)絡(luò)結(jié)構(gòu)(堆疊各層和層的設(shè)計(jì));
2. 數(shù)據(jù)集輸入;
3. 對(duì)輸入進(jìn)行處理(由定義的網(wǎng)絡(luò)層進(jìn)行處理),主要體現(xiàn)在網(wǎng)絡(luò)的前向傳播;
4. 計(jì)算loss ,由Loss層計(jì)算;
5. 反向傳播求梯度;
6. 根據(jù)梯度改變參數(shù)值,最簡單的實(shí)現(xiàn)方式(SGD)為:
???weight = weight - learning_rate * gradient
下面是利用PyTorch定義深度網(wǎng)絡(luò)層(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
由上例代碼可以看到,不論是在定義網(wǎng)絡(luò)結(jié)構(gòu)還是定義網(wǎng)絡(luò)層的操作(Op),均需要定義forward函數(shù),下面看一下PyTorch官網(wǎng)對(duì)PyTorch的forward方法的描述:
那么調(diào)用forward方法的具體流程是什么樣的呢?具體流程是這樣的:
以一個(gè)Module為例:
1. 調(diào)用module的call方法
2. module的call里面調(diào)用module的forward方法
3. forward里面如果碰到Module的子類,回到第1步,如果碰到的是Function的子類,繼續(xù)往下
4. 調(diào)用Function的call方法
5. Function的call方法調(diào)用了Function的forward方法。
6. Function的forward返回值
7. module的forward返回值
8. 在module的call進(jìn)行forward_hook操作,然后返回值
上述中“調(diào)用module的call方法”是指nn.Module 的__call__方法。定義__call__方法的類可以當(dāng)作函數(shù)調(diào)用,具體參考Python的面向?qū)ο缶幊?。也就是說,當(dāng)把定義的網(wǎng)絡(luò)模型model當(dāng)作函數(shù)調(diào)用的時(shí)候就自動(dòng)調(diào)用定義的網(wǎng)絡(luò)模型的forward方法。nn.Module 的__call__方法部分源碼如下所示:
?
- def __call__(self, *input, **kwargs):
- result = self.forward(*input, **kwargs)
- for hook in self._forward_hooks.values():
- #將注冊(cè)的hook拿出來用
- hook_result = hook(self, input, result)
- ...
- return result
可以看到,當(dāng)執(zhí)行model(x)的時(shí)候,底層自動(dòng)調(diào)用forward方法計(jì)算結(jié)果。具體示例如下:
- 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)
如上則調(diào)用網(wǎng)絡(luò)模型定義的forward方法。
總結(jié)
以上是生活随笔為你收集整理的PyTorch之前向传播函数自动调用forward的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: pytorch 调用forward 的具
- 下一篇: Pytorch 多 GPU 并行处理机制