Random Initialization Exercise 2 - initialize_parameters_random

def initialize_parameters_random(layers_dims, scale=0.01):
    layer_dims -- python array (list) containing the size of each layer.
    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)
    parameters = {}
    L = len(layers_dims)

    {moderator edit - solution code removed}

    return parameters```

But I have error:

W1 = [[ 0.01788628  0.0043651   0.00096497]
 [-0.01863493 -0.00277388 -0.00354759]]
b1 = [[0.]
W2 = [[-0.00082741 -0.00627001]]
b2 = [[0.]]
 2  Tests passed
 1  Tests failed
AssertionError                            Traceback (most recent call last)
<ipython-input-33-eb6315d765ef> in <module>
      4 print("W2 = " + str(parameters["W2"]))
      5 print("b2 = " + str(parameters["b2"]))
----> 6 initialize_parameters_random_test(initialize_parameters_random)

~/work/release/W1A1/public_tests.py in initialize_parameters_random_test(target)
     65     ]
---> 67     multiple_test(test_cases, target)
     69 def initialize_parameters_he_test(target):

/opt/conda/lib/python3.7/site-packages/dlai_tools/testing_utils.py in multiple_test(test_cases, target)
    162         print('\033[91m', len(test_cases) - success, " Tests failed")
    163         raise AssertionError(
--> 164             "Not all tests were passed for {}. Check your equations and avoid using global variables inside the function.".format(target.__name__))

AssertionError: Not all tests were passed for initialize_parameters_random. Check your equations and avoid using global variables inside the function.

You filed this question under a generic category, so we cannot tell which Specialization and Course you are dealing with here. The code looks reasonable to me. The only question is whether you are using the correct random function for the W values. What do the instructions say? You are using the Normal (Gaussian) Distribution. Is that what the instructions ask for?

To get help from someone who knows your course, please move the thread to the correct Course Q&A category by using the little “edit pencil” on the title. Or just tell us the course and I can move it for you.


Also, at least in your code shown I’m not sure where this scale=0.01 is coming from. It isn’t listed in the function arguments list…


It is in the code as shown in the post. But the question is where this code came from. If you have extra knowledge and are aware of which course and assignment is referred to here, then are you sure that agrees with what the poster intended? There is a function by that name in the DLS C2 W1 Initialization assignment and that one does not include a scale argument, but I am not familiar with any of the MLS assignments.


Unfortunately Paul I don’t know either-- Sounds like a generic initializer for any L[n] network. As you said the poster will have to provide more details. Of course I didn’t see what they originally posted.

By mentioning the inclusion of scale I meant to point out that in my limited experience the course(s) code is pretty good at specifying in comments the function I/O (ie:)

    layer_dims -- python array (list) containing the size of each layer.

So its absence makes me a little suspicious… Perhaps it is from one of the new courses ? In any case, I haven’t seen it.

1 Like

Double-check the layer dimensions (layers_dims ) and ensure no global variables are used inside the function. Verify if the random seed and scale parameter values match the test cases.

Yes, im using Gaussian variable (numpy.random.randn())

Yep! You are right! Scaled should be 10 and not 0.01.