Pytorch

pytorch框架学习(7)——模型创建与nn.Module

2020-02-03  本文已影响0人  aidanmomo

@[toc]

1. 网络模型创建步骤

机器学习模型训练主要分为以下5个步骤,今天主要学习其中的模型部分

在这里插入图片描述

2. nn.Module

nn.torch是pytorch中神经网络模块,其中包含如下比较重要的四个子模块:

3. 模型容器Containers

class ModuleList(nn.Module):
    def __init__(self):
        super(ModuleList, self).__init__()
        self.linears = nn.ModuleList([nn.Linear(10, 10) for i in range(20)])
    
    def forward(self, x):
        for i, linear in enumerate(self.linears):
            x = linears(x)
        return x

net = ModuleList()
class ModuleDict(nn.Module):
    def __init__(self):
        super(ModuleDict, self).__init__()
        self.choices = nn.ModuleDict({
            'conv' : nn.Conv2d(10, 10, 3),
            'pool' : nn.MaxPool2d(3)
            })
        self.activations = nn.ModuleDict({
            'relu' : nn.ReLU(),
            'prelu' : nn.PReLU()
            })
    def forward(self, c, choice, act):
        x = self.choices[choice](x)
        x = self.activations[act](x)
        return x

net = ModuleDict()

fake_img = torch.randn((4, 10, 32, 32)) # 模拟数据

output = net(fake_img, 'conv', 'prelu')

print(output)

4. AlexNet网络构建

class AlexNet(nn.Module):

    def __init__(self, num_classes=1000):
        super(AlexNet, self).__init__()
        self.features = nn.Sequential(
            nn.Conv2d(3, 64, kernel_size=11, stride=4, padding=2),
            nn.ReLU(inplace=True),
            nn.MaxPool2d(kernel_size=3, stride=2),
            nn.Conv2d(64, 192, kernel_size=5, padding=2),
            nn.ReLU(inplace=True),
            nn.MaxPool2d(kernel_size=3, stride=2),
            nn.Conv2d(192, 384, kernel_size=3, padding=1),
            nn.ReLU(inplace=True),
            nn.Conv2d(384, 256, kernel_size=3, padding=1),
            nn.ReLU(inplace=True),
            nn.Conv2d(256, 256, kernel_size=3, padding=1),
            nn.ReLU(inplace=True),
            nn.MaxPool2d(kernel_size=3, stride=2),
        )
        self.avgpool = nn.AdaptiveAvgPool2d((6, 6))
        self.classifier = nn.Sequential(
            nn.Dropout(),
            nn.Linear(256 * 6 * 6, 4096),
            nn.ReLU(inplace=True),
            nn.Dropout(),
            nn.Linear(4096, 4096),
            nn.ReLU(inplace=True),
            nn.Linear(4096, num_classes),
        )

    def forward(self, x):
        x = self.features(x)
        x = self.avgpool(x)
        x = torch.flatten(x, 1)
        x = self.classifier(x)
        return x

上一篇下一篇

猜你喜欢

热点阅读