import torch.nn as nn

class MyCNN(nn.Module): def init(self): super(MyCNN, self).init() self.conv1 = nn.Conv2d(in_channels=3, out_channels=96, kernel_size=11, stride=4, padding=2) self.relu1 = nn.ReLU() self.pool1 = nn.MaxPool2d(kernel_size=3, stride=2) self.conv2 = nn.Conv2d(in_channels=96, out_channels=256, kernel_size=5, stride=1, padding=2) self.relu2 = nn.ReLU() self.pool2 = nn.MaxPool2d(kernel_size=3, stride=2) self.conv3 = nn.Conv2d(in_channels=256, out_channels=384, kernel_size=3, stride=1, padding=1) self.relu3 = nn.ReLU() self.conv4 = nn.Conv2d(in_channels=384, out_channels=256, kernel_size=3, stride=1, padding=1) self.relu4 = nn.ReLU() self.pool3 = nn.MaxPool2d(kernel_size=3, stride=2) self.flatten = nn.Flatten() self.fc1 = nn.Linear(in_features=43264, out_features=4096) self.relu5 = nn.ReLU() self.fc2 = nn.Linear(in_features=4096, out_features=1000)

def forward(self, x):
    x = self.conv1(x)
    print('Conv2d output shape: 	', x.shape)
    x = self.relu1(x)
    print('ReLU output shape: 	', x.shape)
    x = self.pool1(x)
    print('MaxPool2d output shape: 	', x.shape)
    x = self.conv2(x)
    print('Conv2d output shape: 	', x.shape)
    x = self.relu2(x)
    print('ReLU output shape: 	', x.shape)
    x = self.pool2(x)
    print('MaxPool2d output shape: 	', x.shape)
    x = self.conv3(x)
    print('Conv2d output shape: 	', x.shape)
    x = self.relu3(x)
    print('ReLU output shape: 	', x.shape)
    x = self.conv4(x)
    print('Conv2d output shape: 	', x.shape)
    x = self.relu4(x)
    print('ReLU output shape: 	', x.shape)
    x = self.pool3(x)
    print('MaxPool2d output shape: 	', x.shape)
    x = self.flatten(x)
    print('Flatten output shape: 	', x.shape)
    x = self.fc1(x)
    print('Linear output shape: 	', x.shape)
    x = self.relu5(x)
    print('ReLU output shape: 	', x.shape)
    x = self.fc2(x)
    print('Linear output shape: 	', x.shape)
    return x

model = MyCNN() print(model)

卷积神经网络(CNN)设计:AlexNet架构解析

原文地址: https://www.cveoy.top/t/topic/nYYz 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录