ConvTranspose2d output size

kluk123: <p>Hello!</p>
<p>I am new to PyTorch and I am at the moment building my first ever GAN network.<br>
While choosing the proper layer architecture I have noticed some behavior that I cannot understand and I am really interested in the inner-working of this function.</p>
<p>Let’s say, that I have got a batch of data looking like this:</p>
<pre><code class="lang-python">input_data = torch.randn(64, 100, 1, 1)
</code></pre>
<p>As I understand, it can be interpreted as a set of 64 pictures, of height=100 pixels, width=1 pixel in a grayscale.<br>
I want to use this random data to generate some images, so I insert it into a network in which the first layer is a ConvTranspose2d. This layer requires the size of the input data to be specified and the size of the output data as well. It looks strange for me, because I thought, that the size of an output image is determined by the size of an input image, stride, and padding. However, the layer canfit the output into the given size.</p>
<p><strong>Example 1:</strong></p>
<pre><code class="lang-python">conv = ConvTranspose2d(100, 512, 4, 1, 0)
output = conv(input_data)
print(output.size())
</code></pre>
<pre><code class="lang-python">torch.Size([64, 512, 4, 4])
</code></pre>
<p><strong>Example 2:</strong></p>
<pre><code class="lang-python">conv = ConvTranspose2d(100, 3157, 4, 1, 0)
output = conv(input_data)
print(output.size())
</code></pre>
<pre><code class="lang-python">torch.Size([64, 3157, 4, 4])
</code></pre>
<p>Probably, I do not understand some of the Convolutional Transpose layer’s mechanics but I find it very interesting and I wonder whether someone knows a simple answer to the question:<br>
How does the ConvTranspose2d fit the output into an arbitrary number of output channels?</p>
<p>I will be grateful for your help <img src="https://discuss.pytorch.org/images/emoji/apple/wink.png?v=9" title=":wink:" class="emoji" alt=":wink:"></p>

ptrblck: <aside class="quote no-group" data-username="kluk123" data-post="1" data-topic="104475">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/k/90ced4/48.png" class="avatar"> kluk123:</div>
<blockquote>
<p>As I understand, it can be interpreted as a set of 64 pictures, of height=100 pixels, width=1 pixel in a grayscale.</p>
</blockquote>
</aside>
<p>No, PyTorch uses the NCHW memory format (channels-first), so your input would have 64 channels and a single pixel.</p>
<aside class="quote no-group" data-username="kluk123" data-post="1" data-topic="104475">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/k/90ced4/48.png" class="avatar"> kluk123:</div>
<blockquote>
<p>This layer requires the size of the input data to be specified and the size of the output data as well.</p>
</blockquote>
</aside>
<p>That’s not the case and you would need to define e.g. the <code>in_channels</code>, <code>out_channels</code>, and <code>kernel_size</code>.</p>
<aside class="quote no-group" data-username="kluk123" data-post="1" data-topic="104475">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/k/90ced4/48.png" class="avatar"> kluk123:</div>
<blockquote>
<p>How does the ConvTranspose2d fit the output into an arbitrary number of output channels?</p>
</blockquote>
</aside>
<p>Each output channels is created by a filter kernel, so you can define this number arbitrarily.</p>
<p>For a general overview of the conv / transposed conv arithmetic, have a look at <a href="https://arxiv.org/abs/1603.07285">this tutorial</a>.</p>

kluk123: <p>Thank You very much, this explains a lot. Especially the linked tutorial comes in handy.<br>
Now I understand the problem I had with channels.<br>
Thank You one more time.</p>