| import torch.nn as nn |
| import torch.nn.functional as F |
|
|
|
|
| class CNN(nn.Module): |
| def __init__(self): |
| super(CNN, self).__init__() |
| self.conv1 = nn.Conv2d(3, 32, 3, stride=1, padding=1) |
| self.bn1 = nn.BatchNorm2d(32) |
| self.conv2 = nn.Conv2d(32, 64, 3, stride=1, padding=1) |
| self.bn2 = nn.BatchNorm2d(64) |
| self.conv3 = nn.Conv2d(64, 128, 3, stride=1, padding=1) |
| self.bn3 = nn.BatchNorm2d(128) |
| self.pool = nn.MaxPool2d(stride=2, kernel_size=2) |
| self.fc1 = nn.Linear(128 * 4 * 4, 512) |
| self.fc2 = nn.Linear(512, 10) |
| self.dropout = nn.Dropout(0.5) |
|
|
| def forward(self, x): |
| x = self.pool(F.relu(self.bn1(self.conv1(x)))) |
| x = self.pool(F.relu(self.bn2(self.conv2(x)))) |
| x = self.pool(F.relu(self.bn3(self.conv3(x)))) |
| x = x.view(x.size(0), -1) |
| x = self.dropout(x) |
| x = F.relu(self.fc1(x)) |
| x = self.dropout(x) |
| x = self.fc2(x) |
| return x |
|
|