I have debugged this locally in VS Code running public_tests.py. The closest I can come to figuring out the issue is that it appears that the cost is being returned as a float but an np.array is expected. However I am not certain that is the issue.
I have actually been at this for a few days so any help will be greatly appreciated.
1 Like
Yes, that seems like the correct diagnosis. Note that if you “hand imported” your functions from the Step by Step exercise, that can cause this type of problem. They provide their own implementations here via an “import” file and some of the APIs are a bit more restrictive than what they accepted as correct in the previous assignment.
To state it more plainly: it is a mistake to copy over your own functions from the Step by Step exercise into this assignment.
As Paul described, I think you have a local copy of compute_cost() function which uses “np.sum” to calculate the cost. In this case, the result is np.float64. If “np.dot” is used, then, the output is np.ndarray, which is the expected one.
Thanks for your replies. The answer turned out to be surprising easy, but unexpected for me. Nowhere is it suggested that the output of the cost function must be an array. I am really surprised that no one else had this issue. Anyway here is what did the trick for me. Note the commented line and the line that was inserted.
{moderator edit - solution code removed}
I am going to post my debugging steps below in case it will help someone else down the line.
If anyone is interested in how to debug Coursera code, see below.
- Download the lab files for your exercise which includes the Coursera utility files. Access a programming assignment in your browser. Click the “Lab Files” button at the top right of your browser window to download the files. You can then examine the code in the utility files (.py files) that include the Coursera testing code.
- Download the .py version of your notebook file. From a programming assignment window in a browser, choose File/ Download as/ Python (.py). This will export a .py version of your Jupyter notebook file.
- Open the .py file that was created from your notebook file. You can then debug your code in VS Code. Set breakpoints in your code and in the utility files as desired.
- VS Code handles your .py file that was exported from a Jupyter Notebook file in a cool way. You can still run cells, yet when you debug, VS Code will jump to any breakpoints you have set in any other cells or files.
- Note that you can debug individual cells in your notebook file without using the .py copy of the file, but the debugger will not move to any other cells or files.
- You may see some answers to exercises in the utility files. Don’t just mindlessly copy the code to your exercise. Remember that no one really cares about your grades except you. The point is to actually learn the material.
- In your .py file click on the cell you ultimately want to run and select “Run all above”.
- Depending upon what type of exercise you are programing, do either of the following.
o Run the Coursera testing function that was included in the exercise.
o Run your model code that calls all your helper functions with breakpoints set there and in the Coursera utility files.
- You can step through your code and the utility files to debug your application. It also a good learning tool to see each helper function called and in what order. You can also examine the variables to see exactly how they are modified and how dot products and sums are calculated.
- Set the number of iterations in the various functions to a low number because you obviously are not going to step through 2500 iterations of a function.
You must be a better typist than I. 
Although, point taken.
I think you are fundamentally missing the point here. The point is not to debug your compute_cost function so that it works. That was in the previous exercise. In this exercise, they provided their own implementation. Look at the “import” cell early in the notebook and then click “File → Open” and have a look at the appropriate dnn_utils “dot py” file.
As I commented in two of my earlier replies: it is a mistake to copy over your own code from Step by Step into this assignment notebook.
If you are running locally, you should simply download all the associated files. There is an easy way to do that, which is described on the relevant topic in the FAQ Thread.
I definitely was missing your point. I had originally downloaded the files which could not be easier or more straight forward. However I did not look at the dnn_app_utils_v3.py file which contains check on the cost function.
When you said it’s a bad idea to copy over the code from the step by step exercise to the application exercise, I thought you just meant that in general you can get into trouble when copying code from one place to another. Some people discourage that because it’s easy to miss something that way.
Now I understand that you actually meant that it was specifically a bad idea to copy the code as mentioned above because the tests (assert statements) are different!
I did check out the utility file that contains the assert statement for the cost function. It is:
assert(cost.shape == ())
This ensures that the value is a scaler contained as an element in an ndarray as opposed to just a scaler. (Not really sure why this is important…)
Separately, the instructions for the application exercise specifically state, “…you’ll use the functions from the previous assignment to build a deep network” which says to the uninitiated such as myself, copy and paste. Perhaps there should be a caveat that some modifications to the functions might be required.
Anyway, I believe that now I take you meaning. Thanks very much for steering me in the right direction. I understand that like all of us you have other things to do.
P.S. I am going to modify my answer above (tomorrow) so as not to encourage others to copy the utilities and to be wary of just copying their code from the step by step exercise to the application exercise.
I think I understand your points. As you see, this is a kind of “black box” testing. Given some assumptions, a test program calls target functions and checks results. Then, check shape, type, value and so on. In some cases, a test program feeds an error case to see that is properly handled.
If any of assumptions are broken, then, a test program most likely returns an "error. In the worst case, the behavior of a test program is unpredictable.
I have all programming assignments in my local environment, and uses multiple conda environment. I sometimes change even original code. But, for testing, I put back original one. In some cases, I need to use the same version of tensorflow and others as the grader environment, which is the reason that I have multiple conda environment. (Depending to exercises, versions of key components are different unfortunately…)
If you design and write code by yourself, then, you can define a cost function to return a float value. But, this is not the case. We also guess what happened from description pasted, which seems to be a track back of a black box testing.
As a helper program uses np.dot, then, we can guess which assumption was broken.
Sorry that everything is not flexible, but please understand a black box testing is pretty much focusing on the target program only with fixed assumptions.
Hi, Michael. I’m glad to hear that you’ve got everything sorted now. It’s a good point that they could make the instructions a bit more clear w.r.t. the utility functions. I think they were just hoping that you would naively assume that you can actually call functions in another notebook, but of course that it not actually possible. If they made a big point of the fact that they were importing their own versions of the Step by Step utility functions, it would have revealed that they were giving you the worked answers to the previous exercise. That’s my best guess for why they weren’t a bit more explicit about all this. Of course if you copy the code from their “dot py” file back to Step by Step, you’d actually fail in at least one case. If you look carefully, you’ll notice that they used a different and more sophisticated initialization algorithm in the “deep” init function, which we’ll learn about in Course 2. It turns out that the simplistic init that they had us build in the Step by Step exercise gives really terrible convergence in the 4 layer case. 
Hi Paulin and Nobu,
It’s almost impossible to design a system that handles every possible outcome. Users will invariably take some unanticipated actions. Even Microsoft and Google who together, have more money than the gross national product of Demark, are always releasing patches and updates.
Overall, I think the back end of the testing platform is very effective, even if not absolutely perfect. The code is elegant.
The overall design of the Coursera experience is excellent as reflected in the 4.9 user rating. You cannot go too far wrong when you have Andrew Ng explaining things to you.
Further, the course structures were well thought out. It’s obvious that they were not designed by coders or AI wizzes alone. All coders secretly think they could design a UI or a UX if they wanted to lower themselves to such an endeavor. However, I am guessing that you both would agree that couldn’t be further from the truth.
It’s obvious that teachers and professors were involved in the development of the Coursera/ Deeplearning.ai experience. The lessons are straight forward and build on each other. The arbitrary deadlines and quizzes, the results of which no one will ever see, are extremely helpful. Even if we are long past college age the concepts of deadlines and grades are deeply embedded in our psyches so they are effective motivators.
So even if this is a bit off topic, I just wanted to compliment you and your team for such a tremendous job on what you have done, and what you are a part of.
I posted a response to Paulin that was actually to you both.
Hi, Michael.
It’s great to hear that you have found the course(s) so beneficial. Sorry if this seems a bit pedantic, but I wanted to make sure the “roles and responsibilities” are clear:
Coursera is the “platform provider”. They provide the mechanisms that underlie everything, but not any of the actual course content.
The course material is all the responsibility of DeepLearning.AI. Of course it happens that Andrew Ng is the CotB of both Coursera and DeepLearning.AI, so he is on “both sides” of that transaction, but that is essentially irrelevant. There are completely different teams doing the platform work versus the actual courses.
The mentors (Nobu and I for example) are fellow student volunteers. We have no employment relationship with either Coursera or DLAI. Our participation is to answer questions and facilitate the discussions on the forums. We can claim no credit for the quality of the course material, although we do provide the course teams at DLAI with detailed feedback where required and can file bugs when we find problems.
Thanks for the clarification. Your help and time are much appreciated.