pytorch_simple_CNN

convolutional neural network (CNN)

using the PyTorch library

model

class CNN(nn.Module):
    def __init__(self, in_channels=1, num_classes=10):
        super(CNN, self).__init__()
        self.conv1 = nn.Conv2d(
            in_channels=in_channels,
            out_channels=8,
            kernel_size=(3, 3),
            stride=(1, 1),
            padding=(1, 1),
        )
        self.pool = nn.MaxPool2d(kernel_size=(2, 2), stride=(2, 2))
        self.conv2 = nn.Conv2d(
            in_channels=8,
            out_channels=16,
            kernel_size=(3, 3),
            stride=(1, 1),
            padding=(1, 1),
        )
        self.fc1 = nn.Linear(16 * 7 * 7, num_classes)

    def forward(self, x):
        x = F.relu(self.conv1(x))
        x = self.pool(x)
        x = F.relu(self.conv2(x))
        x = self.pool(x)
        x = x.reshape(x.shape[0], -1)
        x = self.fc1(x)
        return x

nn.Conv2d() 计算公式
pytorch_simple_CNN
nn.MaxPool
self.pool = nn.MaxPool2d(kernel_size=(2, 2), stride=(2, 2))
pytorch_simple_CNN

#测试代码
model=CNN()
x= torch.randn(64,1,28,28)
print(model(x).shape)
exit()
model = CNN(in_channels=in_channels, num_classes=num_classes).to(device)

其他代码与全连接层网络一致

上一篇:Simple Client Management System未授权的注入漏洞


下一篇:vue3配置路由报错Catch all routes (“*“) must now be defined using a param with a custom regexp.