def gradient_descent(X, y, theta, alpha, iterations, do_plot):
    """
        :param X            : 2D array of our dataset
        :param y            : 1D array of the groundtruth labels of the dataset
        :param theta        : 1D array of the trainable parameters
        :param alpha        : scalar, learning rate
        :param iterations   : scalar, number of gradient descent iterations
        :param do_plot      : boolean, used to plot groundtruth & prediction values during the gradient descent iterations
    """

    # Create just a figure and only one subplot
    fig, ax1 = plt.subplots()
    if do_plot == True:
        plot_hypothesis(X, y, theta, ax1)

    m = X.shape[
        0]  # the number of training samples is the number of rows of array X
    cost_vector = np.array(
        [],
        dtype=np.float32)  # empty array to store the cost for every iteration

    # Gradient Descent
    for it in range(iterations):
        # get temporary variables for theta's parameters
        theta_0 = theta[0]
        theta_1 = theta[1]

        # update temporary variable for theta_0
        sigma = 0.0
        for i in range(m):
            #  hypothesis = X[i, 0] * theta[0] + X[i, 1] * theta[1]
            #########################################
            # Write your code here
            # Replace the above line that calculates the hypothesis, with a call to the "calculate_hypothesis" function
            hypothesis = calculate_hypothesis(X, theta, i)

            ########################################/
            output = y[i]
            sigma = sigma + (hypothesis - output)
        theta_0 = theta_0 - (alpha / m) * sigma

        # update temporary variable for theta_1
        sigma = 0.0
        for i in range(m):

            # hypothesis = X[i,0] * theta[0] + X[i,1] * theta[1]
            #########################################
            # Write your code here
            # Replace the above line that calculates the hypothesis, with a call to the "calculate_hypothesis" function
            hypothesis = calculate_hypothesis(X, theta, i)
            ########################################/
            output = y[i]
            sigma = sigma + (hypothesis - output) * X[i, 1]
        theta_1 = theta_1 - (alpha / m) * sigma

        # update theta, using the temporary variables
        theta = np.array([theta_0, theta_1])

        # append current iteration's cost to cost_vector
        iteration_cost = compute_cost(X, y, theta)
        cost_vector = np.append(cost_vector, iteration_cost)

        # plot predictions for current iteration
        if do_plot == True:
            plot_hypothesis(X, y, theta, ax1)
    ####################################################

    # plot predictions using the final parameters
    plot_hypothesis(X, y, theta, ax1)
    # save the predictions as a figure
    plot_filename = os.path.join(os.getcwd(), 'figures', 'predictions.png')
    plt.savefig(plot_filename)
    print('Gradient descent finished.')

    # Plot the cost for all iterations
    plot_cost(cost_vector)
    min_cost = np.min(cost_vector)
    argmin_cost = np.argmin(cost_vector)
    print('Minimum cost: {:.5f}, on iteration #{}'.format(
        min_cost, argmin_cost + 1))

    return theta
Пример #2
0
def gradient_descent(X, y, theta, alpha, iterations, do_plot):
    """
        :param X            : 2D array of our dataset
        :param y            : 1D array of the groundtruth labels of the dataset
        :param theta        : 1D array of the trainable parameters
        :param alpha        : scalar, learning rate
        :param iterations   : scalar, number of gradient descent iterations
        :param do_plot      : boolean, used to plot groundtruth & prediction values during the gradient descent iterations
    """
    
    # Create just a figure and only one subplot
    fig, ax1 = plt.subplots()
    if do_plot==True:
        plot_hypothesis(X, y, theta, ax1)

    m = X.shape[0] # the number of training samples is the number of rows of array X
    cost_vector = np.array([], dtype=np.float32) # empty array to store the cost for every iteration
    
    # Gradient Descent loop
    for it in range(iterations):
        
        # initialize temporary theta, as a copy of the existing theta array
        theta_temp = theta.copy()
        
        sigma = np.zeros((len(theta)))
        for i in range(m):
            #hypothesis = X[i, 0] * theta[0] + X[i, 1] * theta[1]
            #########################################
            # Write your code here
            # Calculate the hypothesis for the i-th sample of X, with a call to the "calculate_hypothesis" function
            hypothesis = calculate_hypothesis(X, theta, i)
            ########################################/
            output = y[i]
            #########################################
            # Write your code here
            # Adapt the code, to compute the values of sigma for all the elements of theta
            sigma +=  np.dot((hypothesis - output),X[i])
            ########################################/
        
        # update theta_temp
        #########################################
        # Write your code here
        # Update theta_temp, using the values of sigma
        theta_temp -= alpha/m * sigma
        ########################################/
        # copy theta_temp to theta
        theta = theta_temp.copy()
        
        # append current iteration's cost to cost_vector
        iteration_cost = compute_cost(X, y, theta)
        cost_vector = np.append(cost_vector, iteration_cost)
        
        # plot predictions for current iteration
        if do_plot==True:
            plot_hypothesis(X, y, theta, ax1)
    ####################################################
    
    # plot predictions using the final parameters
    plot_hypothesis(X, y, theta, ax1)
    # save the predictions as a figure
    plot_filename = os.path.join(os.getcwd(), 'figures', 'predictions.png')
    plt.savefig(plot_filename)
    print('Gradient descent finished.')
    
    # Plot the cost for all iterations
    plot_cost(cost_vector)
    min_cost = np.min(cost_vector)
    argmin_cost = np.argmin(cost_vector)
    print('Minimum cost: {:.5f}, on iteration #{}'.format(min_cost, argmin_cost+1))
    
    return theta
Пример #3
0
def gradient_descent(X, y, theta, alpha, iterations, do_plot):
    """
        :param X            : 2D array of our dataset
        :param y            : 1D array of the groundtruth labels of the dataset
        :param theta        : 1D array of the trainable parameters
        :param alpha        : scalar, learning rate
        :param iterations   : scalar, number of gradient descent iterations
        :param do_plot      : boolean, used to plot groundtruth & prediction values during the gradient descent iterations
    """

    # Create just a figure and only one subplot
    fig, ax1 = plt.subplots()
    if do_plot == True:
        plot_hypothesis(X, y, theta, ax1)

    m = X.shape[
        0]  # the number of training samples is the number of rows of array X
    cost_vector = np.array(
        [],
        dtype=np.float32)  # empty array to store the cost for every iteration

    # Gradient Descent loop
    for it in range(iterations):

        # initialize temporary theta, as a copy of the existing theta array
        theta_temp = theta.copy()
        # print(len(theta_temp))
        sigma = np.zeros((len(theta)))
        # print(sigma)
        for index in range(len(theta_temp)):
            for i in range(m):
                hypothesis = calculate_hypothesis(X, theta, i)
                output = y[i]
                sigma[index] = sigma[index] + (hypothesis - output) * X[i,
                                                                        index]

            theta_temp[index] = theta_temp[index] - (alpha / m) * sigma[index]
        # copy theta_temp to theta
        theta = theta_temp.copy()

        # append current iteration's cost to cost_vector
        iteration_cost = compute_cost(X, y, theta)
        cost_vector = np.append(cost_vector, iteration_cost)

        # plot predictions for current iteration
        if do_plot == True:
            plot_hypothesis(X, y, theta, ax1)
    ####################################################

    # plot predictions using the final parameters
    plot_hypothesis(X, y, theta, ax1)
    # save the predictions as a figure
    plot_filename = os.path.join(os.getcwd(), 'figures', 'predictions.png')
    plt.savefig(plot_filename)
    print('Gradient descent finished.')

    # Plot the cost for all iterations
    plot_cost(cost_vector)
    min_cost = np.min(cost_vector)
    argmin_cost = np.argmin(cost_vector)
    print('Minimum cost: {:.5f}, on iteration #{}'.format(
        min_cost, argmin_cost + 1))

    return theta
Пример #4
0
def gradient_descent(X, y, theta, alpha, iterations, do_plot, l):
    """
        :param X            : 2D array of our dataset
        :param y            : 1D array of the groundtruth labels of the dataset
        :param theta        : 1D array of the trainable parameters
        :param alpha        : scalar, learning rate
        :param iterations   : scalar, number of gradient descent iterations
        :param do_plot      : boolean, used to plot groundtruth & prediction values during the gradient descent iterations
        :param l            : scalar, used to represent lambda, used for regularization
    """

    # Create just a figure and two subplots.
    # The first subplot (ax1) will be used to plot the predictions on the given 7 values of the dataset
    # The second subplot (ax2) will be used to plot the predictions on a densely sampled space, to get a more smooth curve
    fig, (ax1, ax2) = plt.subplots(1, 2)
    if do_plot == True:
        plot_hypothesis(X, y, theta, ax1)

    m = X.shape[
        0]  # the number of training samples is the number of rows of array X
    cost_vector = np.array(
        [],
        dtype=np.float32)  # empty array to store the cost for every iteration

    # Gradient Descent loop
    for it in range(iterations):

        # initialize temporary theta, as a copy of the existing theta array
        theta_temp = theta.copy()

        sigma = np.zeros((len(theta)))
        for i in range(m):
            #########################################
            # Write your code here
            # Calculate the hypothesis for the i-th sample of X, with a call to the "calculate_hypothesis" function
            hypothesis = calculate_hypothesis(X, theta, i)
            ########################################/
            output = y[i]
            #########################################
            # Write your code here
            # Adapt the code, to compute the values of sigma for all the elements of theta
            for j in range(0, len(theta)):
                sigma[j] = sigma[j] + (hypothesis - output) * X[i, j]

                ########################################/

                # update theta_temp
                #########################################
                # Write your code here
                # Update theta_temp, using the values of sigma
                # Make sure to use lambda, if necessary
                if i == 0:
                    theta_temp[0] = theta_temp[0] - ((alpha / m) * sigma[0])
                else:
                    theta_temp[j] = (theta_temp[j] * (1 - (
                        (alpha * l) / m))) - (alpha / m) * sigma[j]

        ########################################/

        # copy theta_temp to theta
        theta = theta_temp.copy()

        # append current iteration's cost to cost_vector
        # iteration_cost = compute_cost(X, y, theta)

        iteration_cost = compute_cost_regularised(X, y, theta, l)
        cost_vector = np.append(cost_vector, iteration_cost)

        # plot predictions for current iteration
        if do_plot == True:
            plot_hypothesis(X, y, theta, ax1)
    ####################################################

    # plot predictions on the dataset's points using the final parameters
    plot_hypothesis(X, y, theta, ax1)
    # sample 1000 points, from -1.0 to +1.0
    X_sampled = np.linspace(-1.0, 1.0, 1000)
    # plot predictions on the sampled points using the final parameters
    plot_sampled_points(X, y, X_sampled, theta, ax2)

    # save the predictions as a figure
    plot_filename = os.path.join(os.getcwd(), 'figures', 'predictions.png')
    plt.savefig(plot_filename)
    print('Gradient descent finished.')

    # Plot the cost for all iterations
    plot_cost(cost_vector)
    min_cost = np.min(cost_vector)
    argmin_cost = np.argmin(cost_vector)
    print('Minimum cost: {:.5f}, on iteration #{}'.format(
        min_cost, argmin_cost + 1))

    return theta
Пример #5
0
def gradient_descent(X, y, theta, alpha, l, iterations, do_plot):
    """
        :param X            : 2D array of our dataset
        :param y            : 1D array of the groundtruth labels of the dataset
        :param theta        : 1D array of the trainable parameters
        :param alpha        : scalar, learning rate
        :param iterations   : scalar, number of gradient descent iterations
        :param do_plot      : boolean, used to plot groundtruth & prediction values during the gradient descent iterations
    """

    # Create just a figure and two subplots.
    # The first subplot (ax1) will be used to plot the predictions on the given 7 values of the dataset
    # The second subplot (ax2) will be used to plot the predictions on a densely sampled space, to get a more smooth curve
    fig, (ax1, ax2) = plt.subplots(1, 2)
    if do_plot == True:
        plot_hypothesis(X, y, theta, ax1)

    m = X.shape[
        0]  # the number of training samples is the number of rows of array X
    cost_vector = np.array(
        [],
        dtype=np.float32)  # empty array to store the cost for every iteration

    # Gradient Descent loop
    for it in range(iterations):

        # initialize temporary theta, as a copy of the existing theta array
        theta_temp = theta.copy()

        sigma = np.zeros((len(theta)))
        for index in range(len(theta_temp)):
            for i in range(m):
                hypothesis = calculate_hypothesis(X, theta, i)
                output = y[i]
                sigma[index] = sigma[index] + (hypothesis - output) * X[i,
                                                                        index]

            theta_temp[index] = theta_temp[index] - (alpha / m) * sigma[index]
        # copy theta_temp to theta
        theta = theta_temp.copy()

        # append current iteration's cost to cost_vector
        # iteration_cost = compute_cost(X, y, theta)
        # iteration_cost = compute_cost_regularised(X, y, theta, l)
        iteration_cost = compute_cost_regularised_new(X, y, theta, alpha, l)
        cost_vector = np.append(cost_vector, iteration_cost)

        # plot predictions for current iteration
        if do_plot == True:
            plot_hypothesis(X, y, theta, ax1)
    ####################################################

    # plot predictions on the dataset's points using the final parameters
    plot_hypothesis(X, y, theta, ax1)
    # sample 1000 points, from -1.0 to +1.0
    X_sampled = np.linspace(-1.0, 1.0, 1000)
    # plot predictions on the sampled points using the final parameters
    plot_sampled_points(X, y, X_sampled, theta, ax2)

    # save the predictions as a figure
    plot_filename = os.path.join(os.getcwd(), 'figures', 'predictions.png')
    plt.savefig(plot_filename)
    print('Gradient descent finished.')

    # Plot the cost for all iterations
    plot_cost(cost_vector)
    min_cost = np.min(cost_vector)
    argmin_cost = np.argmin(cost_vector)
    print('Minimum cost: {:.5f}, on iteration #{}'.format(
        min_cost, argmin_cost + 1))

    return theta