<jupyter_start><jupyter_text>03-02 Module Containers<jupyter_code>from collections import OrderedDict

import torch
import torch.nn as nn
import torchvision

torch.manual_seed(0)
torch.__version__<jupyter_output><empty_output><jupyter_text>1.按顺序包装多个网络层Sequential<jupyter_code>class LeNet(nn.Module):
    
    def __init__(self, num_classes):
        super(LeNet, self).__init__()
        self.features = nn.Sequential(
            nn.Conv2d(3, 6, 5),
            nn.ReLU(),
            nn.MaxPool2d(kernel_size=2, stride=2),
            nn.Conv2d(6, 16, 5),
            nn.ReLU(),
            nn.MaxPool2d(kernel_size=2, stride=2)
        )

        self.classifier = nn.Sequential(
            nn.Linear(16 * 5 * 5, 120),
            nn.ReLU(),
            nn.Linear(120, 84),
            nn.ReLU(),
            nn.Linear(84, num_classes)
        )

    def forward(self, x):
        x = self.features(x)
        x = x.view(x.size(0), -1)
        x = self.classifier(x)
        return x

inputs = torch.randn((4, 3, 32, 32), dtype=torch.float32)
model = LeNet(num_classes=2)
outputs = model(inputs)
print(model)
print(outputs.shape)
class LeNet(nn.Module):

    def __init__(self, num_classes):
        super(LeNet, self).__init__()
        
        self.features = nn.Sequential(OrderedDict({
            'conv1': nn.Conv2d(3, 6, 5),
            'relu1': nn.ReLU(inplace=True),
            'pool1': nn.MaxPool2d(kernel_size=2, stride=2),

            'conv2': nn.Conv2d(6, 16, 5),
            'relu2': nn.ReLU(inplace=True),
            'pool2': nn.MaxPool2d(kernel_size=2, stride=2),
        }))

        self.classifier = nn.Sequential(OrderedDict({
            'fc1': nn.Linear(16 * 5 * 5, 120),
            'relu3': nn.ReLU(inplace=True),

            'fc2': nn.Linear(120, 84),
            'relu4': nn.ReLU(inplace=True),

            'fc3': nn.Linear(84, num_classes),
        }))

    def forward(self, x):
        x = self.features(x)
        x = x.view(x.size(0), -1)
        x = self.classifier(x)
        return x

inputs = torch.randn((4, 3, 32, 32), dtype=torch.float32)
model = LeNet(num_classes=2)
outputs = model(inputs)
print(model)
print(model.features.conv1)
print(outputs.shape)<jupyter_output>LeNet(
  (features): Sequential(
    (conv1): Conv2d(3, 6, kernel_size=(5, 5), stride=(1, 1))
    (relu1): ReLU(inplace=True)
    (pool1): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
    (conv2): Conv2d(6, 16, kernel_size=(5, 5), stride=(1, 1))
    (relu2): ReLU(inplace=True)
    (pool2): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
  )
  (classifier): Sequential(
    (fc1): Linear(in_features=400, out_features=120, bias=True)
    (relu3): ReLU(inplace=True)
    (fc2): Linear(in_features=120, out_features=84, bias=True)
    (relu4): ReLU(inplace=True)
    (fc3): Linear(in_features=84, out_features=2, bias=True)
  )
)
Conv2d(3, 6, kernel_size=(5, 5), stride=(1, 1))
torch.Size([4, 2])<jupyter_text>2.以迭代方式调用网络层ModuleList<jupyter_code>class TestModule(nn.Module):
    def __init__(self):
        super(TestModule, self).__init__()
        self.linears = nn.ModuleList([nn.Linear(10, 10) for i in range(5)])
    
    def forward(self, x):
        for i, linear in enumerate(self.linears):
            x = linear(x)
        return x

inputs = torch.ones((10, 10))
model = TestModule()
outputs = model(inputs)
print(model)
print(outputs.shape)<jupyter_output>TestModule(
  (linears): ModuleList(
    (0): Linear(in_features=10, out_features=10, bias=True)
    (1): Linear(in_features=10, out_features=10, bias=True)
    (2): Linear(in_features=10, out_features=10, bias=True)
    (3): Linear(in_features=10, out_features=10, bias=True)
    (4): Linear(in_features=10, out_features=10, bias=True)
  )
)
torch.Size([10, 10])<jupyter_text>3.以索引方式调用网络层ModuleDict<jupyter_code>class TestModule(nn.Module):
    def __init__(self):
        super(TestModule, 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, x, choice, act):
        x = self.choices[choice](x)
        x = self.activations[act](x)
        return x

inputs = torch.randn((4, 10, 32, 32))
model = TestModule()
outputs = model(inputs, 'conv', 'relu')
print(model)
print(outputs.shape)<jupyter_output>TestModule(
  (choices): ModuleDict(
    (conv): Conv2d(10, 10, kernel_size=(3, 3), stride=(1, 1))
    (pool): MaxPool2d(kernel_size=3, stride=3, padding=0, dilation=1, ceil_mode=False)
  )
  (activations): ModuleDict(
    (prelu): PReLU(num_parameters=1)
    (relu): ReLU()
  )
)
torch.Size([4, 10, 30, 30])<jupyter_text>4.AlexNet<jupyter_code>model = torchvision.models.AlexNet()
model<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15913274698302313, 'fraction_numerical': 0.04806708938433218, 'mean_word_length': 3.1092436974789917, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '353063', 'license': 'MIT', 'n_tokens_mistral': 1996, 'n_tokens_neox': 1862, 'n_words': 326, 'path': '03-02-ModuleContainers.ipynb', 'repo_name': 'RegiusQuant/PyTorch-Tutorials', 'size': '4889'}