我正在处理 Deeplearning.AI 课程的笔记本,但遇到了错误,问题是我用零初始化了神经网络的参数:
# GRADED FUNCTION: initialize_with_zeros
def initialize_with_zeros(dim):
"""
This function creates a vector of zeros of shape (dim, 1) for w and initializes b to 0.
Argument:
dim -- size of the w vector we want (or number of parameters in this case)
Returns:
w -- initialized vector of shape (dim, 1)
b -- initialized scalar (corresponds to the bias) of type float
"""
# (≈ 2 lines of code)
#w = ...
# b = ...
# YOUR CODE STARTS HERE
w = np.zeros((dim,1))
b = np.zeros(dtype = float)
# YOUR CODE ENDS HERE
return w, b
我已在参数中的正确位置填写了正确的形状,但它似乎不起作用,并且出现以下错误:
dim = 2
w, b = initialize_with_zeros(dim)
assert type(b) == float
print ("w = " + str(w))
print ("b = " + str(b))
initialize_with_zeros_test(initialize_with_zeros)
您还需要给
b
一个形状,即使它是一个标量:
b = np.zeros((1, ), dtype = float)
请检查下面的 w、b 初始化
w=np.zeros(形状=(dim,1),dtype=float) b = 0.0
调用函数后出现的断言希望返回的
b
值是单个浮点数,而不是数组。只需使用:
b = 0.0