How to make CNN class work when training and test data are of different dimensions

I have set up the following CNN class to train sequences of length 2000 and dim = 3.
Now, I need to test this model to predict test sequences, which are of different length = 1950, but same dim = 3. I hard-coded the in_features and out_features in fully-connected layers:

    self.fc1 = nn.Linear(2500, 2300) 
    self.fc2 = nn.Linear(2300, 2100)
    self.fc3 = nn.Linear(2100, 2000)

Thus, it doesn’t work if I need to predict sequences of different length, as numbers will be different. Any suggestions how I can make the model work without hard-coding these numbers, so it works for different number of in_features and out_features?

class my_CNN(nn.Module):
    def __init__(self):
         super(my_CNN, self).__init__()
    
        self.conv1 = nn.Conv2d(1, 10, 2, padding = (1,1))
        self.relu1 = nn.ReLU(inplace=True)
        self.maxpool1 = nn.MaxPool2d(2, 2)
        
        self.conv2 = nn.Conv2d(10, 15, 2, padding = (1,1))
        self.relu2 = nn.ReLU(inplace=True)
        self.maxpool2 = nn.MaxPool2d(2, 2)
        
        self.fc1 = nn.Linear(2500, 2300) 
        self.fc2 = nn.Linear(2300, 2100)
        self.fc3 = nn.Linear(2100, 2000)
        self.relu3 = nn.ReLU(inplace=True)
        self.relu4 = nn.ReLU(inplace=True)


       def forward(self, x):
        out1 = self.relu1(self.conv1(x))
        out2 = self.maxpool1(out1)
        out3 = self.relu2(self.conv2(out2))
        out4 = self.maxpool2(out3)
        
        out = out4.view(out4.size(0), 1, 3, -1)
        
        out = self.relu3(self.fc1(out))
        out = self.relu4(self.fc2(out))
        out = self.fc3(out)
 
        return out

List item

Yes, classifier networks using fc layers don’t work on variable sized images. This is generally true regardless the framework. The most commonly used option is to simply resize the test images to be similar to train images. On a slightly related note, you can use even fancier methods like 5-crop to enhance your test score. http://pytorch.org/docs/master/torchvision/transforms.html#torchvision.transforms.FiveCrop

@SimonW, thank you for your answer.