Define variable size weight matrix in convolutional neural network

I have defined a convolutional neural network as follows:

class CNN(nn.Module):

    def __init__(self):
    super(CNN, self).__init__()
    self.layer1 = nn.Sequential(
        nn.Conv2d(1, 20, kernel_size=5, padding=2),
        nn.BatchNorm2d(20),
        nn.ReLU(),
        nn.MaxPool2d(2))
    self.layer2 = nn.Sequential(
        nn.Conv2d(20, 20, kernel_size=5, padding=2),
        nn.BatchNorm2d(20),
        nn.ReLU(),
        nn.MaxPool2d(2))
    self.fc = nn.Linear(MxNx20, 10)

    def forward(self, x):
        out = self.layer1(x)
        out = self.layer2(out)
        out = out.view(out.size(0), -1)
        out = self.fc(out)
    return out

cnn = CNN()

In self.fc layer of nn.module i have used M and N in order to define weight matrix size. M and N can take values like 5x5, 7x7, 10x10 and so on. I can not define M and N in terms of self.layer2. Is there any way to predefine size of the weight matrix of self.fc layer