關于 Pytorch 幾種定義網(wǎng)絡的方法
點擊上方“機器學習與生成對抗網(wǎng)絡”,關注星標
獲取有趣、好玩的前沿干貨!
來源:知乎—ppgod
地址:https://zhuanlan.zhihu.com/p/80308275
import torchimport torch.nn as nnfrom torch.autograd import Variablefrom collections import OrderedDictclass Net(nn.Module):def __init__(self):super(Net, self).__init__()self.fc1 = nn.Linear(10,10)self.relu1 = nn.ReLU(inplace=True)self.fc2 = nn.Linear(10,2)def forward(self,x):x = self.fc1(x)x = self.relu1(x)x = self.fc2(x)return x
這是最簡單的定義一個網(wǎng)絡的方法,但是當網(wǎng)絡層數(shù)過多的時候,這么寫未免太麻煩,于是Pytorch還有第二種定義網(wǎng)絡的方法nn.ModuleList()
class Net(nn.Module):def __init__(self):super(Net, self).__init__()self.base = nn.ModuleList([nn.Linear(10,10), nn.ReLU(), nn.Linear(10,2)])def forward(self,x):x = self.base(x)return x
base = [nn.Linear(10,10) for i in range(5)]net = nn.ModuleList(base)
class Net(nn.Module):def __init__(self):super(Net, self).__init__()self.base = nn.Sequential(nn.Linear(10,10), nn.ReLU(), nn.Linear(10,2))def forward(self,x):x = self.base(x)return x
class MultiLayerNN5(nn.Module):def __init__(self):super(MultiLayerNN5, self).__init__()self.base = nn.Sequential(OrderedDict([('0', BasicConv(1, 16, 5, 1, 2)),('1', BasicConv(16, 32, 5, 1, 2)),]))self.fc1 = nn.Linear(32 * 7 * 7, 10)def forward(self, x):x = self.base(x)x = x.view(x.size(0), -1)x = self.fc1(x)return x
class MultiLayerNN4(nn.Module):def __init__(self):super(MultiLayerNN4, self).__init__()self.base = nn.Sequential()self.base.add_module('0', BasicConv(1, 16, 5, 1, 2))self.base.add_module('1', BasicConv(16, 32, 5, 1, 2))self.fc1 = nn.Linear(32 * 7 * 7, 10)def forward(self, x):x = self.base(x)x = x.view(x.size(0),-1)x = self.fc1(x)
tt = [nn.Linear(10,10), nn.Linear(10,2)]n_1 = nn.Sequential(*tt)n_2 = nn.ModuleList(tt)x = torch.rand([1,10,10])x = Variable(x)n_1(x)n_2(x)#會出現(xiàn)NotImplementedError
class DenseLayer(nn.Sequential):def __init__(self):super(DenseLayer, self).__init__()self.add_module("conv1", nn.Conv2d(1, 1, 1, 1, 0))self.add_module("conv2", nn.Conv2d(1, 1, 1, 1, 0))def forward(self, x):new_features = super(DenseLayer, self).forward(x)return torch.cat([x, new_features], 1)#這個寫法和下面的是一樣的class DenLayer1(nn.Module):def __init__(self):super(DenLayer1, self).__init__()convs = [nn.Conv2d(1, 1, 1, 1, 0), nn.Conv2d(1, 1, 1, 1, 0)]self.conv = nn.Sequential(*convs)def forward(self, x):return torch.cat([x, self.conv(x)], 1)net = DenLayer1()x = torch.Tensor([[[[1, 2], [3, 4]]]])print(x)x = Variable(x)print(net(x))
猜您喜歡:
CVPR 2021 | GAN的說話人驅動、3D人臉論文匯總
CVPR 2021生成對抗網(wǎng)絡GAN部分論文匯總
附下載 | 經(jīng)典《Think Python》中文版
附下載 |《TensorFlow 2.0 深度學習算法實戰(zhàn)》
附下載 | 超100篇!CVPR 2020最全GAN論文梳理匯總!
評論
圖片
表情
