gpt4 book ai didi

python - 我在 np.random.seed(2) 中得到 “IndentationError: expected an indented block”。如何解决这个问题?

转载 作者:行者123 更新时间:2023-12-01 07:04:11 25 4
gpt4 key购买 nike

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:需要一个缩进 block

最佳答案

一切来自:

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 字典格式作为奖励;))

关于python - 我在 np.random.seed(2) 中得到 “IndentationError: expected an indented block”。如何解决这个问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58507869/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com