Wk 3 Programming Assignment Exercise 3 has broken me

Ok. Referring back to previous optional labs on the python implementation of gradient descent for logistic regression, there are examples such as this for regularized gradient descent:

And for gradient plain old descent, there’s this:

When I look at the programming assignment, it gives me a suggested structure as follows (no final code, just the template here):

The suggested structure is so different from the other examples with different variable names that I’m finding myself completely confused.

And I thought I was doing so well.

I know…real life, you can’t expect the same var names, blah blah blah…but it’s also the structure as to where the for i in range(m) and for j in range(n) loops fall relative to other code. It feels so different from all the other examples I’ve seen so far.

I’m so confused. So, I don’t know…is there a word of wisdom from someone here? What am I missing?

For now, I’m going back through my notes and trying to re-understand those arlgorithms so I can reinterpret to this new structure. But damn…this has thrown me hard.

Hello pchittum,

I can understand the confusion here :slight_smile: related to the optional and assignment lab different in template code.

The current assignment lab is an updated version of the assignment and that’s why you see variation. When I had done the assignment even I got stuck for these cells, but the hints given below these grader cells, helped me a lot with my confusion.

Probably if I give you some pointers, it might help you too

  1. Firstly, the err_i code given in the optional lab is not required to be recalled separately in the assignment lab.

  2. The updated current assignment lab is actually more simpler form of the optional lab.

  3. The template code is given to make your work more simpler by not letting you hard code the parameters and only write codes as per the template code.

  4. They have basically recalled f_wb = wx +b equation here as z_wb and then they have recalled f_wb separately to recalled that code with sigmoid of z_wb.

  5. Other derivatives again doesn’t require you to require call separate the err_i but a simpler version is given in the hint section. Please refer them.

  6. Once refer the hint section below the
    <# UNQ_C3
    <# GRADED FUNCTION: compute_gradient

if the hints are still confusing, let me know!!!


My word of wisdom is:
The for-loops can cause a lot of confusion, with all those index values and hand-crafted summations.

So don’t use them.

Use numpy matrix math functions, and you don’t need any for-loops at all.

You don’t need to accumulate any sums yourself. because np.dot() and np.matmul() will do all that for you automatically.

Hello @pchittum,

Let’s try to look at the codes in a different way.

This one, for example, is trying to implement some formulae that I have shown on the right hand side:

You see, if you analyze the code like I did, you see what each line is doing. Right? I only showed the connections for dj_dw but I am sure you can draw the connections for dj_db yourself.

Now, if there is anything I wish you to get from this code, I hope it is going to be how you connect the code to the formulae, AND the same connections but in the reverse order. The reverse order is important when you have to implement the code FROM the formulae on your own, right? Let’s look at the template code and put some formulae beside:

The spirit of the assignment is, can you find the connections and implement the code ACCORDING to the formulae?

This is the spirit because as you move on along the courses, you will be kept asking to implement GIVEN formulae or algorithms. This might be a challenge but this might also make the course’s expectation on you clear - because it just keeps asking you for a similar kind of work so that you can really tune yourself in that direction to complete everything :wink:

As a machine learning guy, we often have to implement things according to plans and this is the same kind of challenge. Now the assignment has laid out the plan for us, and we read, understand and implement them. Probably the one difference between the current us and the machine learning guy (the future us) is that, besides just implementing, we need to also take the opportunity to review and make sure we have understood the concept well. Oftentime people feel they have got nothing after completing the specialization, maybe you can make a difference by going through the assignment a bit more thoroughly? But it is your way so I cannot tell what you are going to get finally, but I hope through the works, it is going to be a fun and enriching experience!

Good luck!

@pchittum, just one more comment.

Good variable names are important for us to read the code, but as you know, when you do your own project in the future, all names are going to be up to you. On the other hand, when you read your colleague’s or friend’s codes in the future, all names are going to be up to them.

So, the names are not guaranteed to be very much recognizable or similar to any MLS’s lab.

Then, what’s the key? The power to connect code to formulae!

Let’s imagine that I am shown my friend’s code, and I am told what the code is about (by my friend or my friend’s documentation). What my brain is going to do is to convert “what the code is about” into some plans of formulae or algorithms that I know it may be going to work that way, then my brain is going to find a place in those plans for each line of the code shown to me, and if I can find a place for this line or that line, it means that I change from “kind of knowing” the meaning of a variable in the line to “confirming” the meaning of the variable. Well, the best hope is my friend and I are both using names rationally, but who knows? Haha, but the bottom line is, I know how to connect!

Forgot to mention, I saw this from your screenshot:


This is a great, wonderful way to find out what’s going on in the code. This was what I would do when learning or confirming a piece of code. This was what I would do if I was debugging code (although there are other more jupyter ways for inspecting your variables, but print will just work).

Seeing this alone is a good sign :wink: Good job, @pchittum!


Thanks for the thoughtful comments everyone. I appreciate that I was perhaps being a bit over dramatic.

I especially appreciate the time each of you have taken to explain things out.

And @TMosh , your advice to just skip the iteration and use NumPy’s vector math features was really helpful. That was kind of what I needed to hear from the get go: “just do your thing”. Part of what was breaking my brain was being told to iterate where the lessons had explicitly told me that NumPy would be faster using vector math.

Part of my frustration was not understanding if the grading that I knew that would happen would evaluate the specific steps outlined (or even look for specific variable names, etc.). And that template just didn’t map to how I was thinking about these algorithms at all.

To your point, Raymond, I had planned to sit down with the formulae from the lessons and then implement the code in a way that made sense to me. But when presented with that template, I was stuck between what I wanted, and a structure that looked alien to how I wanted to proceed.

So if any curriculum folks are reading this, would you please put some notes in the code comments that if we want to ignore your suggested template, we can? That would have helped me greatly in this situation.

All in all, I passed. I do have an incorrect implementation in C4, but I’ll take the pass for now and go back and understand it next week.

Good to know. I’ll look around the notebook menus for some kind of debug options. It’ll be super helpful going forward.

:+1: Search for “jupyter magic”.


I suggested “jupyter magic” because these courses only give you Jupyter Notebook. However, besides the courses, you might be using Jupyter Lab on your own machine and in that case, you might google “jupyter lab debugger” to find relevant articles. The jupyter-lab provided a more GUI-kind of interface and should be more convenient.

Back to “jupyter magic” since our courses live in Jupyter Notebook, you should be able to find jupyter’s official doc for magics, then you might read about %pdb first but there are other useful magics too. Maybe I could write an article about that? What do you think?


@pchittum, thanks for your report.