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