1
1
"""
2
- Implementation of gradient descent algorithm using momentum for minimizing cost of a linear hypothesis
3
- function.
2
+ Implementation of gradient descent algorithm using momentum
3
+ for minimizing cost of a linear hypothesis function.
4
4
"""
5
5
6
6
import numpy as np
25
25
26
26
def _error (example_no , data_set = "train" ):
27
27
"""
28
- Calculate the error (difference between predicted and actual output) for a given example.
28
+ Calculate the error for a given example.
29
29
Args:
30
30
example_no (int): Index of the example in the dataset.
31
31
data_set (str): The dataset to use, either "train" or "test".
32
32
Returns:
33
- float: The difference between the predicted output and the actual output.
33
+ float: The difference between predicted output and actual output.
34
34
"""
35
- return calculate_hypothesis_value (example_no , data_set ) - output (
36
- example_no , data_set
37
- )
35
+ hypo_value = calculate_hypothesis_value (example_no , data_set )
36
+ output_value = output ( example_no , data_set )
37
+ return hypo_value - output_value
38
38
39
39
40
40
def _hypothesis_value (data_input_tuple ):
41
41
"""
42
42
Compute the hypothesis value (predicted output) for a given input tuple.
43
43
Args:
44
- data_input_tuple (tuple) : The input tuple (features) for the example.
44
+ data_input_tuple: The input tuple (features) for the example.
45
45
Returns:
46
46
float: The hypothesis value for the given input.
47
47
"""
@@ -54,7 +54,8 @@ def _hypothesis_value(data_input_tuple):
54
54
55
55
def output (example_no , data_set ):
56
56
"""
57
- Retrieve the actual output (label) for a given example from the specified dataset.
57
+ Retrieve the actual output (label) for a given example
58
+ from the specified dataset.
58
59
Args:
59
60
example_no (int): Index of the example in the dataset.
60
61
data_set (str): The dataset to use, either "train" or "test".
@@ -89,7 +90,8 @@ def summation_of_cost_derivative(index, end=m):
89
90
Calculate the summation of the cost derivative for a given index.
90
91
Args:
91
92
index (int): The index of the parameter for which the derivative is calculated.
92
- end (int): The number of examples to consider (defaults to the size of the training set).
93
+ end (int): The number of examples to consider
94
+ (defaults to the size of the training set).
93
95
Returns:
94
96
float: The summation of the cost derivatives for the given parameter.
95
97
"""
@@ -152,7 +154,5 @@ def test_gradient_descent():
152
154
153
155
if __name__ == "__main__" :
154
156
run_gradient_descent_with_momentum ()
155
- print (
156
- "\n Testing gradient descent with momentum for a linear hypothesis function.\n "
157
- )
157
+ print ("\n Testing gradient descent momentum for a linear hypothesis function.\n " )
158
158
test_gradient_descent ()
0 commit comments