Breakdown of Stochastic Gradient Descent Code in Python










2















In Michael Nielson's Online book on Artificial Neural Networks, http://neuralnetworksanddeeplearning.com, he provides the following code:



 def update_mini_batch(self, mini_batch, eta):
"""Update the network's weights and biases by applying
gradient descent using backpropagation to a single mini batch.
The ``mini_batch`` is a list of tuples ``(x, y)``, and ``eta``
is the learning rate."""
nabla_b = [np.zeros(b.shape) for b in self.biases]
nabla_w = [np.zeros(w.shape) for w in self.weights]
for x, y in mini_batch:
delta_nabla_b, delta_nabla_w = self.backprop(x, y)
nabla_b = [nb+dnb for nb, dnb in zip(nabla_b, delta_nabla_b)]
nabla_w = [nw+dnw for nw, dnw in zip(nabla_w, delta_nabla_w)]
self.weights = [w-(eta/len(mini_batch))*nw
for w, nw in zip(self.weights, nabla_w)]
self.biases = [b-(eta/len(mini_batch))*nb
for b, nb in zip(self.biases, nabla_b)]


I am having trouble understanding the parts with nabla_b and nabla_w.



If delta_nabla_b and delta_nabla_w are the gradients of the cost function then why do we add them to the existing values of nabla_b and nabla_w here?



nabla_b = [nb+dnb for nb, dnb in zip(nabla_b, delta_nabla_b)]
nabla_w = [nw+dnw for nw, dnw in zip(nabla_w, delta_nabla_w)]


Shouldn't we just directly define



nabla_b, nabla_w = self.backprop(x, y)


and update the weight and bias matrices?



Do we make nabla_b and nabla_w because we want to do an average over the gradients and they are the matrices of the sums of the gradients?










share|improve this question




























    2















    In Michael Nielson's Online book on Artificial Neural Networks, http://neuralnetworksanddeeplearning.com, he provides the following code:



     def update_mini_batch(self, mini_batch, eta):
    """Update the network's weights and biases by applying
    gradient descent using backpropagation to a single mini batch.
    The ``mini_batch`` is a list of tuples ``(x, y)``, and ``eta``
    is the learning rate."""
    nabla_b = [np.zeros(b.shape) for b in self.biases]
    nabla_w = [np.zeros(w.shape) for w in self.weights]
    for x, y in mini_batch:
    delta_nabla_b, delta_nabla_w = self.backprop(x, y)
    nabla_b = [nb+dnb for nb, dnb in zip(nabla_b, delta_nabla_b)]
    nabla_w = [nw+dnw for nw, dnw in zip(nabla_w, delta_nabla_w)]
    self.weights = [w-(eta/len(mini_batch))*nw
    for w, nw in zip(self.weights, nabla_w)]
    self.biases = [b-(eta/len(mini_batch))*nb
    for b, nb in zip(self.biases, nabla_b)]


    I am having trouble understanding the parts with nabla_b and nabla_w.



    If delta_nabla_b and delta_nabla_w are the gradients of the cost function then why do we add them to the existing values of nabla_b and nabla_w here?



    nabla_b = [nb+dnb for nb, dnb in zip(nabla_b, delta_nabla_b)]
    nabla_w = [nw+dnw for nw, dnw in zip(nabla_w, delta_nabla_w)]


    Shouldn't we just directly define



    nabla_b, nabla_w = self.backprop(x, y)


    and update the weight and bias matrices?



    Do we make nabla_b and nabla_w because we want to do an average over the gradients and they are the matrices of the sums of the gradients?










    share|improve this question


























      2












      2








      2








      In Michael Nielson's Online book on Artificial Neural Networks, http://neuralnetworksanddeeplearning.com, he provides the following code:



       def update_mini_batch(self, mini_batch, eta):
      """Update the network's weights and biases by applying
      gradient descent using backpropagation to a single mini batch.
      The ``mini_batch`` is a list of tuples ``(x, y)``, and ``eta``
      is the learning rate."""
      nabla_b = [np.zeros(b.shape) for b in self.biases]
      nabla_w = [np.zeros(w.shape) for w in self.weights]
      for x, y in mini_batch:
      delta_nabla_b, delta_nabla_w = self.backprop(x, y)
      nabla_b = [nb+dnb for nb, dnb in zip(nabla_b, delta_nabla_b)]
      nabla_w = [nw+dnw for nw, dnw in zip(nabla_w, delta_nabla_w)]
      self.weights = [w-(eta/len(mini_batch))*nw
      for w, nw in zip(self.weights, nabla_w)]
      self.biases = [b-(eta/len(mini_batch))*nb
      for b, nb in zip(self.biases, nabla_b)]


      I am having trouble understanding the parts with nabla_b and nabla_w.



      If delta_nabla_b and delta_nabla_w are the gradients of the cost function then why do we add them to the existing values of nabla_b and nabla_w here?



      nabla_b = [nb+dnb for nb, dnb in zip(nabla_b, delta_nabla_b)]
      nabla_w = [nw+dnw for nw, dnw in zip(nabla_w, delta_nabla_w)]


      Shouldn't we just directly define



      nabla_b, nabla_w = self.backprop(x, y)


      and update the weight and bias matrices?



      Do we make nabla_b and nabla_w because we want to do an average over the gradients and they are the matrices of the sums of the gradients?










      share|improve this question
















      In Michael Nielson's Online book on Artificial Neural Networks, http://neuralnetworksanddeeplearning.com, he provides the following code:



       def update_mini_batch(self, mini_batch, eta):
      """Update the network's weights and biases by applying
      gradient descent using backpropagation to a single mini batch.
      The ``mini_batch`` is a list of tuples ``(x, y)``, and ``eta``
      is the learning rate."""
      nabla_b = [np.zeros(b.shape) for b in self.biases]
      nabla_w = [np.zeros(w.shape) for w in self.weights]
      for x, y in mini_batch:
      delta_nabla_b, delta_nabla_w = self.backprop(x, y)
      nabla_b = [nb+dnb for nb, dnb in zip(nabla_b, delta_nabla_b)]
      nabla_w = [nw+dnw for nw, dnw in zip(nabla_w, delta_nabla_w)]
      self.weights = [w-(eta/len(mini_batch))*nw
      for w, nw in zip(self.weights, nabla_w)]
      self.biases = [b-(eta/len(mini_batch))*nb
      for b, nb in zip(self.biases, nabla_b)]


      I am having trouble understanding the parts with nabla_b and nabla_w.



      If delta_nabla_b and delta_nabla_w are the gradients of the cost function then why do we add them to the existing values of nabla_b and nabla_w here?



      nabla_b = [nb+dnb for nb, dnb in zip(nabla_b, delta_nabla_b)]
      nabla_w = [nw+dnw for nw, dnw in zip(nabla_w, delta_nabla_w)]


      Shouldn't we just directly define



      nabla_b, nabla_w = self.backprop(x, y)


      and update the weight and bias matrices?



      Do we make nabla_b and nabla_w because we want to do an average over the gradients and they are the matrices of the sums of the gradients?







      machine-learning neural-network deep-learning backpropagation gradient-descent






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Dec 14 '18 at 19:17









      Maxim

      31k2176124




      31k2176124










      asked Nov 13 '18 at 2:02









      Shrey JoshiShrey Joshi

      1205




      1205






















          1 Answer
          1






          active

          oldest

          votes


















          0















          Do we make nabla_b and nabla_w because we want to do an average over the gradients and they are the matrices of the sums of the gradients?




          Yes, your thinking is right. Basically, this code directly corresponds to the formula in the step 3 Gradient descent in the tutorial.



          The formula itself is a bit misleading, and intuitively it's easier to think that the weights and biases are updated independently for each instance in a mini-batch. But if you recall that the gradient of the sum is the sum of the gradients it becomes clear that it's actually the same. In both cases, all gradients contribute in the same way into the parameters update.






          share|improve this answer






















            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53272710%2fbreakdown-of-stochastic-gradient-descent-code-in-python%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0















            Do we make nabla_b and nabla_w because we want to do an average over the gradients and they are the matrices of the sums of the gradients?




            Yes, your thinking is right. Basically, this code directly corresponds to the formula in the step 3 Gradient descent in the tutorial.



            The formula itself is a bit misleading, and intuitively it's easier to think that the weights and biases are updated independently for each instance in a mini-batch. But if you recall that the gradient of the sum is the sum of the gradients it becomes clear that it's actually the same. In both cases, all gradients contribute in the same way into the parameters update.






            share|improve this answer



























              0















              Do we make nabla_b and nabla_w because we want to do an average over the gradients and they are the matrices of the sums of the gradients?




              Yes, your thinking is right. Basically, this code directly corresponds to the formula in the step 3 Gradient descent in the tutorial.



              The formula itself is a bit misleading, and intuitively it's easier to think that the weights and biases are updated independently for each instance in a mini-batch. But if you recall that the gradient of the sum is the sum of the gradients it becomes clear that it's actually the same. In both cases, all gradients contribute in the same way into the parameters update.






              share|improve this answer

























                0












                0








                0








                Do we make nabla_b and nabla_w because we want to do an average over the gradients and they are the matrices of the sums of the gradients?




                Yes, your thinking is right. Basically, this code directly corresponds to the formula in the step 3 Gradient descent in the tutorial.



                The formula itself is a bit misleading, and intuitively it's easier to think that the weights and biases are updated independently for each instance in a mini-batch. But if you recall that the gradient of the sum is the sum of the gradients it becomes clear that it's actually the same. In both cases, all gradients contribute in the same way into the parameters update.






                share|improve this answer














                Do we make nabla_b and nabla_w because we want to do an average over the gradients and they are the matrices of the sums of the gradients?




                Yes, your thinking is right. Basically, this code directly corresponds to the formula in the step 3 Gradient descent in the tutorial.



                The formula itself is a bit misleading, and intuitively it's easier to think that the weights and biases are updated independently for each instance in a mini-batch. But if you recall that the gradient of the sum is the sum of the gradients it becomes clear that it's actually the same. In both cases, all gradients contribute in the same way into the parameters update.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Dec 14 '18 at 19:16









                MaximMaxim

                31k2176124




                31k2176124



























                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53272710%2fbreakdown-of-stochastic-gradient-descent-code-in-python%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Use pre created SQLite database for Android project in kotlin

                    Darth Vader #20

                    Ondo