Week3 lab 6


In equation (2)
image
the gradient of Wj is the sum of the losses of all examples multipled by x[i,j] and divided by number of examples. But why in the code implementation, the looping of examples is outside looping of the features? Then it becomes update each feature’s weight by one example instead of all examples?

Hi @flyunicorn

The code is correct. The code initializes dj_dw to zeros before the loops and accumulates the contributions incrementally. The outer loop iterates over each example i and calculates its error err_i. The inner loop then iterates over each feature j and adds them the term err_i * X[i,j] to the running sum stored in dj_dw[j]. After the outer loop completes, dj_dw[j] keeps the total sum sum (err_i * X[i,j]) over all examples i for that specific feature j (exactly as required by the formula) before the final division by m calculates the average.

Hope it helps! Feel free to ask if you need further assistance.

Hi @flyunicorn ,

The ‘Hints’ under the function definition explained clearly the code structure and what each code block does. I find it helpful for check of understanding and code logic. If you have not used it in the past, I highly recommend it. Just click on the link and it will open up a sub-window on your notebook.

Please bear in mind of the forums’ code of conduct policy and refrain from posting code on the forum. The code element of your query will be removed to keep in line with the forum policy.

Ah, I will have to remove my code then :face_with_monocle: It is the solution to the problem if I understand correctly

Hi @dtonhofer ,

I didn’t check your code, but just my 2 cent of my understanding of how the forums operate.

Prof Ng always try to start from a simple way to illustrate the point/topic, and then gradually brings the learning content to a higher/deeper/ level, or suggests alternative methods. You can say this is Prof Ng’s hallmark of teaching.

1 Like

This is how to do it.

1 Like

But if I swtich the 2 loops, is it also correct?

{moderator edit - solution code removed}

Where is the “Hints” you mentioned? Strange I don’t find it under the function definition.

Hi @flyunicorn ,

The lab you are referring to is actually an optional lab. For the optional lab, there is not ‘hints’. But there are ‘hints’ offering assistance at various places along the graded notebook. You will see Click for hints in green colour just below the code cell of the function definition.

Hi @flyunicorn ,

What is your reason for switching the 2 loops?

Just wondering why one set up is chosen over the other? Can I conclude that switching the 2 loops will yield the same result?

“Switching the loops” is only rarely possible.

In this case it wouldn’t even make sense to “switch the loops”.

Python seems particularly bad at making code easy to read, but consider this tree of operations:

You cannot exchange the lower and upper green boxes because the would destroy the flow of data, in particular the one for err_i.

I’m not sure I can post this, but here is a version that doesn’t even have an inner loop (the original version should not have one either), because it is done via vector operations. I have added the type annotations, which should be in those exercises. Leaving them out doesn’t help understanding, on the contrary, it transforms the code int slop: it just gives the illusion of understanding as one glances at the visually apparently agreeable code, whose actual complexity is not taken in. Python already does not demand that variables be properly declared and does not restrict them to the scope of the local block, two egregious design errors.

Enough ranting, here we go:

{mentor edit: code removed}

@dtonhofer,
I think it would be best if you do not post code that can be used for other graded assignments in the course.

That’s the thing - I don’t know whether it can. (I don’t know how that course is structured)

It’s such basic code. The alternative would be to add spoiler tags …

Also, it’s already in the response Week3 lab 6 - #9 by flyunicorn

I’ve removed the code from your reply.

The code that was posted earlier in this thread was provided with the optional lab. So posting it on the forum does not reveal anything unique.

Well, okay, but it’s the same code, at least functionally.

i am having the following problem in Week 3 Graded Practice lab


What is the supported format string?

Sorry, meant unsupported format string.

First guess is that your compute_cost() function is returning the “total_cost” variable as equal to None.

1 Like