Initialize_parameters_he

I have written the following code
def initialize_parameters_he(layers_dims):
“”"
Arguments:
layer_dims – python array (list) containing the size of each layer.

Returns:
parameters -- python dictionary containing your parameters "W1", "b1", ..., "WL", "bL":
                W1 -- weight matrix of shape (layers_dims[1], layers_dims[0])
                b1 -- bias vector of shape (layers_dims[1], 1)
                ...
                WL -- weight matrix of shape (layers_dims[L], layers_dims[L-1])
                bL -- bias vector of shape (layers_dims[L], 1)
"""

np.random.seed(3)
parameters = {}
L = len(layers_dims) - 1 # integer representing the number of layers
 
for l in range(1, L + 1):
    #(≈ 2 lines of code)
    # parameters['W' + str(l)] = 
    # parameters['b' + str(l)] =
    # YOUR CODE STARTS HERE
    parameters['W' + str(l)] = np.random.randn((layers_dims[l],layers_dims[l-1]))*np.sqrt(2/layers_dim[l-1])
    parameters['b' + str(l)] = np.zeros((layers_dims[l],1))
    # YOUR CODE ENDS HERE
    
return parameters

My error is
mtrand.pyx in numpy.random.mtrand.RandomState.randn()

mtrand.pyx in numpy.random.mtrand.RandomState.standard_normal()

_common.pyx in numpy.random._common.cont()

TypeError: ‘tuple’ object cannot be interpreted as an integer

That is not the correct syntax for invoking np.random.randn. It takes a list of dimension arguments, not a tuple. It is different than np.zeros. Why, you ask? The answer is “because”. Seriously. They are just different APIs and they work differently. This is the joy of Open Source Software: the two APIs were written by different people at different times with different ideas in mind. There is no unifying principle or conceptual model. You just have to read the documentation and “deal with it”.

4 Likes

Thanks a lot, really appreciated.