-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCNN_Class.py
More file actions
52 lines (45 loc) · 1.56 KB
/
Copy pathCNN_Class.py
File metadata and controls
52 lines (45 loc) · 1.56 KB
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
import torch.nn as nn
class CNN(nn.Module):
def __init__(self, dropout_rate=0.25):
super(CNN, self).__init__()
# Initial number of channels increased
self.conv1 = nn.Conv2d(1, 64, kernel_size=3, stride=1, padding=1)
self.bn1 = nn.BatchNorm2d(64)
self.conv2 = nn.Conv2d(64, 128, kernel_size=3, stride=1, padding=1)
self.bn2 = nn.BatchNorm2d(128)
self.conv3 = nn.Conv2d(128, 256, kernel_size=3, stride=1, padding=1)
self.bn3 = nn.BatchNorm2d(256)
self.relu = nn.LeakyReLU(0.1) # Changed to LeakyReLU
self.pool = nn.MaxPool2d(kernel_size=2, stride=2)
self.dropout = nn.Dropout(dropout_rate)
# Adjusted fully connected layers
self.fc1 = nn.Linear(256 * 3 * 3, 512)
self.bn4 = nn.BatchNorm1d(512)
self.fc2 = nn.Linear(512, 10)
def forward(self, x):
# First conv block
x = self.conv1(x)
x = self.bn1(x)
x = self.relu(x)
x = self.pool(x)
x = self.dropout(x)
# Second conv block
x = self.conv2(x)
x = self.bn2(x)
x = self.relu(x)
x = self.pool(x)
x = self.dropout(x)
# Third conv block
x = self.conv3(x)
x = self.bn3(x)
x = self.relu(x)
x = self.pool(x)
x = self.dropout(x)
# Flatten and FC layers
x = x.view(-1, 256 * 3 * 3)
x = self.fc1(x)
x = self.bn4(x)
x = self.relu(x)
x = self.dropout(x)
x = self.fc2(x)
return x