-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheight_layer_CNN.py
59 lines (48 loc) · 2.11 KB
/
eight_layer_CNN.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
from torch.utils.data import Dataset, DataLoader, random_split
class Medium_CNN(nn.Module):
def __init__(self, num_classes):
super(Medium_CNN, self).__init__()
#SIMPLER, 8-layer model: 6 conv layers and 2 fully connected layers. works better on smaller dataset!
self.pool = nn.MaxPool2d(kernel_size=3, stride=1, padding =1)
self.conv_1= nn.Conv2d(1, 16, kernel_size=5, stride=3, padding=2)
self.conv_2 = nn.Conv2d(16, 32, kernel_size=3, stride=3, padding=2)
self.conv_3 = nn.Conv2d(32, 32, kernel_size=3, stride=3, padding=0)
self.conv_4 = nn.Conv2d(32, 64, kernel_size=3, stride=3, padding=1)
self.conv_5 = nn.Conv2d(64, 64, kernel_size=3, stride=1, padding=1)
self.conv_6 = nn.Conv2d(64, 64, kernel_size=3, stride=1, padding=1)
self.FCL_1 = nn.Linear(256, 100)
self.dropout_1 = nn.Dropout(0.4)
self.FCL_2 = nn.Linear(100, num_classes)
# Defining the forward pass
def forward(self, input):
#CONV LAYERS ---------------------------------------------------------------
#1ST conv layer
output = self.conv_1(input)
output = F.relu(output)
output = self.pool(output)
#2nd conv layer
output = self.conv_2(output)
output = F.relu(output)
output = self.pool(output)
#3rd conv layer
output = self.conv_3(output)
output = F.relu(output)
output = self.pool(output)
#4th conv layer
output = self.conv_4(output)
output = F.relu(output)
output = self.pool(output)
#DROPOUT + FCL LAYERS ------------------------------------------------------
#1st dropout + FCL layer
output = output.view(output.size(0), -1) #doesn't work if not flattened!!
output = self.FCL_1(output)
#2nd dropout + FCL layer
output = self.dropout_1(output)
output = self.FCL_2(output)
#SOFTMAX--------------------------------------------------------------------
output = F.log_softmax(output, dim=1)
return output