np.zeros() 缺少必需参数“形状”(位置 1)

问题描述 投票:0回答:3

我正在处理 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) 

在此输入图片描述

python numpy
3个回答
0
投票

您还需要给

b
一个形状,即使它是一个标量:

b = np.zeros((1, ), dtype = float)

0
投票

请检查下面的 w、b 初始化

w=np.zeros(形状=(dim,1),dtype=float) b = 0.0


-1
投票

调用函数后出现的断言希望返回的

b
值是单个浮点数,而不是数组。只需使用:

b = 0.0
© www.soinside.com 2019 - 2024. All rights reserved.