Deep Learning in Python – Stochastic Gradient Descent – Breaking down a code
Question:
I’m trying to learn Deep Learning basically by myself, using a few books provided by my university and this one Neural networks and Deep learning.
The process is hard, and as I’m not used to code either, some issues have appeared. Such as from the function that follows, which is in Chapter 1 of the link provided (I updated the code from 2.7 to 3.6) .
def SGD(self, training_data, epochs, mini_batch_size, eta,
test_data=None):
"""Train the neural network using mini-batch stochastic
gradient descent. The ``training_data`` is a list of tuples
``(x, y)`` representing the training inputs and the desired
outputs. The other non-optional parameters are
self-explanatory. If ``test_data`` is provided then the
network will be evaluated against the test data after each
epoch, and partial progress printed out. This is useful for
tracking progress, but slows things down substantially."""
if test_data: n_test = len(test_data)
n = len(training_data)
for j in range(epochs): #xrange was renamed to range
random.shuffle(training_data)
mini_batches = [
training_data[k:k+mini_batch_size]
for k in range(0, n, mini_batch_size)] #xrange was renamed to range
for mini_batch in mini_batches:
self.update_mini_batch(mini_batch, eta)
if test_data:
print ("Epoch {0}: {1} / {2}".format(
j, self.evaluate(test_data), n_test))
#print (self.biases)
#print("Hello02")
else:
print ("Epoch {0} complete".format(j))
return(self.biases, self.weights)
The issue for me is this one:
if test_data: n_test = len(test_data)
n = len(training_data)
Can anyone explain to me what is happening in this 2 lines? I’m used to a more conventional code stile such as:
if something:
print (another_thing)
Answers:
Maybe I’m misunderstanding you, but :
if test_data: n_test = len(test_data)
n = len(training_data)
… means the same as :
if test_data:
n_test = len(test_data)
n = len(training_data)
This part: if test_data
is semantically equivalent with if test_data is not None
or if test_data != None
.
Please let me know if I misunderstood something 🙂
I’m trying to learn Deep Learning basically by myself, using a few books provided by my university and this one Neural networks and Deep learning.
The process is hard, and as I’m not used to code either, some issues have appeared. Such as from the function that follows, which is in Chapter 1 of the link provided (I updated the code from 2.7 to 3.6) .
def SGD(self, training_data, epochs, mini_batch_size, eta,
test_data=None):
"""Train the neural network using mini-batch stochastic
gradient descent. The ``training_data`` is a list of tuples
``(x, y)`` representing the training inputs and the desired
outputs. The other non-optional parameters are
self-explanatory. If ``test_data`` is provided then the
network will be evaluated against the test data after each
epoch, and partial progress printed out. This is useful for
tracking progress, but slows things down substantially."""
if test_data: n_test = len(test_data)
n = len(training_data)
for j in range(epochs): #xrange was renamed to range
random.shuffle(training_data)
mini_batches = [
training_data[k:k+mini_batch_size]
for k in range(0, n, mini_batch_size)] #xrange was renamed to range
for mini_batch in mini_batches:
self.update_mini_batch(mini_batch, eta)
if test_data:
print ("Epoch {0}: {1} / {2}".format(
j, self.evaluate(test_data), n_test))
#print (self.biases)
#print("Hello02")
else:
print ("Epoch {0} complete".format(j))
return(self.biases, self.weights)
The issue for me is this one:
if test_data: n_test = len(test_data)
n = len(training_data)
Can anyone explain to me what is happening in this 2 lines? I’m used to a more conventional code stile such as:
if something:
print (another_thing)
Maybe I’m misunderstanding you, but :
if test_data: n_test = len(test_data)
n = len(training_data)
… means the same as :
if test_data:
n_test = len(test_data)
n = len(training_data)
This part: if test_data
is semantically equivalent with if test_data is not None
or if test_data != None
.
Please let me know if I misunderstood something 🙂