How can I ensure that my Conv1d retains the same shape with unknown sequence lengths?

username: shamoons
cooked: <p>I get the equation:<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/e/a/ea70e56d8d65fdd2d29bbfb6517dfb85768a7456.png" data-download-href="https://discuss.pytorch.org/uploads/default/ea70e56d8d65fdd2d29bbfb6517dfb85768a7456" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/e/a/ea70e56d8d65fdd2d29bbfb6517dfb85768a7456_2_690x153.png" alt="image" data-base62-sha1="xrXBDDawL6GB6b5ItzyCSBkrwzk" width="690" height="153" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/e/a/ea70e56d8d65fdd2d29bbfb6517dfb85768a7456_2_690x153.png, https://discuss.pytorch.org/uploads/default/optimized/3X/e/a/ea70e56d8d65fdd2d29bbfb6517dfb85768a7456_2_1035x229.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/e/a/ea70e56d8d65fdd2d29bbfb6517dfb85768a7456.png 2x" data-dominant-color="F6F6F6"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">1232×274 13.9 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>but it depends on Lin and Lout. I want them to be the same, but when defining my network, I don’t know what my input size will be. In my <code>__init__</code>, I have:</p>
<pre><code class="lang-auto">        l_in = 128
        padding1 = l_in - 1
        padding1 *= strides[0]
        padding1 += 1
        padding1 -= l_in
        padding1 += kernel_sizes[0] - 1
        padding1 /= 2
        print('padding1', padding1)
        padding1 = int(padding1)
        self.conv1 = torch.nn.Conv1d(
            in_channels=feature_dim,
            out_channels=feature_dim,
            kernel_size=kernel_sizes[0],
            stride=strides[0],
            padding=padding1)
</code></pre>
<p>This works well if I know what <code>l_in</code> is. But what if I don’t? For future sequences, I won’t have the length when defining the model.</p>
<p>Is it possible for me to define my padding in a way that’s independent from sequence length?</p>

username: albanD
cooked: <p>Hi,</p>
<p>The usual trick here is that if your stride is 1 an dilation is 1and your kernel has an odd size, you can set the padding to be floor(kernel_size/2).<br>
If you use a stride that is not 1, you won’t be able to remove l_in from the formula above I’m afraid.</p>

username: shamoons
cooked: <p>So then I have to use a stride of 1 if I have unknown lengths?</p>

username: albanD
cooked: <p>Well if you have a stride that is not 1, it is very weird to ask for the output to be the same size no? Or you should ask the output to be input/stride?</p>

username: shamoons
cooked: <p>Maybe I can pad the inputs to fit something before passing to my model?</p>

username: albanD
cooked: <p>For example: If you use a stride of 2 and kernel of 3 for an input of size 10, you get would get according to your formula above that the padding should be 5. That means that your output will contain mostly 0s as you have many applications of your kernel that just end up completely in the padding.</p>

username: shamoons
cooked: <p>Stride of 1 it is <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>

username: albanD
cooked: <p>So for stride of 1, you have:</p>
<pre><code class="lang-auto">padding = dilation * (kernel -1) / 2
</code></pre>
<p>Assuming dilation of 1 as you don’t use it in your code, you have (kernel - 1)/2.<br>
Since pytorch only supports padding as an integer number (what would be non-integer padding?), then you need kernel to be an odd number and you have a padding that can be <code>(kernel - 1)/2</code> <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>