2017-09-21 72 views
-1

我做叫coursera課程:建立你的深層神經網絡python3 Asse田(建立你的深層神經網絡)

我不明白這個代碼是如何工作的,它應該做什麼和爲什麼它不工作:

def initialize_parameters_deep(layer_dims): 
    np.random.seed(3) 
    parameters = {} 
    L = len(layer_dims)   # number of layers in the network 
​ 
    for l in range(1, L): 
     ### START CODE HERE ### (≈ 2 lines of code) 
     parameters['W' + str(l)] = np.random.randn(layer_dims[1], layer_dims[1-1])*0.01 
     parameters['b' + str(l)] = np.zeros((layer_dims[1], 1)) 
     ### END CODE HERE ### 

     assert(parameters['W' + str(l)].shape == (layer_dims[l], layer_dims[l-1])) 
     assert(parameters['b' + str(l)].shape == (layer_dims[l], 1)) 
​ 

    return parameters 

parameters = initialize_parameters_deep([5,4,3]) 
print("W1 = " + str(parameters["W1"])) 
print("b1 = " + str(parameters["b1"])) 
print("W2 = " + str(parameters["W2"])) 
print("b2 = " + str(parameters["b2"])) 

,當我運行此代碼:

AssertionError       Traceback (most recent call last) 
<ipython-input-24-01a57e5821a5> in <module>() 
----> 1 parameters = initialize_parameters_deep([5,4,3]) 
     2 print("W1 = " + str(parameters["W1"])) 
     3 print("b1 = " + str(parameters["b1"])) 
     4 print("W2 = " + str(parameters["W2"])) 
     5 print("b2 = " + str(parameters["b2"])) 

<ipython-input-23-bd5b7f814b99> in initialize_parameters_deep(layer_dims) 
    22   ### END CODE HERE ### 
    23 
---> 24   assert(parameters['W' + str(l)].shape == (layer_dims[l], layer_dims[l-1])) 
    25   assert(parameters['b' + str(l)].shape == (layer_dims[l], 1)) 
    26 

AssertionError: 
+0

你有答案嗎? –

回答

0

我覺得你只是混合L(L)以數字1

的錯誤是在下面一行:

parameters['W' + str(l)] = np.random.randn(layer_dims[1], layer_dims[1-1])*0.01 
parameters['b' + str(l)] = np.zeros((layer_dims[1], 1)) 

這應該是這樣的:

parameters['W' + str(l)] = np.random.randn(layer_dims[l], layer_dims[l-1])*0.01 
parameters['b' + str(l)] = np.zeros((layer_dims[l], 1)) 

如果您想了解從碼深度學習,我建議這個鏈接: https://github.com/ludlows/deep-learning-specialization/

克隆它,你可以在你自己的PC上運行它。

快樂編碼!