Gradient Descent Lab

plt_gradients(x_train,y_train, compute_cost, compute_gradient)
plt.show()

When I run this code on google collab,it gives error. Please explain why .

Hi @Ashutosh_Agrawal
Welcome to the Community!

There aren’t an command in python programming language called plt_gradients(…), it’s an function that made for you in the assignments to plot the steps of gradient… the implementation of this function is in lab_utils_uni file it is

def plt_gradients(x_train,y_train, f_compute_cost, f_compute_gradient):
  #===============
  #  First subplot
  #===============
  fig,ax = plt.subplots(1,2,figsize=(12,4))

  # Print w vs cost to see minimum
  fix_b = 100
  w_array = np.linspace(-100, 500, 50)
  w_array = np.linspace(0, 400, 50)
  cost = np.zeros_like(w_array)

  for i in range(len(w_array)):
      tmp_w = w_array[i]
      cost[i] = f_compute_cost(x_train, y_train, tmp_w, fix_b)
  ax[0].plot(w_array, cost,linewidth=1)
  ax[0].set_title("Cost vs w, with gradient; b set to 100")
  ax[0].set_ylabel('Cost')
  ax[0].set_xlabel('w')

  # plot lines for fixed b=100
  for tmp_w in [100,200,300]:
      fix_b = 100
      dj_dw,dj_db = f_compute_gradient(x_train, y_train, tmp_w, fix_b )
      j = f_compute_cost(x_train, y_train, tmp_w, fix_b)
      add_line(dj_dw, tmp_w, j, 30, ax[0])

  #===============
  # Second Subplot
  #===============

  tmp_b,tmp_w = np.meshgrid(np.linspace(-200, 200, 10), np.linspace(-100, 600, 10))
  U = np.zeros_like(tmp_w)
  V = np.zeros_like(tmp_b)
  for i in range(tmp_w.shape[0]):
      for j in range(tmp_w.shape[1]):
          U[i][j], V[i][j] = f_compute_gradient(x_train, y_train, tmp_w[i][j], tmp_b[i][j] )
  X = tmp_w
  Y = tmp_b
  n=-2
  color_array = np.sqrt(((V-n)/2)**2 + ((U-n)/2)**2)

  ax[1].set_title('Gradient shown in quiver plot')
  Q = ax[1].quiver(X, Y, U, V, color_array, units='width', )
  ax[1].quiverkey(Q, 0.9, 0.9, 2, r'$2 \frac{m}{s}

also you can create your own code to plot the gradient using the python libraries

Cheers,
Abdelrahman, labelpos=‘E’,coordinates=‘figure’)
ax[1].set_xlabel(“w”); ax[1].set_ylabel(“b”)

also you can create your own code to plot the gradient using the python libraries

Cheers,
Abdelrahman

Hi @AbdElRhaman_Fakhry

Thanks a lot.Also, how can I access lab_utilis_uni files

@Ashutosh_Agrawal

you can access these file from the top right there are an button called lab files or go to the file-> open you will get lab file you can install it and open it in the environment support the extension *.py (like spider or visual studio code)

Cheers,
Abdelrahman