Week 4, Assignment 2, Application

Hi, I’m stopped by the error message at L-layer neural network.

Tried many time, but still cannot fix the “too many indices for array”.

Help needed. Thanks.

You could gain some insight by printing out the type of costs, though the error message is already giving us the clue that it is single-valued, and thus does not support the Python index operator [ ]

Here is how I would think this one through. We know, or can guess that even though costs is spelled like it should be multi-valued, it is not. How could that be? Where is costs Initialized? It is assigned one of the return values from the L_layer_model()function. So take a look at what is going on at the end of that function. The single value cost is appended to the multi-valued costs and when the iteration is done we return parameters and … oops

There is also another little problem in that code related to costs vs cost. You have written np.squeeze(cost) but cost is a scalar so squeeze doesn’t make any sense there.

Unfortunately that np.squeeze is part of the given template code. You’re right that it doesn’t make sense, but it apparently does no harm.

Notice that’s not the only wrong but harmless code in that template section:

if i % 100 == 0 or i == num_iterations:
            costs.append(cost)

The way that “for” loops work in python the second condition on that “or” will never be true, right? Contrast that with the boolean expression on the previous “if” which is correct.

Two bugs in four lines of given code, so they’re batting 500 here. :grin:

But the real problem here is that the return statement was part of the template code and should not have been modified, but it looks like Jason got a little creative with that.

Jason, if you want to get a clean copy of the notebook to see how that code originally looked, there is a topic about that on the FAQ Thread.

Thank you both for your help. I had the problem fixed by restarting the code from a clean copy!

1 Like

Glad you got it sorted @Jason_Wang1 and sorry for attributing inattention to detail on the part of the course developers to you. It’s hard to write clean code!