<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Pierre JAUMIER on Medium]]></title>
        <description><![CDATA[Stories by Pierre JAUMIER on Medium]]></description>
        <link>https://medium.com/@pjaumier85?source=rss-7393ee0f85b1------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*ZPtn09DTkCgXeXuvHNEqvQ.jpeg</url>
            <title>Stories by Pierre JAUMIER on Medium</title>
            <link>https://medium.com/@pjaumier85?source=rss-7393ee0f85b1------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 01 Dec 2026 21:07:15 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@pjaumier85/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Backpropagation in a convolutional layer]]></title>
            <link>https://medium.com/data-science/backpropagation-in-a-convolutional-layer-24c8d64d8509?source=rss-7393ee0f85b1------2</link>
            <guid isPermaLink="false">https://medium.com/p/24c8d64d8509</guid>
            <category><![CDATA[mathematics]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[neural-networks]]></category>
            <category><![CDATA[convolutional-network]]></category>
            <dc:creator><![CDATA[Pierre JAUMIER]]></dc:creator>
            <pubDate>Wed, 10 Jul 2019 14:08:26 GMT</pubDate>
            <atom:updated>2019-07-17T16:15:55.709Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_ZXD_eBOQKSILoxWBMFlKA.jpeg" /><figcaption>Backpropagation in a convolutional layer</figcaption></figure><h3>Introduction</h3><h4>Motivation</h4><p>The aim of this post is to detail how gradient backpropagation is working in a convolutional layer of a neural network. Typically the output of this layer will be the input of a chosen activation function (relufor instance). We are making the assumption that we are given the gradient dy backpropagated from this activation function. As I was unable to find on the web a complete, detailed, and “simple” explanation of how it works. I decided to do the maths, trying to understand step by step how it’s working on simple examples before generalizing. Before further reading, you should be familiar with neural networks, and especially forward pass, backpropagation of gradient in a computational graph and basic linear algebra with tensors.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/400/1*5MZusElPqtwH9A3mJF8F3A.jpeg" /><figcaption>Convolution layer — Forward pass &amp; BP</figcaption></figure><h4>Notations</h4><p>* will refer to the convolution of 2 tensors in the case of a neural network (an input x and a filter w).</p><ul><li>When xand w are matrices:</li><li>if xand w share the same shape, x*w will be a scalar equal to the sum across the results of the element-wise multiplication between the arrays.</li><li>if wis smaller the x, we will obtain an activation map y where each value is the predefined convolution operation of a sub-region of x with the sizes of w. This sub-region activated by the filter is sliding all across the input array x.</li><li>if xand w have more than 2 dimensions, we are considering the last 3 ones for the convolution, and the last 2 ones for the highlighted sliding area (we just add one depth to our matrix)</li></ul><p>Notations and variables are the same as the ones used in the <a href="http://cs231n.stanford.edu/">excellent Stanford course</a> on convolutional neural networks for visual recognition and especially the ones of <a href="http://cs231n.github.io/assignments2019/assignment2/">assignment 2</a>. Details on convolutional layer and forward pass will be found in this <a href="https://www.youtube.com/watch?v=bNb2fEVKeEo&amp;list=PL3FW7Lu3i5JvHM8ljYj-zLfQRF3EO8sYv&amp;index=5">video</a> and an instance of a naive implementation of the forward pass <a href="https://neodelphis.github.io/convnet/python/2019/07/02/convnet-forward-pass.html">post</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/541/1*L0uD18HDiWTy_a9og--c1A.jpeg" /><figcaption>Convolution layer notations</figcaption></figure><h4>Goal</h4><p>Our goal is to find out how gradient is propagating backwards in a convolutional layer. The forward pass is defined like this:</p><p>The input consists of N data points, each with C channels, height H and width W. We convolve each input with F different filters, where each filter spans all C channels and has height HH and width WW.</p><p>Input:</p><ul><li>x: Input data of shape (N, C, H, W)</li><li>w: Filter weights of shape (F, C, HH, WW)</li><li>b: Biases, of shape (F,)</li><li>conv_param: A dictionary with the following keys:</li><li>‘stride’: The number of pixels between adjacent receptive fields in the horizontal and vertical directions.</li><li>‘pad’: The number of pixels that will be used to zero-pad the input.</li></ul><p>During padding, ‘pad’ zeros should be placed symmetrically (i.e equally on both sides) along the height and width axes of the input.</p><p>Returns a tuple of:</p><ul><li>out: Output data, of shape (N, F, H’, W’) where H’ and W’ are given by</li></ul><p>H’ = 1 + (H + 2 * pad — HH) / stride</p><p>W’ = 1 + (W + 2 * pad — WW) / stride</p><ul><li>cache: (x, w, b, conv_param)</li></ul><h3>Forward pass</h3><h4>Generic case (simplified with N=1, C=1, F=1)</h4><p>N=1 one input, C=1 one channel, F=1 one filter.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/580/1*0e98PqagSEkxhy8Ef02XgA.jpeg" /><figcaption>Convolution 2D</figcaption></figure><p>x : H×W<br>x′=x with padding<br>w : HH×WW<br>b bias : scalar<br>y : H′×W′<br>stride s</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/529/1*jv1dAg0bjPRN3r3Fqh5rvQ.png" /></figure><h4>Specific case: stride=1, pad=0, and no bias.</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/490/1*7p5zdb9aP_CJtznivj9IVg.png" /></figure><h3>Backpropagation</h3><p>We know:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/107/1*n_tKekYDQEeOak32F0-reA.png" /></figure><p>We want to compute <em>dx</em>, <em>dw</em> and <em>db</em>, partial derivatives of our cost funcion L. We suppose that the gradient of this function has been backpropagated till y.</p><h3>Trivial case: input x is a vector (1 dimension)</h3><p>We are looking for an intuition of how it works on an easy setup and later on we will try to generalize.</p><h4>Input</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/143/1*HTrkNc0vYuk5_Kcm-uzb8w.png" /></figure><h4>Output</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/128/1*4ACH7pkMN8Q1NIuwUDqUiw.png" /></figure><h4>Forward pass — convolution with one filter w, stride = 1, padding = 0</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/468/1*oEKG_YPXRd4bTZqrXXlIug.png" /></figure><h4>Backpropagation</h4><p>We know the gradient of our cost function L with respect to y:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/108/1*CIYrnm7DIeek040bosfSHA.png" /></figure><p>This can be written with the Jacobian notation:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/211/1*krZNcjrEShywC0NoEe_vGg.png" /></figure><p>dy and y share the same shape:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/183/1*jlfQ8gH-o9WxgmbLwDi9dQ.png" /></figure><p>We are looking for</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/271/1*7ECVPMPfvev2IIkOXgFWmg.png" /></figure><h4>db</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/229/1*2I-g5Fm0ZOcRnyAa06iusA.png" /></figure><p>Using the chain rule and the forward pass formula (1), we can write:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/400/1*ioefE_aGDkz2Bik-qJlMoA.png" /></figure><h4>dw</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/237/1*8X6gCM7zZuwJwpn9czATgQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/179/1*PhDc5tjo46K6qXCQMmNCnw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/151/1*4kdsQjw24ea-p6yC9aUsnw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/263/1*s5vyoFInRYVDo67vOaYJWg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/249/1*NpSLMb7Ij8PwwA5Gw9zRaA.png" /></figure><p>We can notice that dw is a convolution of the input x with a filter dy. Let’s see if it’s still valid with a added dimension.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/192/1*2mWY3AGMbklOSfOgg09YcA.png" /></figure><h4>dx</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/240/1*n1l4kfM9pDEIFYnIIlNXLg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/254/1*vJvGnXEqrauu3LRxzyLSDw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/234/1*0MI4CgJaYJKPXqfvGuLNnw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/189/1*dAoujCUwjloIdRHkA8xWaA.png" /></figure><p>Once again, we have a convolution. A little bit more complex this time. We should consider an input dy with a 0-padding of size 1 convolved with an “inverted” filter w like (<em>w</em>2,<em>w</em>1)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/188/1*LKJTeJYb6ye-bHPAYP2Wfw.png" /></figure><p>Next step will be to have a look on how it works on small matrices.</p><h3>Input x is a matrix (2 dimensions)</h3><h4>Input</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/248/1*xGGpG1-uj_ohs9wvBTLfJg.png" /></figure><h4>Output</h4><p>Once again, we will choose the easiest case: stride = 1 and no padding. Shape of y will be (3,3)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/195/1*fm0ujVWwn15gkfd9E-4Ixg.png" /></figure><h4>Forwad pass</h4><p>We will have:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/383/1*1_hA3pdOvX7cXskc6pRzlQ.png" /></figure><p>Written with subscripts:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/612/1*vhFieax-du_7KJtVT66tow.png" /></figure><h4>Backpropagation</h4><p>We know:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/113/1*IVjiW7VEcV6J6PKBSmEmng.png" /></figure><h4>db</h4><p>Using the Einstein convention to alleviate the formulas (when an index variable appears twice in a multiplication, it implies summation of that term over all the values of the index)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/131/1*tM3L1cY4qHgbFdcHfFp2sQ.png" /></figure><p>Summation on i and j. And we have:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/162/1*BXMmF25HwhfQ4Slk9QnU_Q.png" /></figure><h4>dw</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/243/1*VtP7t3xpC4JBxFtNLrHvZA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/465/1*fA-S3RzO4fedEcMKKMRsiA.png" /></figure><p>We are looking for</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/65/1*6SVpbpaNyGY7Ar4_QbTqGA.png" /></figure><p>Using the formula (4) we have:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/285/1*5oPNAcbOoNYNhcBDEc0WvQ.png" /></figure><p>All terms</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/108/1*qpt6V-0ZHvFz7uPIrSMVCg.png" /></figure><p>Except for (<em>k</em>,<em>l</em>)=(<em>m</em>,<em>n</em>) where it’s 1, case occurring just once in the double sum. Hence:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/185/1*56m0jtN1CXTGy6Q7wCrO0g.png" /></figure><p>Using formula (3) we now have:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/319/1*AD_V9vcjyL6eF-Xe2JotxQ.png" /></figure><p>If we compare this equation with formula (1) giving the result of a convolution, we can distinguish a similar pattern where dy is a filter applied on an input x.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/458/1*hrnQ8wNbzvs_X_oBjFLCJw.png" /></figure><h4>dx</h4><p>Using the chaine rule as we did for (5), we have:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/174/1*BFZX2HGW7YQYlT04D2mBTg.png" /></figure><p>This time, we are looking for</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/57/1*-HKElWmGM93WijMW6-_60g.png" /></figure><p>Using equation (4):</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/280/1*8X2l06wuIwlPV-HdGrxlnw.png" /></figure><p>We now have:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/442/1*LieslHYhbBnqAzINOPd79g.png" /></figure><p>In our example, range sets for indices are:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/238/1*Bo_ISAxssh8yVR_ATg9OxA.png" /></figure><p>When we set <em>k</em>=<em>m</em>−<em>i</em>+1, we are going to be out of the defined boundaries:(<em>m</em>−<em>i</em>+1)∈[−1,4]</p><p>In order to keep confidence in formula above, we choose to extend the definition of matrix <em>w</em> with 0 values as soon as indices will go out of the defined range.</p><p>Once again in the double sum , we only have once partial derivative of x equals 1. So:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/200/1*wE_pxLT-517MtrevuMm-tw.png" /></figure><p>where <em>w</em> is our 0-extended initial filter, thus:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/282/1*A_PZk_4qfat2cA157uZfqA.png" /></figure><p>Lets visualize it on several chosen values for the indices.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/414/1*EskyBiZFbpHgy7-GrEmhlA.png" /></figure><p>Using ∗ notation for convolution, we have:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/240/1*Y9v940ky3XBuXzLWyfq5Vg.png" /></figure><p>As <em>dy</em> remain the same, we will only look at the values of indices of w. For <em>dx</em>22, range for w: 3−<em>i</em>,3−<em>j</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/183/1*3BtGQPCT3ar0OYAt9zFntg.png" /></figure><p>We now have a convolution between dy and a w’ matrix defined by:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/163/1*Okx6bVhOM9nfA53nzwSW0Q.png" /></figure><p>Another instance in order to see what’s happening. <em>dx</em>43, w : 4−<em>i</em>,3−<em>j</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/176/1*oZ-iXoDvI93JohVYX7e8jw.png" /></figure><p>Last one <em>dx</em>44</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/144/1*F69zyCJf-h3xpNmO07zjiw.png" /></figure><p>We do see poping up an “inverted filter” w’. This time we have a convolution between an input <em>dy</em> with a 0-padding border of size 1 and a filter w’ slidding with a stride of 1.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/462/1*M615JPXg2m8XOfEh9iLdvQ.png" /></figure><h4>Summary of backprop equations</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/567/1*MZfsVCVBF9pgpVKFQ5Q8PQ.png" /></figure><h3>Taking depths into account</h3><p>Things are becoming slightly more complex when we try to take depth into account (C channels for input x, and F distinct filters for w)</p><p>Inputs:</p><ul><li>x: shape (C, H, W)</li><li>w: filter’s weights shape (F, C, HH, WW)</li><li>b: shape (F,)</li></ul><p>Outputs:</p><ul><li>y: shape (F, H’, W’)</li></ul><p>Maths formulas see many indices emerging, making them more difficult to read. The forward pass formula in our example will be:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/314/1*IXRJoTD9Vh-OJ3x_GILVvg.png" /></figure><h4>db</h4><p>db computation remains easy as each <em>b_f</em> is related to an activation map <em>y_f</em>:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/174/1*rwkeZSS2auo9Qi896XdtrQ.png" /></figure><h4>dw</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/193/1*B97OJGLiGceksP5CarxN4A.png" /></figure><p>Using the forward pass formula, as the double sum does not use dy indices, we can write:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/246/1*lr-LqF88DFK_K1Dsf8-V3Q.png" /></figure><h3>Algorithm</h3><p>Now that we have the intuition of how it’s working, we choose not to write the entire set of equations (which can be pretty tedious), but we’ll use what has been coded for the forward pass, and playing with dimensions try to code the backprop for each gradient. Fortunately we can compute a numerical value of the gradient to check our implementation. This implementation is only valid for a stride=1, thing are becoming slightly more complex with a distinct stride, and another approach is needed. Maybe for another post!</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/576794ddd68592a7df0fb2c763b59457/href">https://medium.com/media/576794ddd68592a7df0fb2c763b59457/href</a></iframe><h4>Gradient numerical check</h4><pre>Testing conv_backward_naive function<br>dx error:  7.489787768926947e-09<br>dw error:  1.381022780971562e-10<br>db error:  1.1299800330640326e-10</pre><p>Almost 0 each time, everything seems tobe OK! :)</p><h3>References</h3><ul><li><a href="https://neodelphis.github.io/convnet/maths/python/english/2019/07/10/convnet-bp-en.html">This post on my blog with mathjax equations</a> :)</li><li><a href="http://cs231n.stanford.edu/">Stanford course on convolutional neural networks for visual recognition</a></li><li><a href="http://cs231n.github.io/assignments2019/assignment2/">Stanford CNN assignment 2</a></li><li><a href="https://www.youtube.com/watch?v=bNb2fEVKeEo&amp;list=PL3FW7Lu3i5JvHM8ljYj-zLfQRF3EO8sYv&amp;index=5">Convolutional neural network, forward pass</a></li><li><a href="https://neodelphis.github.io/convnet/python/2019/07/02/convnet-forward-pass.html">Convolution Layer : Naive implementation of the forward pass</a>.</li><li><a href="https://www.jefkine.com/general/2016/09/05/backpropagation-in-convolutional-neural-networks/">Backpropagation In Convolutional Neural Networks</a></li><li><a href="https://neodelphis.github.io/convnet/maths/python/2019/07/08/convnet-bp.html">Cet article en français</a></li></ul><p>Comments are welcome to improve this post, feel free to contact me!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=24c8d64d8509" width="1" height="1" alt=""><hr><p><a href="https://medium.com/data-science/backpropagation-in-a-convolutional-layer-24c8d64d8509">Backpropagation in a convolutional layer</a> was originally published in <a href="https://medium.com/data-science">TDS Archive</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>