Working of nn.nn.AdaptiveAvgPool2d()?

username: krishna511
cooked: <pre><code class="lang-auto">A=torch.rand(8,64,6,15)
m = nn.AdaptiveAvgPool2d(1)
o=m(A)
o.shape
&gt;&gt;torch.Size([8, 64, 1, 1])
</code></pre>
<p>Here what I am grasping is every <code>6*15</code> will be average to get a single value. Then</p>
<pre><code class="lang-auto">A=torch.rand(8,64,6,15)
m = nn.AdaptiveAvgPool2d(2)
o=m(A)
o.shape
&gt;&gt;torch.Size([8, 64, 2, 2])
</code></pre>
<p>Here the output shape indicates that every <code>5*14</code> grid is average to get this shape <code> 2*2</code>?<br>
So the output size is denoted in kernel??<br>
Am I right ?</p>

username: Epoching
cooked: <p>Looks correct to me according to <a href="https://stackoverflow.com/a/58694174" class="inline-onebox" rel="noopener nofollow ugc">python - What is Adaptive average pooling and How does it work? - Stack Overflow</a> :</p>
<pre><code class="lang-auto">Stride = (input_size//output_size)  
Kernel size = input_size - (output_size-1)*stride  
Padding = 0
</code></pre>
<aside class="onebox stackexchange" data-onebox-src="https://stackoverflow.com/questions/58692476/what-is-adaptive-average-pooling-and-how-does-it-work/58694174#58694174">
  <header class="source">

      <a href="https://stackoverflow.com/questions/58692476/what-is-adaptive-average-pooling-and-how-does-it-work/58694174#58694174" target="_blank" rel="noopener nofollow ugc">stackoverflow.com</a>
  </header>

  <article class="onebox-body">
      <a href="https://stackoverflow.com/users/12266923/anant-mittal" target="_blank" rel="noopener nofollow ugc">
    <img alt="Anant Mittal" src="https://i.stack.imgur.com/REOdp.jpg?s=128&amp;g=1" class="thumbnail onebox-avatar" width="128" height="128">
  </a>

<h4>
  <a href="https://stackoverflow.com/questions/58692476/what-is-adaptive-average-pooling-and-how-does-it-work/58694174#58694174" target="_blank" rel="noopener nofollow ugc">What is Adaptive average pooling and How does it work?</a>
</h4>

<div class="tags">
  <strong>python, math, neural-network, deep-learning</strong>
</div>

<div class="date">
  
  answered by
  <a href="https://stackoverflow.com/users/12266923/anant-mittal" target="_blank" rel="noopener nofollow ugc">
    Anant Mittal
  </a>
  on <a href="https://stackoverflow.com/questions/58692476/what-is-adaptive-average-pooling-and-how-does-it-work/58694174#58694174" target="_blank" rel="noopener nofollow ugc">01:13PM - 04 Nov 19 UTC</a>
</div>

  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>


username: krishna511
cooked: <p>Could u plz explain these three lines, I guess for second example stride is 1, is there any explicit way to mention stride, or is this the formula to calculate stride.</p>

username: Epoching
cooked: <p>For <code>nn.AdaptiveAvgPool2d</code>, you just specify the output size, stride will always be 1 (every possible grid will be used to achieve the output size):</p>
<p><code>torch.nn.AdaptiveAvgPool2d(output_size)</code> according to <a href="https://pytorch.org/docs/stable/generated/torch.nn.AdaptiveAvgPool2d.html?highlight=adapative%20avg%20pool" rel="noopener nofollow ugc">AdaptiveAvgPool2d — PyTorch 1.9.0 documentation</a></p>
<p>The formulas above I believe is to calculate kernel size given input size, output size, and stride, (i.e. the size of the grids) <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>

username: krishna511
cooked: <p>Overall My answers are correct, actually there is no need for this much confusion here, The output size given in function only, which is required after avg pooling that’s it.</p>

username: Epoching
cooked: <p>Nice! Yea sorry if I misunderstood anything/made things confusing!</p>

username: Epoching
cooked: <blockquote>
<p>Here the output shape indicates that every <code>5*14</code> grid is average to get this shape <code>2*2</code> ?<br>
Am I right ?</p>
</blockquote>
<p>Completely correct <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=12" title=":slight_smile:" class="emoji" alt=":slight_smile:" loading="lazy" width="20" height="20"></p>
<p>Also just noticed I linked to <code>AvgPool2d</code> rather than <code>AdaptiveAvgPool2d</code>: <a href="https://pytorch.org/docs/stable/generated/torch.nn.AdaptiveAvgPool2d.html?highlight=adapative%20avg%20pool" rel="noopener nofollow ugc">AdaptiveAvgPool2d — PyTorch 1.9.0 documentation</a>. Sorry about that (just edited the reply)</p>
<blockquote>
<p>The output size given in function only, which is required after avg pooling that’s it.</p>
</blockquote>
<p>Exactly this, it’s the only argument we can specify! I’d imagine the stride is always 1 for the formulas mentioned above, i.e. every possible grid is used, like you said!</p>

username: krishna511
cooked: <aside class="quote no-group" data-username="Epoching" data-post="7" data-topic="125389">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/epoching/48/18395_2.png" class="avatar"> Epoching:</div>
<blockquote>
<p>I’d imagine the stride is always 1 for the formulas mentioned above, i.e. every possible grid is used, like you said!</p>
</blockquote>
</aside>
<p>Exactly <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=12" title=":slight_smile:" class="emoji" alt=":slight_smile:" loading="lazy" width="20" height="20"><br>
Thanks <a class="mention" href="/u/epoching">@Epoching</a></p>

username: Epoching
cooked: <p>No problem, thanks for posting <a class="mention" href="/u/krishna511">@krishna511</a>, I learned something new haha!</p>