Course Week 1 exercise 3

Hi all,
very strangely i am getting the syntax error as below. I ran the same in previous exercise and it run perfectly.
anyone can help, whats wrong here?

File “”, line 28
parameters[‘b’ + str(l)] = np.zeros((layers_dims[l],1))
^
SyntaxError: invalid syntax

1 Like

Hi Hamid,

Can you please share the screenshot of above lines of code as well?

as below:

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])) * int(sqrt(2./layers_dims[l-1])
        parameters['b' + str(l)] = np.zeros((layers_dims[l], 1))
      
        
        # YOUR CODE ENDS HERE
        
    return parameters

Thanks for sharing.

The issue is that in above line you have got one closing bracket missing for int()

the correct statement should be:

parameters[‘W’ + str(l)] = np.random.randn((layers_dims[l], layers_dims[l-1])) * int(sqrt(2./layers_dims[l-1]))

1 Like

Thank you @c.godawat , it is fixed however another error poped up:

TypeError                                 Traceback (most recent call last)
<ipython-input-32-0e37753f3d91> in <module>
----> 1 parameters = initialize_parameters_he([2, 4, 1])
      2 print("W1 = " + str(parameters["W1"]))
      3 print("b1 = " + str(parameters["b1"]))
      4 print("W2 = " + str(parameters["W2"]))
      5 print("b2 = " + str(parameters["b2"]))

<ipython-input-31-12b1458e6396> in initialize_parameters_he(layers_dims)
     25         # YOUR CODE STARTS HERE
     26 
---> 27         parameters['W' + str(l)] = np.random.randn((layers_dims[l], layers_dims[l-1])) * int(sqrt(2./layers_dims[l-1]))
     28         parameters['b' + str(l)] = np.zeros((layers_dims[l], 1))
     29 

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

Hi @Hamid_ILBEYGI

The issue now is that you are passing tupple in randn.

The correct statement should be:-

—> 27 parameters[‘W’ + str(l)] = np.random.randn(layers_dims[l], layers_dims[l-1]) * int(sqrt(2./layers_dims[l-1]))

For further understanding of the function, I would highly recommend you to read this documentation.

https://numpy.org/doc/stable/reference/random/generated/numpy.random.randn.html

Hope this helps! :slight_smile:

1 Like