Course1 Week 3 Exercise 8 TypeError: only integer scalar arrays can be converted to a scalar index

Hello Team,

I am stuck in the exercise 8 ( course 1 week 3 ) following error throws when i am calling the initialize_parameters function.
Error


TypeError Traceback (most recent call last)
in
----> 1 nn_model_test(nn_model)

~/work/release/W3A1/public_tests.py in nn_model_test(target)
261
262 t_X, t_Y = nn_model_test_case()
→ 263 parameters = target(t_X, t_Y, n_h, num_iterations=10000, print_cost=True)
264
265 print("W1 = " + str(parameters[“W1”]))

in nn_model(X, Y, n_h, num_iterations, print_cost)
27 # YOUR CODE STARTS HERE
28
—> 29 parameters = initialize_parameters(n_x,n_h,n_y)
30
31 # YOUR CODE ENDS HERE

in initialize_parameters(n_x, n_h, n_y)
22 # YOUR CODE STARTS HERE
23
—> 24 W1 = np.random.randn(n_h,n_x) * 0.01
25 b1 = np.zeros([n_h,1])
26 W2 = np.random.randn(n_y,n_h)* 0.01

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: only integer scalar arrays can be converted to a scalar index

Please guide me in resolving this error.

Thanks in advance.

Regards,
Abhay

1 Like

Hi @Abhay25

It looks like there is a problem with the way n_x, n_h is extracted. Please check. The code for extracting these values should already be given to you.

2 Likes

Thanks for your inputs.
I tried to understood but in the code i found that n_ x and n_h are the input parameters of the function initialize_parameters.i used these values for calculating the value of W1.
If I missed something then please correct me.
Thanks again.

regards,
Abhay

Hi @Abhay25 ,

The function initialize_papameters(n_x, n_h, n_y) is called within the nn_model(). You should find these lines of code in the nn_model() function:

n_x = layer_sizes(X, Y)[0]
n_y = layer_sizes(X, Y)[2]

These variables together with n_h which is an input argument of nn_model() are to be passed to initialize_parameters() function.

Do you see the n_x and n_y lines of code in the nn_model().

2 Likes

Hi Kic,

Yes, I passed the parameters n_x, n_h and n_y to initialize_parameters() function. Still I am receiving the same error. :frowning:

Thanks,
Abhay

hi @Abhay25

If A and B are two 2D numpy.arrays, then numpy.concatenate(A,B) yields the error.

The fix was to simply to add the missing brackets: numpy.concatenate( ( A,B ) ), which are required because the arrays to be concatenated constitute to a single argument.

so check you parameter intialization has been recalled as per above mentioned, I noticed you have used an incorrect bracket.

Hope this helps!!

Regards
DP

Hello Deepti,

Thank you for your help.
I am little confuse in the usage of numpy.concatenate in my code snippet.
I am not getting where exactly I need to add the missing brackets.

could you please give me more hints so i can try it again ?
Thanks in advance.

Regards,
Abhay

I think you are missing the point that Kic is making. The question is how the values of n_x, n_h and n_y are determined in nn_model before you pass them to initialize_parameters. Either n_h or n_x must not be a scalar integer value. Print them out before the call to the initialize routine. Kic’s other point is that all those values are just given to you, so if they are incorrect, you must have modified part of the code that you did not need to modify.

2 Likes

:point_up_2:

I confess that I am also confused by this. I did not use np.concatenate anywhere in my code. It should not be required. So Deepti must be making some higher level point here.

Maybe one relevant point to make is to note that the APIs to np.zeros and np.random.randn are fundamentally different. np.zeros takes a single “tuple” as the argument giving the dimensions you want. For np.random.randn, you just give a list of individual parameters as the dimensions. But we’re not really talking about how those APIs work here: your initialize routine already passed the local test cases, I assume, right? The question is what the values are that you are passing from nn_model when you call the lower level routine.

1 Like

But that’s not the line that “threw”, right? It was the randn line that threw. And if the code passed the test case, I’m guessing that implementation works.

Update: I changed my code to use square brackets instead of parens to create the arguments for np.zeros and it works fine for me and passes the test cases both for initialize_parameters and nn_model.

1 Like

The below link will help you understand more clearly

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

@Abhay25

by any chance did you remove the below codes in the initialise parameter grade cell?

assert(W1.shape == (n_h, n_x))
assert(b1.shape == (n_h, 1))
assert(W2.shape == (n_y, n_h))
assert(b2.shape == (n_y, 1))

Thank you All.
Your valuable inputs helped me in resolving the error in exercise 8.

Regards,
Abhay

Your inputs really helped me.
Thank you very much.

1 Like

what was the issue let us know, so other learners can find your post helpful

initialize_parameters method having following parameters
n_x – size of the input layer
n_h – size of the hidden layer
n_y – size of the output layer

When i called this method from nn_model method I passed the values n_x and n_y return by
n_x = layer_sizes(X, Y)[0]
n_y = layer_sizes(X, Y)[2]

This was the issue,
For resolving this issue, as suggested by @paulinpaloalto I use print statement before the function call.
It returns array for n_x and n_y respectively.
Then I pass respective index of n_x and n_y to the initialize_parameters method.
This resolves the issue.

Thanks,
Abhay

2 Likes