Breakdown of Stochastic Gradient Descent Code in Python
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
add a comment |
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
add a comment |
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
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
machine-learning neural-network deep-learning backpropagation gradient-descent
edited Dec 14 '18 at 19:17
Maxim
32.5k2178131
32.5k2178131
asked Nov 13 '18 at 2:02
Shrey JoshiShrey Joshi
1205
1205
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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.
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
add a comment |
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.
add a comment |
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.
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.
answered Dec 14 '18 at 19:16
MaximMaxim
32.5k2178131
32.5k2178131
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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