Questions in the last part of Gradient descent function

Hello there!
I am a beginner in Python, then I have a very basic question in gradient descent function code. I am going to know what are the meaning of

  1. Iteration {i:4}: Cost {J_history[-1]:0.2e
    ,
  2. dj_dw: {dj_dw: 0.3e}
    in:
    if i% math.ceil(num_iters/10) == 0:
    print(f"Iteration {i:4}: Cost {J_history[-1]:0.2e} ",
    f"dj_dw: {dj_dw: 0.3e}, dj_db: {dj_db: 0.3e} “,
    f"w: {w: 0.3e}, b:{b: 0.5e}”)
    ,
  3. {w_final:8.4f}?
    And
  4. why J_history.append( cost_function(x, y, w , b)) and p_history.append([w,b]) are written in the for loop in gradient descent function? if they are out of the for loop, what’s happend?
    Thank you for your help in advance!

Hi @Mahshid_Khazaei_Shad your questions code statements didn’t look clear to me, Can you post image of the code you are talking about.

If you are taking about optional lab for gradient descent then,

  1. in Iteration {i:4} - {i:4} styles your output (it atleast prints 4 spaces even if there is nothing to print for y), try removing :4 and rerun your code, you will find the difference in the output style.
    while J_history[-1]:0.2e is used to display last element of J_history array upto second decimal poin, in terms of power of e.

  2. dj_dw: {dj_dw:0.3e) is used to display three dj_dw upto three decimal points, in terms of power of e.

  3. {w_final:8.4f} for printing at least 8 spaces(again for styling like in 1), if you understood 1, then you can understand it easily) and upto 4 decimal points.

  4. J_history and p_history are written inside the loop to add error and weights calculated at every iteration of gradient descent algorithm. If you only add final values of cost and weights, then you cannot visualise how cost function changed with weights or number of iterations.

3 Likes

Thank you so much ritik5. The explanation were great and very clear.

@Mahshid_Khazaei_Shad I am Glad that it helped you.

1 Like

Hello again ritik5!

I did understood everything that you explained to me except two things!!

  1. Iteration {i:4}
    As you offered, I removed :4 but I did not see any change in the output of the code. I am still confused!

  2. {w_final:8.4f}. in this case, why we should determine 8 spaces? and spaces for what?

Thank you for your help in advance.

HI @Mahshid_Khazaei_Shad,

  1. If you don’t use :4 , then you will find the output correspond to Iteration unstructred (0 will not be in the right corner).
    illustration: with :4


    without :4

  2. You can look the red underlined text in image 1, there are total 8 elements counting . over there.

1 Like

Thank you. Now I got them.