14
14
((11 , 12 , 13 ), 41 ),
15
15
)
16
16
test_data = (((515 , 22 , 13 ), 555 ), ((61 , 35 , 49 ), 150 ))
17
- parameter_vector = [2 , 4 , 1 , 5 ]
17
+ parameter_vector = [0.0 , 0.0 , 0.0 , 0.0 ]
18
+ velocity = [0.0 ] * len (parameter_vector )
18
19
m = len (train_data )
19
20
LEARNING_RATE = 0.009
20
21
MOMENTUM = 0.9
21
22
22
- # Initialize velocity (for momentum)
23
- velocity = [0 ] * len (parameter_vector )
24
-
25
23
26
24
def _error (example_no , data_set = "train" ) -> float :
27
25
"""
@@ -45,7 +43,7 @@ def _hypothesis_value(data_input_tuple) -> float:
45
43
Returns:
46
44
float: The hypothesis value for the given input.
47
45
"""
48
- hyp_val = 0
46
+ hyp_val = 0.0
49
47
for i in range (len (parameter_vector ) - 1 ):
50
48
hyp_val += data_input_tuple [i ] * parameter_vector [i + 1 ]
51
49
hyp_val += parameter_vector [0 ]
@@ -66,7 +64,7 @@ def output(example_no, data_set) -> int:
66
64
return train_data [example_no ][1 ]
67
65
elif data_set == "test" :
68
66
return test_data [example_no ][1 ]
69
- return None
67
+ return - 1
70
68
71
69
72
70
def calculate_hypothesis_value (example_no , data_set ) -> float :
@@ -82,7 +80,7 @@ def calculate_hypothesis_value(example_no, data_set) -> float:
82
80
return _hypothesis_value (train_data [example_no ][0 ])
83
81
elif data_set == "test" :
84
82
return _hypothesis_value (test_data [example_no ][0 ])
85
- return None
83
+ return - 1
86
84
87
85
88
86
def summation_of_cost_derivative (index , end = m ) -> float :
@@ -95,7 +93,7 @@ def summation_of_cost_derivative(index, end=m) -> float:
95
93
Returns:
96
94
float: The summation of the cost derivatives for the given parameter.
97
95
"""
98
- summation_value = 0
96
+ summation_value = 0.0
99
97
for i in range (end ):
100
98
if index == - 1 :
101
99
summation_value += _error (i )
@@ -124,9 +122,10 @@ def run_gradient_descent_with_momentum() -> None:
124
122
absolute_error_limit = 0.000002
125
123
relative_error_limit = 0
126
124
iteration = 0
125
+
127
126
while True :
128
127
iteration += 1
129
- temp_parameter_vector = [0 ] * len (parameter_vector )
128
+ temp_parameter_vector = [0.0 ] * len (parameter_vector )
130
129
for i in range (len (parameter_vector )):
131
130
cost_derivative = get_cost_derivative (i - 1 )
132
131
velocity [i ] = MOMENTUM * velocity [i ] + cost_derivative
0 commit comments