|
| 1 | +""" |
| 2 | +Implementation of gradient descent algorithm for minimizing cost of a linear hypothesis function. |
| 3 | +""" |
| 4 | +import numpy |
| 5 | + |
| 6 | +# List of input, output pairs |
| 7 | +train_data = (((5, 2, 3), 15), ((6, 5, 9), 25), |
| 8 | + ((11, 12, 13), 41), ((1, 1, 1), 8), ((11, 12, 13), 41)) |
| 9 | +test_data = (((515, 22, 13), 555), ((61, 35, 49), 150)) |
| 10 | +parameter_vector = [2, 4, 1, 5] |
| 11 | +m = len(train_data) |
| 12 | +LEARNING_RATE = 0.009 |
| 13 | + |
| 14 | + |
| 15 | +def _error(example_no, data_set='train'): |
| 16 | + """ |
| 17 | + :param data_set: train data or test data |
| 18 | + :param example_no: example number whose error has to be checked |
| 19 | + :return: error in example pointed by example number. |
| 20 | + """ |
| 21 | + return calculate_hypothesis_value(example_no, data_set) - output(example_no, data_set) |
| 22 | + |
| 23 | + |
| 24 | +def _hypothesis_value(data_input_tuple): |
| 25 | + """ |
| 26 | + Calculates hypothesis function value for a given input |
| 27 | + :param data_input_tuple: Input tuple of a particular example |
| 28 | + :return: Value of hypothesis function at that point. |
| 29 | + Note that there is an 'biased input' whose value is fixed as 1. |
| 30 | + It is not explicitly mentioned in input data.. But, ML hypothesis functions use it. |
| 31 | + So, we have to take care of it separately. Line 36 takes care of it. |
| 32 | + """ |
| 33 | + hyp_val = 0 |
| 34 | + for i in range(len(parameter_vector) - 1): |
| 35 | + hyp_val += data_input_tuple[i]*parameter_vector[i+1] |
| 36 | + hyp_val += parameter_vector[0] |
| 37 | + return hyp_val |
| 38 | + |
| 39 | + |
| 40 | +def output(example_no, data_set): |
| 41 | + """ |
| 42 | + :param data_set: test data or train data |
| 43 | + :param example_no: example whose output is to be fetched |
| 44 | + :return: output for that example |
| 45 | + """ |
| 46 | + if data_set == 'train': |
| 47 | + return train_data[example_no][1] |
| 48 | + elif data_set == 'test': |
| 49 | + return test_data[example_no][1] |
| 50 | + |
| 51 | + |
| 52 | +def calculate_hypothesis_value(example_no, data_set): |
| 53 | + """ |
| 54 | + Calculates hypothesis value for a given example |
| 55 | + :param data_set: test data or train_data |
| 56 | + :param example_no: example whose hypothesis value is to be calculated |
| 57 | + :return: hypothesis value for that example |
| 58 | + """ |
| 59 | + if data_set == "train": |
| 60 | + return _hypothesis_value(train_data[example_no][0]) |
| 61 | + elif data_set == "test": |
| 62 | + return _hypothesis_value(test_data[example_no][0]) |
| 63 | + |
| 64 | + |
| 65 | +def summation_of_cost_derivative(index, end=m): |
| 66 | + """ |
| 67 | + Calculates the sum of cost function derivative |
| 68 | + :param index: index wrt derivative is being calculated |
| 69 | + :param end: value where summation ends, default is m, number of examples |
| 70 | + :return: Returns the summation of cost derivative |
| 71 | + Note: If index is -1, this means we are calculating summation wrt to biased parameter. |
| 72 | + """ |
| 73 | + summation_value = 0 |
| 74 | + for i in range(end): |
| 75 | + if index == -1: |
| 76 | + summation_value += _error(i) |
| 77 | + else: |
| 78 | + summation_value += _error(i)*train_data[i][0][index] |
| 79 | + return summation_value |
| 80 | + |
| 81 | + |
| 82 | +def get_cost_derivative(index): |
| 83 | + """ |
| 84 | + :param index: index of the parameter vector wrt to derivative is to be calculated |
| 85 | + :return: derivative wrt to that index |
| 86 | + Note: If index is -1, this means we are calculating summation wrt to biased parameter. |
| 87 | + """ |
| 88 | + cost_derivative_value = summation_of_cost_derivative(index, m)/m |
| 89 | + return cost_derivative_value |
| 90 | + |
| 91 | + |
| 92 | +def run_gradient_descent(): |
| 93 | + global parameter_vector |
| 94 | + # Tune these values to set a tolerance value for predicted output |
| 95 | + absolute_error_limit = 0.000002 |
| 96 | + relative_error_limit = 0 |
| 97 | + j = 0 |
| 98 | + while True: |
| 99 | + j += 1 |
| 100 | + temp_parameter_vector = [0, 0, 0, 0] |
| 101 | + for i in range(0, len(parameter_vector)): |
| 102 | + cost_derivative = get_cost_derivative(i-1) |
| 103 | + temp_parameter_vector[i] = parameter_vector[i] - \ |
| 104 | + LEARNING_RATE*cost_derivative |
| 105 | + if numpy.allclose(parameter_vector, temp_parameter_vector, |
| 106 | + atol=absolute_error_limit, rtol=relative_error_limit): |
| 107 | + break |
| 108 | + parameter_vector = temp_parameter_vector |
| 109 | + print("Number of iterations:", j) |
| 110 | + |
| 111 | + |
| 112 | +def test_gradient_descent(): |
| 113 | + for i in range(len(test_data)): |
| 114 | + print("Actual output value:", output(i, 'test')) |
| 115 | + print("Hypothesis output:", calculate_hypothesis_value(i, 'test')) |
| 116 | + |
| 117 | + |
| 118 | +if __name__ == '__main__': |
| 119 | + run_gradient_descent() |
| 120 | + print("\nTesting gradient descent for a linear hypothesis function.\n") |
| 121 | + test_gradient_descent() |
0 commit comments