Week 3 Exercise 3 initialize_parameters - "data type not understood"

Function initialize_parameters looks very simple - dimensions of matrices / vectors are given with initialisation functions. But while my initial matrix is set ok, the following vector (column) will not initialise. I’ve printed out the values of the variables passed into the function in case there was a problem with the test. “data type not understood” I cannot fathom :frowning:

See below - thanks!

n_x = 2 n_y = 1 n_h = 4

TypeError Traceback (most recent call last)
1 np.random.seed(2)
2 n_x, n_h, n_y = initialize_parameters_test_case()
----> 3 parameters = initialize_parameters(n_x, n_h, n_y)
5 print("W1 = " + str(parameters[“W1”]))

in initialize_parameters(n_x, n_h, n_y)
24 print('n_x = ‘, n_x,’ n_y = ',n_y, ’ n_h = ',n_h)
25 W1 = np.random.randn(n_x,n_y) * 0.01
—> 26 b1 = np.zeros(n_h,1)
27 W2 = np.random.randn(n_y,n_h) * 0.01
28 b2 = np.zeros(n_y,1)

TypeError: data type not understood

Hey @Brendon_Wolff-Piggot,
As the error suggests, indeed numpy doesn’t understand your data-type :nerd_face: Let’s see why numpy refuses to do so. If we take a look at the documentation of np.random.randn, we will find that it takes dimensions in the form of separate arguments, i.e., if we need a (2, 3) dimensional matrix of random samples drawn from a standard normal distribution, we will use:

np.random.randn(2, 3)

However, the same format doesn’t work for numpy.zeros and numpy.ones. If we take a look at the documentation of np.zeros, we will find that the shape needs to be passed either as an integer (single dimension case) or as a tuple of integers (multiple dimension case). So, all you need to do is use np.zeros(n_h,1) and np.zeros(n_y,1) as

np.zeros((n_y, 1))

Now, as to why numpy provides 2 different formats like this, this is primarily to support different packages, for instance Matlab. If you check out the documentation of np.random.randn(), you will find the below to be mentioned:

This is a convenience function for users porting code from Matlab, and wraps standard_normal. That function takes a tuple to specify the size of the output, which is consistent with other NumPy functions like numpy.zeros and numpy.ones.

You will find a similar thing mentioned in the docs of various other functions provided by Numpy as well. I hope this helps.


Great! Your advice fixed that error!

Now I pass the tests, but there is still an error stating that the shape of the first matrix is wrong. Looking at the output it seems that the matrix size parameters have somehow been incremented after the matrices were defined.

And even though the test is marked as passed the outputs do not match?

I have only one print statement and that is before the matrix / vector sizes are assigned:

n_x = 2 n_y = 1 n_h = 4
W1 = [[-0.00416758]
b1 = [0. 0. 0. 0.]
W2 = [[-0.02136196 0.01640271 -0.01793436 -0.00841747]]
b2 = [0.]
n_x = 3 n_y = 2 n_h = 5

AssertionError Traceback (most recent call last)
8 print("b2 = " + str(parameters[“b2”]))
—> 10 initialize_parameters_test(initialize_parameters)

~/work/release/W3A1/public_tests.py in initialize_parameters_test(target)
52 assert type(parameters[“b2”]) == np.ndarray, f"Wrong type for b2. Expected: {np.ndarray}"
—> 54 assert parameters[“W1”].shape == expected_output[“W1”].shape, f"Wrong shape for W1."
55 assert parameters[“b1”].shape == expected_output[“b1”].shape, f"Wrong shape for b1."
56 assert parameters[“W2”].shape == expected_output[“W2”].shape, f"Wrong shape for W2."

AssertionError: Wrong shape for W1.

Expected output

W1 = [[-0.00416758 -0.00056267]
 [-0.02136196  0.01640271]
 [-0.01793436 -0.00841747]
 [ 0.00502881 -0.01245288]]
b1 = [[0.]
W2 = [[-0.01057952 -0.00909008  0.00551454  0.02292208]]
b2 = [[0.]]
All tests passed!

Hi, Brendon Wolff.

Look at the instructions given in the notebook.

Make sure your parameters’ sizes are right

W1 – weight matrix of shape (n_h, n_x).

This is causing error in its shape.