Home > Software design >  np.zeros() missing required argument 'shape' (pos 1)
np.zeros() missing required argument 'shape' (pos 1)

Time:12-16

I'm working on a notebook from the Deeplearning.AI course and i've ran into an error, the problem was that I had initialize the parameters of the neural network with zeros:

# 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

I've filled in the correct shape at correct position in the argument but it does not seem to be working and i'm getting the following error :

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) 

enter image description here

CodePudding user response:

You also need to give b a shape, even if it's a scalar:

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

CodePudding user response:

Your code appears to want b to be a single float, not an array. Just use:

b = 0.0
  • Related