Python的深度学习-随机梯度下降-分解代码

时间:2022-10-27 21:25:53

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 appeard. Such as from the function that follows, wich is in Chapter 1 of the link provided (I updated the code from 2.7 to 3.6) .

我正在尝试自学,主要是通过我的大学提供的一些书籍以及这个神经网络和深度学习。这个过程很困难,而且我也不习惯编码,因此出现了一些问题。例如从下面的函数中,wich是在提供的链接的第1章中(我将代码从2.7更新到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 renamedto 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 renamedto 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)

1 个解决方案

#1


2  

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.

这一部分:如果test_data在语义上等同于if test_data不是None,或者如果test_data != None。

Please let me know if I misunderstood something :)

如果我误解了什么,请告诉我。

#1


2  

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.

这一部分:如果test_data在语义上等同于if test_data不是None,或者如果test_data != None。

Please let me know if I misunderstood something :)

如果我误解了什么,请告诉我。