goal = 42 # Label
weight = 0.0
input_val = 0.5
iteration = 0
for iteration in range(15):
# The output of the algorithm
output = input_val * weight
# Calculate the error. Negative errors would hinder the learning process, so errors are squared to ensure that the error metric is positiv
error = (output - goal) ** 2
# Calculate the difference between the output and the goal value
delta = output - goal
# Calculate weight delta, the value with wich we alter the weight
weight_delta = input_val * delta
# Adjust the weight
weight = weight - weight_delta
# Update the iteration number
iteration = iteration + 1
# Print out the results of the iteration
print('----- Iteration {} -----'.format(iteration))
print('Error: {}'.format(error), 'Output: {}'.format(output))
----- Iteration 1 -----
Error: 1764.0 Output: 0.0
----- Iteration 2 -----
Error: 992.25 Output: 10.5
----- Iteration 3 -----
Error: 558.140625 Output: 18.375
----- Iteration 4 -----
Error: 313.9541015625 Output: 24.28125
----- Iteration 5 -----
Error: 176.59918212890625 Output: 28.7109375
----- Iteration 6 -----
Error: 99.33703994750977 Output: 32.033203125
----- Iteration 7 -----
Error: 55.87708497047424 Output: 34.52490234375
----- Iteration 8 -----
Error: 31.430860295891762 Output: 36.3936767578125
----- Iteration 9 -----
Error: 17.679858916439116 Output: 37.795257568359375
----- Iteration 10 -----
Error: 9.944920640497003 Output: 38.84644317626953
----- Iteration 11 -----
Error: 5.594017860279564 Output: 39.63483238220215
----- Iteration 12 -----
Error: 3.1466350464072548 Output: 40.22612428665161
----- Iteration 13 -----
Error: 1.7699822136040808 Output: 40.66959321498871
----- Iteration 14 -----
Error: 0.9956149951522955 Output: 41.00219491124153
----- Iteration 15 -----
Error: 0.5600334347731661 Output: 41.25164618343115