Some confusion in Course 1 Week 2

Hello everyone! I hope all is well.
I have some confusion regarding the code of course 1, week, 2 “C1_W2_Lab02_Multiple_Variable_Soln”.

As you see in the attached file (1), why cost is set to zero?
In fig (2), what is theta?
And, can you please explain the code of fig (3)?
Moreover, what does this line of code mean? plt.style.use(‘./deeplearning.mplstyle’)

Finally, kindly recommend any Python course for Machine Learning, as I do not understand some code here.

I really appreciate any help you can provide.
Regards,
Saif Ur Rehman.



Hey @saifkhanengr,
Let me try to answer your questions one-by-one.

0 is just defined as the initial value for the cost. If you go through the code of the compute_cost function, you will find that it computes the cost according to the formulations that have been discussed in the lecture videos. Please note that you can’t use any other initial value here, due to the way, the code has been written.

Here, theta refers to the parameters that are updated in this function, and for which, we are performing the gradient descent. For this particular scenario, theta refers to w and b, i.e., the weights and bias parameters respectively. I am assuming they wanted to give a generic definition for the function, and hence, they used theta instead of weights and bias, since gradient descent is a generic concept of optimisation, and not restricted to machine learning only.

if i<100000:      # prevent resource exhaustion 
    J_history.append( cost_function(X, y, w, b))

The above 2 lines of code simply stores the cost for each iteration in a list J_history. It also ensures that only the first 100K values are stored due to restrictions on memory requirements. However, in the notebook, num_iterations = 1000, so, we don’t need to worry about this.

if i% math.ceil(num_iters / 10) == 0:
    print(f"Iteration {i:4d}: Cost {J_history[-1]:8.2f}   ")

The above 2 lines of code, simply considers the entire gradient descent procedure in the form of 10 equally spaced intervals, and prints the cost at the starting of every interval.

For instance, if num_iters = 264, then num_iters / 10 = 26.4 and math.ceil(num_iters / 10) = 27. Here, you can find how to use math.ceil() function. So, at multiples of 27, starting from 0, 27, 54, and so on, up to 264, these lines of code print the value of the cost.

You can find the answer for this query here.

Here and here, you can find some great resources for starting with Python.

Let me know if this helps.

P.S. - You have posted this discussion in the wrong section. Let me move it for you by using the little pencil icon next to the title.

Cheers,
Elemento

4 Likes

Hello Elemento! I am extremely sorry as I posted in the wrong place.
Regarding your explanation, you explained everything in great detail and no doubt left. Thanks for such a thorough elaboration. This helps a lot.