In equation (2)
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?
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.
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.
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.
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 gradednotebook. You will see Click for hints in green colour just below the code cell of the function definition.
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.