Where is 1/m in `compute_gradient` function in "Optional Lab - Gradient Descent"

My question is about the lab in Machine Learning Specialization, and the “Optional Lab - Gradient Descent”

In the lab, the function compute_gradient is supposed to compute the gradient. The formulas are given as

\begin{align} \frac{\partial J(w,b)}{\partial w} &= \frac{1}{m} \sum\limits_{i = 0}^{m-1} (f_{w,b}(x^{(i)}) - y^{(i)})x^{(i)} \tag{4}\\ \frac{\partial J(w,b)}{\partial b} &= \frac{1}{m} \sum\limits_{i = 0}^{m-1} (f_{w,b}(x^{(i)}) - y^{(i)}) \tag{5}\\ \end{align}

The code was defined as follows,

def compute_gradient(x, y, w, b): 
    """
    Computes the gradient for linear regression 
    Args:
      x (ndarray (m,)): Data, m examples 
      y (ndarray (m,)): target values
      w,b (scalar)    : model parameters  
    Returns
      dj_dw (scalar): The gradient of the cost w.r.t. the parameters w
      dj_db (scalar): The gradient of the cost w.r.t. the parameter b     
     """
    
    # Number of training examples
    m = x.shape[0]    
    dj_dw = 0
    dj_db = 0
    
    for i in range(m):  
        f_wb = w * x[i] + b # linear regression
        dj_dw_i = (f_wb - y[i]) * x[i] 
        dj_db_i = f_wb - y[i] 
        dj_db += dj_db_i
        dj_dw += dj_dw_i 
    dj_dw = dj_dw / m # what's happening here?
    dj_db = dj_db / m # what's happening here?
        
    return dj_dw, dj_db

Where is the 1/m from formulas (4) and (5) given? The code implementation looks different than the formulas, I’m confused about this step. Thanks for any help

The source of the 1/m term in the gradients is from the 1/(2m) scaling factor that is part of the squared-error cost function.

Hello @MrBlobby1999,

Here it is, and the code implemented as the formula says. :wink:

Cheers,
Raymond

1 Like

I get it now. Thanks @rmwkwok @TMosh

1 Like