Alex's Notes

cm3015 Lab03: Gradient Descent


# Task Two
def h_lin( X, theta):
    'X should be a data vector, theta the parameters'
    return np.squeeze(theta[0] + (X * theta[1]))

# Task Three
def loss_L2( h, y ):
    'Calculate the least-squares loss.'
    'h is a vector of predictions by the hypothesis function, y is the target'
    factor = 1 / (2 * len(y))
    losses = np.square((h - y))
    sum_of_losses = np.sum(losses)
    return factor * sum_of_losses

# Task Four
def gradient_batch(theta, X, y):
    'Calculate the gradients of the loss function for the parameters in theta'
    'Given data X and target y'
    m = float(len(y))
    grad = np.array([0.0,0.0])

    h0_loss = h_lin(X, theta) - y
    grad[0] = np.sum(h0_loss) / m

    h1_loss = h0_loss * np.squeeze(X)
    grad[1] = np.sum(h1_loss) / m

    return grad

# Task Five
def batch_gd( X, y, old_theta, alpha, loss_stop_threshold):
    ' Batch gradient descent training for linear function with a single variable '
    theta = old_theta.copy()

    # initial loss value
    loss = loss_L2( h_lin(X,theta), y)

    while loss > loss_stop_threshold:
	grad = gradient_batch(theta, X, y)
	theta = theta - (alpha * grad)
	loss = loss_L2(h_lin(X, theta), y)

    print('GD stopped at loss %s, with coefficients: %s' % (loss,theta))
    return theta

Links to this note