| 2 | |
| 3 | |
| 4 | class CNNModel(nn.Module): |
| 5 | def __init__(self,config): |
| 6 | super(CNNModel, self).__init__() |
| 7 | |
| 8 | # Convolution 1 |
| 9 | self.cnn1 = nn.Conv2d(in_channels=1, out_channels=config.channels_one, kernel_size=5, stride=1, padding=0) |
| 10 | self.relu1 = nn.ReLU() |
| 11 | # Max pool 1 |
| 12 | self.maxpool1 = nn.MaxPool2d(kernel_size=2) |
| 13 | |
| 14 | # Convolution 2 |
| 15 | self.cnn2 = nn.Conv2d(in_channels=config.channels_one, out_channels=config.channels_two, kernel_size=5, stride=1, padding=0) |
| 16 | self.relu2 = nn.ReLU() |
| 17 | |
| 18 | # Max pool 2 |
| 19 | self.maxpool2 = nn.MaxPool2d(kernel_size=2) |
| 20 | |
| 21 | self.dropout = nn.Dropout(p=config.dropout) |
| 22 | |
| 23 | # Fully connected 1 (readout) |
| 24 | self.fc1 = nn.Linear(config.channels_two*4*4, 10) |
| 25 | |
| 26 | def forward(self, x): |
| 27 | # Convolution 1 |
| 28 | out = self.cnn1(x) |
| 29 | out = self.relu1(out) |
| 30 | |
| 31 | # Max pool 1 |
| 32 | out = self.maxpool1(out) |
| 33 | |
| 34 | # Convolution 2 |
| 35 | out = self.cnn2(out) |
| 36 | out = self.relu2(out) |
| 37 | |
| 38 | # Max pool 2 |
| 39 | out = self.maxpool2(out) |
| 40 | |
| 41 | # Resize |
| 42 | # Original size: (100, 32, 7, 7) |
| 43 | # out.size(0): 100 |
| 44 | # New out size: (100, 32*7*7) |
| 45 | out = out.view(out.size(0), -1) |
| 46 | out = self.dropout(out) |
| 47 | # Linear function (readout) |
| 48 | out = self.fc1(out) |
| 49 | |
| 50 | return out |