Why is there data type conversion just after value is passed?

  Kiến thức lập trình
import numpy as np
import copy,math


def compute_cost(X, y, w, b):
    """
    compute cost
    Args:
      X (ndarray (m,n)): Data, m examples with n features
      y (ndarray (m,)) : target values
      w (ndarray (n,)) : model parameters
      b (scalar)       : model parameter

    Returns:
      cost (scalar): cost
    """
    m = X.shape[0]
    cost = 0.0
    print(f"b is {type(b)}")
    for i in range(m):
        f_wb_i = np.dot(X[i], w) + b
        cost = cost + (f_wb_i - y[i]) ** 2
    cost = cost / (2 * m)
    print(type(cost))
    print(cost)
    return cost

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     
     """
    m = X.shape[0]
    dj_dw = np.zeros(X.shape[1])
    dj_db = 0.0
    for i in range(m):
        fw_b = np.dot(w,X[i]) + b
        dj_dw += (fw_b - y[i]) * X[i]
        dj_db = dj_db + (fw_b - y[i])
    dj_dw = dj_dw/m
    dj_db = dj_db/m
    print(type(dj_db), type(dj_dw))
    return dj_db, dj_dw

def gradient_descent(
    X, y,alpha, num_iter
):
    """
    Performs batch gradient descent to learn w and b. Updates w and b by taking
    num_iters gradient steps with learning rate alpha

    Args:
      X (ndarray (m,n))   : Data, m examples with n features
      y (ndarray (m,))    : target values
      w_in (ndarray (n,)) : initial model parameters
      b_in (scalar)       : initial model parameter
      cost_function       : function to compute cost
      gradient_function   : function to compute the gradient
      alpha (float)       : Learning rate
      num_iters (int)     : number of iterations to run gradient descent

    Returns:
      w (ndarray (n,)) : Updated values of parameters
      b (scalar)       : Updated value of parameter
    """

    # An array to store cost J and w's at each iteration primarily for graphing later
    J_history = []
    w = np.zeros(X.shape[1])
    b = 0.0
    for i in range(num_iter):
        dj_dw, dj_db = compute_gradient(X, y, w, b)
        w = w - alpha * dj_dw
        b = b - alpha * dj_db
        print(f"Here b is {type(b)}", f"Here dj_db is {type(dj_db)}")

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

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

    return w, b, J_history  # return final w,b and J history for graphing

def zscore_normalize_features(X):
    mu = np.mean(X, axis=0)
    sigma = np.std(X, axis=0)
    X_norm = (X - mu)/sigma
    return X_norm, mu, sigma


x = np.arange(0.0, 20.0, 1.0)
y = 1 + x**2
X = x.reshape(-1, 1)
gradient_descent(X,y,1.0e-2,2)

output:

<class 'numpy.float64'> <class 'numpy.ndarray'>
Here b is <class 'numpy.ndarray'> Here dj_db is <class 'numpy.ndarray'>
b is <class 'numpy.ndarray'>
<class 'numpy.ndarray'>
[10147.48954375]
<class 'numpy.ndarray'> <class 'numpy.ndarray'>
Here b is <class 'numpy.ndarray'> Here dj_db is <class 'numpy.ndarray'>
b is <class 'numpy.ndarray'>
<class 'numpy.ndarray'>
[7633.25585605]

My question is why is dj_db is changing data type from ‘numpy.float64’ to ‘numpy.ndarray’ just after the value is received in gradient_descent function as evident from the first two lines of the output. How can the data type just convert itself? I do not understand. Code was run in Visual Studio code.

LEAVE A COMMENT