Consider the convolutional neural network (two convolutional layers):
class ConvNet(nn.Module):
def __init__(self, num_classes=10):
super(ConvNet, self).__init__()
self.layer1 = nn.Sequential(
nn.Conv2d(1, 16, kernel_size=5, stride=1, padding=2),
nn.BatchNorm2d(16),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2, stride=2))
self.layer2 = nn.Sequential(
nn.Conv2d(16, 32, kernel_size=5, stride=1, padding=2),
nn.BatchNorm2d(32),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2, stride=2))
self.fc = nn.Linear(7*7*32, num_classes)
def forward(self, x):
out = self.layer1(x)
out = self.layer2(out)
out = out.reshape(out.size(0), -1)
out = self.fc(out)
return out
The fully connected layer fc
is to have 7*7*32
inputs coming in. The above:
out = out.reshape(out.size(0), -1)
leads to a tensor with size of (32, 49)
.
This doesn't seem right as the dimensions of input for the dense layer is different. What am I missing here?
[Note that in Pytorch the input is in the following format: [N, C, W, H] so no. of channels comes before the width and height of image]