|
| 1 | +""" |
| 2 | +Implementation of gradient descent algorithm for minimizing cost of a linear hypothesis function. |
| 3 | +""" |
| 4 | +importnumpy |
| 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 | +returncalculate_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 | +foriinrange(len(parameter_vector) -1): |
| 35 | +hyp_val+=data_input_tuple[i]*parameter_vector[i+1] |
| 36 | +hyp_val+=parameter_vector[0] |
| 37 | +returnhyp_val |
| 38 | + |
| 39 | + |
| 40 | +defoutput(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 | +ifdata_set=='train': |
| 47 | +returntrain_data[example_no][1] |
| 48 | +elifdata_set=='test': |
| 49 | +returntest_data[example_no][1] |
| 50 | + |
| 51 | + |
| 52 | +defcalculate_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 | +ifdata_set=="train": |
| 60 | +return_hypothesis_value(train_data[example_no][0]) |
| 61 | +elifdata_set=="test": |
| 62 | +return_hypothesis_value(test_data[example_no][0]) |
| 63 | + |
| 64 | + |
| 65 | +defsummation_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 | +foriinrange(end): |
| 75 | +ifindex==-1: |
| 76 | +summation_value+=_error(i) |
| 77 | +else: |
| 78 | +summation_value+=_error(i)*train_data[i][0][index] |
| 79 | +returnsummation_value |
| 80 | + |
| 81 | + |
| 82 | +defget_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 | +returncost_derivative_value |
| 90 | + |
| 91 | + |
| 92 | +defrun_gradient_descent(): |
| 93 | +globalparameter_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 | +whileTrue: |
| 99 | +j+=1 |
| 100 | +temp_parameter_vector= [0, 0, 0, 0] |
| 101 | +foriinrange(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 | +ifnumpy.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 | +deftest_gradient_descent(): |
| 113 | +foriinrange(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