Pytorch框架学习系列(一)-功能模块

官方文档链接:https://pytorch.org/docs/stable/index.html

nn.Sequential

一个顺序容器,模型将会以在构造函数中的顺序被依次添加到容器中,同样,也允许传入有序的模块字典

# Example of using Sequential
model = nn.Sequential(
          nn.Conv2d(1,20,5),
          nn.ReLU(),
          nn.Conv2d(20,64,5),
          nn.ReLU()
        )

# Example of using Sequential with OrderedDict
model = nn.Sequential(OrderedDict([
          ('conv1', nn.Conv2d(1,20,5)),
          ('relu1', nn.ReLU()),
          ('conv2', nn.Conv2d(20,64,5)),
          ('relu2', nn.ReLU())
        ]))

构造网络模型的几种方式

  1. 直接构建
import torch
import torchvision
import torch.nn as nn
import torch.nn.functional as F
from collections import OrderedDict

class Net1(nn.Module):
    def __init__(self):
        super().__init__()
        self.conv1 = nn.Conv2d(3, 32, 1, 1)
        self.fc1 = nn.Linear(32*3*3, 128)
        self.fc2 = nn.Linear(128, 10)

    def forward(self, x):
        x = F.max_pool2d(F.relu(self.conv1(x)), 2)
        x = x.view(x.size(0), -1)
        x = F.relu(self.fc1(x))
        x = self.fc2(x)
        return x

model1 = Net1()
print(model1)
Net1(
  (conv1): Conv2d(3, 32, kernel_size=(1, 1), stride=(1, 1))
  (fc1): Linear(in_features=288, out_features=128, bias=True)
  (fc2): Linear(in_features=128, out_features=10, bias=True)
)
  1. 借助torch.nn.Sequential()容器
class Net2(nn.Module):
    def __init__(self):
        super().__init__()
        self.conv = nn.Sequential(
            nn.Conv2d(3, 32, 1, 1),
            nn.ReLU(),
            nn.MaxPool2d(2)
        )
        self.fc = nn.Sequential(
            nn.Linear(32*3*3, 128),
            nn.ReLU(),
            nn.Linear(128, 10)
        )

    def forward(self, x):
        conv_out = self.conv(x)
        fc_in = conv_out.view(conv_out.size(0), -1)
        out = self.fc(fc_in)
        return out
        
model2 = Net2()
print(model2)
Net2(
  (conv): Sequential(
    (0): Conv2d(3, 32, kernel_size=(1, 1), stride=(1, 1))
    (1): ReLU()
    (2): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
  )
  (fc): Sequential(
    (0): Linear(in_features=288, out_features=128, bias=True)
    (1): ReLU()
    (2): Linear(in_features=128, out_features=10, bias=True)
  )
)
  1. 借助torch.nn.Sequential.add_module() 方法
    该方法是基于torch.nn.Sequential()上可以定义各层网络名称
class Net3(torch.nn.Module):
    def __init__(self):
        super(Net3, self).__init__()
        self.conv=torch.nn.Sequential()
        self.conv.add_module("conv1", torch.nn.Conv2d(3, 32, 3, 1, 1))
        self.conv.add_module("relu1", torch.nn.ReLU())
        self.conv.add_module("pool1", torch.nn.MaxPool2d(2))

        self.fc = torch.nn.Sequential()
        self.fc.add_module("fc1", torch.nn.Linear(32 * 3 * 3, 128))
        self.fc.add_module("relu2", torch.nn.ReLU())
        self.fc.add_module("fc2", torch.nn.Linear(128, 10))

    def forward(self, x):
        conv_out = self.conv(x)
        fc_in = conv_out.view(conv_out.size(0), -1)
        out = self.fc(fc_in)
        return out

model3 = Net3()
print(model3)
Net3(
  (conv): Sequential(
    (conv1): Conv2d(3, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (relu1): ReLU()
    (pool1): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
  )
  (fc): Sequential(
    (fc1): Linear(in_features=288, out_features=128, bias=True)
    (relu2): ReLU()
    (fc2): Linear(in_features=128, out_features=10, bias=True)
  )
)
  1. 在torch.nn.Sequential()中传入有序模块字典
    通过OrderedDict类传入有序字典
class Net4(torch.nn.Module):
    def __init__(self):
        super().__init__()
        self.conv = torch.nn.Sequential(
            OrderedDict(
                [
                    ("conv1", torch.nn.Conv2d(3, 32, 3, 1, 1)),
                    ("relu1", torch.nn.ReLU()),
                    ("pool", torch.nn.MaxPool2d(2))
                ]
            ))

        self.fc = torch.nn.Sequential(
            OrderedDict([
                ("fc1", torch.nn.Linear(32 * 3 * 3, 128)),
                ("relu2", torch.nn.ReLU()),
                ("fc2", torch.nn.Linear(128, 10))
            ])
        )

    def forward(self, x):
        conv_out = self.conv(x)
        fc_in = conv_out.view(conv_out.size(0), -1)
        out = self.fc(fc_in)
        return out
        
model4 = Net4()
print(model4)
Net4(
  (conv): Sequential(
    (conv1): Conv2d(3, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (relu1): ReLU()
    (pool): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
  )
  (fc): Sequential(
    (fc1): Linear(in_features=288, out_features=128, bias=True)
    (relu2): ReLU()
    (fc2): Linear(in_features=128, out_features=10, bias=True)
  )
)
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值