Custom function in conv layer

yuri: <p>as the conv layer performs the sum of the elementwise product between two arrays A and B</p>
<p><img src="https://discuss.pytorch.org/uploads/default/original/3X/5/4/5420c408553453a9b376fdb4b9bd83be878897b7.png" alt="Screenshot (89)" data-base62-sha1="c0ej5fDijsTaZvkPIW8hDCv7chN" width="320" height="235"></p>
<p>however , I would like to use the same iteration mechanism to perform a custom operation as follows:</p>
<p><img src="https://discuss.pytorch.org/uploads/default/original/3X/9/4/94bdd8482fe5c7437da7b3d86ddef91cef9df596.png" alt="Screenshot (88)" data-base62-sha1="ldPpbZmPSeTIPpGBt1jvlzLYHz0" width="498" height="240"></p>
<p>for those who might notice that this equation is the Kullback–Leibler divergence, indeed I want to perform the KL div between 2d arrays that have different sizes that’s why I need the sliding mechanism from the conv layer to run kl iteratively…<br>
thnk</p>

KFrank: <p>Hi Yuri!</p>
<aside class="quote no-group" data-username="yuri" data-post="1" data-topic="154386" data-full="true">
<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/y/71c47a/48.png" class="avatar"> yuri:</div>
<blockquote>
<p>I need the sliding mechanism from the conv layer</p>
</blockquote>
</aside>
<p>Please take a look at <a href="https://pytorch.org/docs/1.11/generated/torch.nn.Unfold.html" rel="noopener nofollow ugc">Unfold</a> (or <code>unfold()</code>).</p>
<p>Here is a discussion about using <code>unfold()</code> for a custom convolution (with<br>
links to further discussion):</p>
<aside class="quote quote-modified" data-post="1" data-topic="70175">
  <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/phisku/48/14080_2.png" class="avatar">
    <a href="https://discuss.pytorch.org/t/make-custom-conv2d-layer-efficient-wrt-speed-and-memory/70175">Make Custom Conv2d Layer efficient (wrt speed and memory)</a> <a class="badge-category__wrapper " href="/c/vision/5"><span data-category-id="5" data-drop-close="true" class="badge-category " title="Topics related to either pytorch/vision or vision research related topics"><span class="badge-category__name">vision</span></span></a>
  </div>
  <blockquote>
    I was able to make my own Conv2d layer using ‘unfold’ and following <a href="https://discuss.pytorch.org/t/convolution-that-only-take-channel-wise-summation/21240/4">this post</a>. I need to have full access to the conv2d operations as I will apply some custom functions at several points during the kernel operation. For this post however I am - for a start - only concerned with getting the most basic Conv2d layer up and running. 
My resulting function looks as follows: 
# Vanilla Convolution
def myconv2d(input, weight, bias=None, stride=(1,1), padding=(0,0), dilation=(1,1), groups=1):
    # pad …
  </blockquote>
</aside>

<p>Best.</p>
<p>K. Frank</p>

yuri: <p>thank you, I have also found this discussion that clearly explains the fold unfold process</p>
<aside class="onebox stackexchange" data-onebox-src="https://stackoverflow.com/questions/53972159/how-does-pytorchs-fold-and-unfold-work">
  <header class="source">

      <a href="https://stackoverflow.com/questions/53972159/how-does-pytorchs-fold-and-unfold-work" target="_blank" rel="noopener nofollow ugc">stackoverflow.com</a>
  </header>

  <article class="onebox-body">
      <a href="https://stackoverflow.com/users/5170171/shoshi" target="_blank" rel="noopener nofollow ugc">
    <img alt="shoshi" src="https://www.gravatar.com/avatar/f4fd295a4ae026e318d1063c95c4c028?s=256&amp;d=identicon&amp;r=PG&amp;f=1" class="thumbnail onebox-avatar" width="256" height="256">
  </a>

<h4>
  <a href="https://stackoverflow.com/questions/53972159/how-does-pytorchs-fold-and-unfold-work" target="_blank" rel="noopener nofollow ugc">How does Pytorch's "Fold" and "Unfold" work?</a>
</h4>

<div class="tags">
  <strong>python, machine-learning, deep-learning, computer-vision</strong>
</div>

<div class="date">
  asked by
  
  <a href="https://stackoverflow.com/users/5170171/shoshi" target="_blank" rel="noopener nofollow ugc">
    shoshi
  </a>
  on <a href="https://stackoverflow.com/questions/53972159/how-does-pytorchs-fold-and-unfold-work" target="_blank" rel="noopener nofollow ugc">06:14PM - 29 Dec 18 UTC</a>
</div>

  </article>

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

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