Я получаю «IndentationError: ожидается блок с отступом» в np.random.seed (2). Как это исправить? - PullRequest
1 голос
/ 22 октября 2019
import numpy as np
def initialize_parameters(n_x, n_h, n_y):

    np.random.seed(2) # we set up a seed so that our output matches ours although the initialization is random.

    W1 = np.random.randn(n_h, n_x) * 0.01 #weight matrix of shape (n_h, n_x)
    b1 = np.zeros(shape=(n_h, 1))  #bias vector of shape (n_h, 1)
    W2 = np.random.randn(n_y, n_h) * 0.01   #weight matrix of shape (n_y, n_h)
    b2 = np.zeros(shape=(n_y, 1))  #bias vector of shape (n_y, 1)

    #store parameters into a dictionary    
    parameters = {"W1": W1,
                  "b1": b1,
                  "W2": W2,
                  "b2": b2}

    return parameters

#Function to define the size of the layer
def layer_sizes(X, Y):
    n_x = X.shape[0] # size of input layer
    n_h = 6# size of hidden layer
    n_y = Y.shape[0] # size of output layer
    return (n_x, n_h, n_y)

Но получил эту ошибку: Файл "", строка 4 np.random.seed (2) # мы настроили начальное число так, чтобы наш вывод соответствовал нашему, хотя инициализация была случайной. ^ IndentationError: ожидается блок с отступом

1 Ответ

1 голос
/ 22 октября 2019

Все от:

def initialize_parameters(n_x, n_h, n_y):

до

return parameters

в приведенном выше примере должно быть с отступом в четыре пробела. То есть это:

def initialize_parameters(n_x, n_h, n_y):

np.random.seed(2) # we set up a seed so that our output matches ours although the initialization is random.

W1 = np.random.randn(n_h, n_x) * 0.01 #weight matrix of shape (n_h, n_x)
b1 = np.zeros(shape=(n_h, 1))  #bias vector of shape (n_h, 1)
W2 = np.random.randn(n_y, n_h) * 0.01   #weight matrix of shape (n_y, n_h)
b2 = np.zeros(shape=(n_y, 1))  #bias vector of shape (n_y, 1)

#store parameters into a dictionary    
parameters = {"W1": W1,
                  "b1": b1,
                  "W2": W2,
                  "b2": b2}

return parameters

должно быть отформатировано так:

def initialize_parameters(n_x, n_h, n_y):

    np.random.seed(2) # we set up a seed so that our output matches ours although the initialization is random.

    W1 = np.random.randn(n_h, n_x) * 0.01 #weight matrix of shape (n_h, n_x)
    b1 = np.zeros(shape=(n_h, 1))  #bias vector of shape (n_h, 1)
    W2 = np.random.randn(n_y, n_h) * 0.01   #weight matrix of shape (n_y, n_h)
    b2 = np.zeros(shape=(n_y, 1))  #bias vector of shape (n_y, 1)

    #store parameters into a dictionary    
    parameters = {
        "W1": W1,
        "b1": b1,
        "W2": W2,
        "b2": b2
    }

    return parameters

(я добавила parameters словарь форматирования в качестве бонуса;))

...