Input to nn.Linear(in_features=16*4*4, out_features=100)
I am performing a CNN on the MNIST dataset with the following model:
class ConvNet(nn.Module):
  def __init__(self, mode):
    super(ConvNet, self).__init__()
    
    # Define various layers here, such as in the tutorial example
    # self.conv1 = nn.Conv2D(...)
    #First Convolution Kayer
    #input size (28,28), output size = (24,24)
    self.conv1 = nn.Conv2d(1,6,5)
    self.reLU1 = nn.ReLU(inplace=True)
    self.MaxPool1 = nn.MaxPool2d(kernel_size=2)

    #Second Convolution Layer
    #input size (12,12), output_size = (8,8)
    self.conv2 = nn.Conv2d(in_channels=6, out_channels=16, kernel_size=5)
    self.reLU2 = nn.ReLU(inplace=True)
    self.MaxPool2 = nn.MaxPool2d(kernel_size=2)
    
    #Affine operations
    self.fc1 = nn.Linear(in_features = 16*4*4, out_features = 100)
    self.sig = torch.nn.Sigmoid()
    self.fc2 = nn.Linear(in_features=100, out_features=10)

My forward pass is defined below.
def forward_pass(self, X):
    #Conv Layer #1
    X = self.conv1(X)
    X = self.reLU1(X)
    X = self.MaxPool1(X)
    #Conv Layer #2
    X = self.conv2(X)
    X = self.reLU2(X)
    X = self.MaxPool2(X)

    print(Tensor.size(X))
    #X = X.view()
    X = self.fc1(X)
    X = self.sig(X)
    X = self.fc2(X)
  
    return X

I get an error when trying to pass the Tensor into the fully connected layer #1 (fc1). This is due to a mismatch in the in_features from my last Convolutional Layers.
When I print out the size of the tensor X before my fully connected layer I get tensor.Size([10,16,4,4]). Can anyone explain to me what would be the proper way to calculate the input to the first fully connected layer?

Your classifier's input is shaped (10, 16, 4, 4), discarding the first dimension which corresponds to the batch size, you end up with 16*4*4 elements. So this is correct, but the shape isn't: you need to flatten the spatial dimension before feeding the tensor to fc1. You can do using nn.Flatten:
class ConvNet(nn.Module):
    def __init__(self, mode):
        super(ConvNet, self).__init__()
        ## layer definitions
        self.flatten = nn.Flatten()

    def forward(self, X):
        ## inference on CNN
        X = self.flatten(X)
        ## inference on fully-connected layers

Here is a inference example:
&gt;&gt;&gt; model = ConvNet(mode=None)
&gt;&gt;&gt; model(torch.rand(10, 1, 24, 24))
torch.Size([10, 10])

Side note please name your function forward instead of forward_pass, it is standard practice.
