Output_size when using nn.Sequential

username: aroibu1
cooked: <p>Hi! In my network, I am trying to carry out upsampling from an encoded latent space. So far I have been using nearest upsampling, but I want to switch to transpose convolutions as this could aid in my training.</p>
<p>The way I wish to define my upsampling operation, to keep it consistent with the rest of my code, is as follows:</p>
<pre><code class="lang-auto">a = nn.Sequential(
            OrderedDict(
                [
                    (
                        'test' + "conv1",
                        nn.ConvTranspose3d(
                            in_channels=1,
                            out_channels=1,
                            kernel_size=3,
                            stride=2,
                            padding=padding,
                            bias= False,
                        ),
                    ),
                    (
                        'test' + 'normalization1',
                        normalization,
                    ),
                    (
                        'test' + 'nonlinearity1',
                        nonlinearity,
                    ),
                ]
            )
        )
</code></pre>
<p>A typical input to the upsampling operation would be of shape <code>(1,1,5,6,5)</code> with the output being of shape <code>(1,1,10,12,10)</code>. However, due to the convolution kernel size, the output that I am getting is of shape <code>(1,1,9,11,9)</code>.</p>
<p>Now comes my question: I read that I can use an argument <code>output_size=desired_shape</code> in the forward call of the ConvTranspose to get the right dimension. This works when I use it solely on the ConvTranspose3d, but raises a <code>TypeError: forward() got an unexpected keyword argument 'output_size'</code> when I try using it on teh Sequential block defined above. So, does anybody know if and how I could fix this issue, while still retaining my Sequential block approach? Cheers!</p>

username: aroibu1
cooked: <p>Does anybody have any ideas regarding this? Sorry, but it has been two weeks since my original post and I am still struggling with this. Cheers!</p>

username: ptrblck
cooked: <p>If the <code>output_size</code> argument is fixed for each layer, you could create a custom module and set this argument during its initialization:</p>
<pre><code class="lang-python">class MyConvTranspose3d(nn.Module):
    def __init__(self, conv, output_size=None):
        super().__init__()
        self.conv = conv
        self.output_size = output_size
        
    def forward(self, x):
        x = self.conv(x, output_size=self.output_size)
        return x


model = nn.Sequential(
    MyConvTranspose3d(
        nn.ConvTranspose3d(
            in_channels=1,
            out_channels=1,
            kernel_size=3,
            stride=2,
            padding=1,
            bias= False,
        ),
        output_size=(10, 12, 10)
    )
)

x = torch.randn(1, 1, 5, 6, 5)
out = model(x)
print(out.shape)
&gt; torch.Size([1, 1, 10, 12, 10])
</code></pre>
<p>Otherwise, if this argument depends on the input shape, I would recommend to use a custom module and not an <code>nn.Sequential</code> container for the needed flexibility.</p>