import torch import torch.nn as nn import torch.nn.functional as F class SimpleCNN(nn.Module): def __init__(self): super(SimpleCNN, self).__init__() # Convolutional layers self.conv1 = nn.Conv2d(1, 32, kernel_size=3, stride=1, padding=1) # 28x28 -> 28x28 self.conv2 = nn.Conv2d(32, 64, kernel_size=3, stride=1, padding=1) # 28x28 -> 28x28 self.pool = nn.MaxPool2d(kernel_size=2, stride=2) # 28x28 -> 14x14 self.bn1 = nn.BatchNorm2d(32) self.bn2 = nn.BatchNorm2d(64) # Fully connected layers self.fc1 = nn.Linear(64 * 14 * 14, 128) self.dropout = nn.Dropout(0.5) self.fc2 = nn.Linear(128, 10) def forward(self, x): x = F.relu(self.bn1(self.conv1(x))) # Apply first convolution and ReLU x = self.pool(F.relu(self.bn2(self.conv2(x)))) # Apply second convolution, ReLU, and pooling x = torch.flatten(x, 1) # Flatten the feature maps x = F.relu(self.fc1(x)) # Fully connected layer with ReLU x = self.dropout(x) x = self.fc2(x) # Output layer return x