Python 我得到了&x201C;缩进错误:应为缩进块”;在np.random.seed(2)中。如何解决这个问题?
但出现以下错误: 文件“”,第4行 np.random.seed(2)#我们设置了一个seed,以便我们的输出与我们的匹配,尽管初始化是随机的。 ^ 缩进错误:应为缩进块,包括以下内容:Python 我得到了&x201C;缩进错误:应为缩进块”;在np.random.seed(2)中。如何解决这个问题?,python,numpy,backpropagation,Python,Numpy,Backpropagation,但出现以下错误: 文件“”,第4行 np.random.seed(2)#我们设置了一个seed,以便我们的输出与我们的匹配,尽管初始化是随机的。 ^ 缩进错误:应为缩进块,包括以下内容: 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 initiali
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)
到
在上面的示例中,需要缩进四个空格。即:
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
(我额外添加了
参数
字典格式;)函数中应该包含的代码需要缩进。这就是python如何知道属于函数的代码的原因。我在您的文章中编辑了函数的缩进方式。只需确保函数/类等内部的内容正确缩进您声明了一个函数行2,因此您必须缩进属于此函数的行。如果函数应该是空的,那么现在您只需添加一个“path”作为body@Chrispresso这应该是评论或回答,而不是对问题的编辑。以这种方式编辑只会导致提问者、评论者和答案之间的混淆
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