Exercise 7: only size-1 arrays can be converted to Python scalars

When submitting the final assignment in Week 3 I’m getting 0/20 points on Exercise 7 even though the notebook ran w/o error and passed all the unit tests. The error I get from the grader is:

There was a problem grading your submission. Details:
only size-1 arrays can be converted to Python scalars

Is anyone familiar with this problem and able to give me a hint on what might be going wrong? Thanks.

Try searching the forum for the words “size-1 array”.

If the search Tom suggests doesn’t turn up anything, note that the error message is pretty specific. A lot more so than you normally get from the grader. :laughing:

Of course the first question is whether all the tests in the notebook pass. If that’s true, then the cause must be something about your code that is not general, meaning that it fails with a different test case. But the first thing I would look for is instances anywhere in the code of nn_model or any of the functions it calls (which is all the functions we just wrote) in which an array is assigned to a scalar value. How could that go wrong in that you end up with an array with more than one element? One place would be dealing with the return value of compute_cost.

1 Like

Thanks, both, for the suggestions. I spent an hour doing a line-by-line code review and improving things that I thought might have been problematic, but haven’t been able to track this down yet. I also wrapped the entire body of the nn_model() function in a try block, catching the exception and replacing it with my own. However, the grader is still throwing the original error. It makes me think that the problem isn’t inside the nn_model() function or any of its other function calls.

Is there any way for me to increase the detail of the output or debug in a more efficient way?

Did you rename your notebook file? That would cause problems.

Maybe it’s time to just look at your code. We shouldn’t do that on a public thread, but I’ll send you a DM about how to do that.

1 Like

No, I didn’t rename the notebook. I sent the notebook to Paul via DM. Hopefully he can figure out what I’m doing wrong.

1 Like

Ok, well it was definitely worth looking at the code. I’ve never seen this bug before and there’s no way I could have figured it out just from the error output. In fact, it’s still a little unclear why the grader throws that particular error.

The sigmoid activation function was implemented for scalars only using math.exp after manually importing the math library. Then @MikeML gets bonus points for creativity by using the numpy vectorize() function as a wrapper on sigmoid to allow it to handle array inputs when invoking it in forward propagation. I definitely learned something here: that was completely new to me. Not that I’m recommending using that technique, mind you … :laughing:

The slightly shocking thing is that the test cases in the notebook and the grader accept the scalar only sigmoid. I’ll file a bug about that. I will also chide them for not setting the random seeds in the test cases, so that the test cases in the notebook could actually check the outputs. They based this assignment on DLS C1 W3, so why did they go to the trouble to remove the seed setting logic? Hmmmmmm.

There was also a more subtle bug in nn_model in terms of how it was calling the “helper” functions. But the sigmoid problem was the cause of the “size-1” errors.

1 Like

To give a little more detail here, if you try the implementation with scalar only sigmoid and np.vectorize in forward_propagation, the grader gives full points for forward_propagation and all other functions except nn_model. Of course nn_model nowhere calls sigmoid directly: it only calls forward_propagation. But the error it gives for nn_model in that case is the one Mike quoted in the initial post on this thread:

I do not have visibility into how the grader is implemented or what the test cases are for this assignment. I’m having real trouble coming up with a theory of the case, but the best I can think of is that grader test for forward_propagation is somehow too simplistic and then there is something more complicated about the definitions and dimensions of the objects in the nn_model test case.

1 Like

Thanks for the help, Paul. All good now :slight_smile:

1 Like

That’s great news! This was a fun topic. The best case scenario is where everybody learns something in the process. And we also gained some knowledge that I hope will improve the course in the future by enhancing some of the test cases.