There are formulas for this. You can see a worked out example here:

  
      stats.stackexchange.com
  
  
      
    
  

  Does padding ensure that the sizes of input and output of a CNN match? 850



  machine-learning, neural-networks, conv-neural-network



  asked by
  
  
    Pinocchio
  
  on 11:31PM - 30 Jul 19 UTC


  
  
    
    
  
  


also here is a reference for arithmetic of convolutions:

  
      
      arXiv.org
  
  
    

A guide to convolution arithmetic for deep learning 144

We introduce a guide to help deep learning practitioners understand and
manipulate convolutional neural network architectures. The guide clarifies the
relationship between various properties (input shape, kernel shape, zero
padding, strides and...


  
  
    
    
  
  



Though I agree that this so staandard that Pytorch should just have a layer or an option that calculates all this once for the user. It seems silly and repetitive that we have to think about this every time we want to use same convolution. Would be nice if the code was already ready and provided for us, specially for standard things like this.


  
    
    
    How do I make sure the output of a CNN never decrease in size using padding? 
  
  
    I want to create one code such that no matter what task or input size the image is, it will always produce a ConvNet such that the padding is set such that the size of the features is always equal to the size of the original image (up until the end/final layer). 
How does one do that in pytorch? 

I realized this might be useful: