pytorch中常见的模型3种组织方式 nn.Sequential(OrderedDict)

在nn.Sequential中嵌套OrderedDict组织网络,以对层进行命名

import torch
import torch.nn as nn
from collections import OrderedDict

class OrderedDictCNN(nn.Module):
    def __init__(self):
        super(OrderedDictCNN, self).__init__()
        # 使用 OrderedDict 定义网络层
        self.model = nn.Sequential(OrderedDict([
            ('conv1', nn.Conv2d(in_channels=3, out_channels=64, kernel_size=7, stride=2, padding=3)),  # 初始卷积层
            ('bn1', nn.BatchNorm2d(64)),
            ('relu1', nn.ReLU(inplace=True)),
            ('maxpool1', nn.MaxPool2d(kernel_size=3, stride=2, padding=1)),
            
            ('conv2', nn.Conv2d(in_channels=64, out_channels=128, kernel_size=3, stride=1, padding=1)),  # 特征提取层
            ('bn2', nn.BatchNorm2d(128)),
            ('relu2', nn.ReLU(inplace=True)),
            ('maxpool2', nn.MaxPool2d(kernel_size=2, stride=2, padding=0)),
            
            ('flatten', nn.Flatten()),  # 展平层
            ('fc1', nn.Linear(128 * 112 * 112, 1000)),  # 全连接层
            ('relu3', nn.ReLU(inplace=True)),
            ('fc2', nn.Linear(1000, 10))  # 输出层
        ]))
    
    def forward(self, x):
        return self.model(x)

使用多个nn.Sequential组织网络

import torch.nn as nn

class SimpleCNN(nn.Module):
    def __init__(self):
        super(SimpleCNN, self).__init__()
        # 初始卷积层
        self.stem = nn.Sequential(
            nn.Conv2d(in_channels=3, out_channels=64, kernel_size=7, stride=2, padding=3),
            nn.BatchNorm2d(64),
            nn.ReLU(inplace=True)
        )
        # 特征提取层
        self.feature_extraction = nn.Sequential(
            nn.Conv2d(in_channels=64, out_channels=128, kernel_size=3, stride=1, padding=1),
            nn.BatchNorm2d(128),
            nn.ReLU(inplace=True),
            nn.MaxPool2d(kernel_size=2, stride=2, padding=0)
        )
        # 全连接层
        self.fc = nn.Sequential(
            nn.Flatten(),
            nn.Linear(128 * 112 * 112, 1000),
            nn.ReLU(inplace=True),
            nn.Linear(1000, 10)
        )
    
    def forward(self, x):
        x = self.stem(x)
        x = self.feature_extraction(x)
        x = self.fc(x)
        return x

使用单个nn.Sequential组织网络

import torch
import torch.nn as nn

class SequentialCNN(nn.Module):
    def __init__(self):
        super(SequentialCNN, self).__init__()
        # 使用 nn.Sequential 定义网络层
        self.model = nn.Sequential(
            nn.Conv2d(in_channels=3, out_channels=64, kernel_size=7, stride=2, padding=3),  # 初始卷积层
            nn.BatchNorm2d(64),
            nn.ReLU(inplace=True),
            nn.MaxPool2d(kernel_size=3, stride=2, padding=1),
            
            nn.Conv2d(in_channels=64, out_channels=128, kernel_size=3, stride=1, padding=1),  # 特征提取层
            nn.BatchNorm2d(128),
            nn.ReLU(inplace=True),
            nn.MaxPool2d(kernel_size=2, stride=2, padding=0),
            
            nn.Flatten(),  # 展平层
            nn.Linear(128 * 112 * 112, 1000),  # 全连接层
            nn.ReLU(inplace=True),
            nn.Linear(1000, 10)  # 输出层
        )
    
    def forward(self, x):
        return self.model(x)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值