I have the code below where in the function neural_net_trainer(p, a), the while-loop is meant to end/exit when the value of p is equal to the value of a. However, the loop keeps running infinitely.
Instead of while-loop I tried for-loop, to loop it 30 times (running the 'update' function each time), and 'p' eventually becomes equal to 'a'. But I need to use while-loop instead because I don't know how many times the loop will run. It's whenever 'p' will become equal to 'a'.
# 'p' represent prediction. 'a' represent actual output of Neural network.
#-- improving Neural Network prediction (using derivative of cost function) --
def slope(p, a):
return 2 * (p - a)
# --- Create the Update rule function (to correct prediction) ---
def update(p, a):
p = p - 0.1 * slope(p, a)
return p
# -- Train network - minimising the cost function until prediction is equal to actual output
# - In while loop, if 'p' is not equal to 'a', then 'update' function will increment/decrement 'p' accordingly, until 'p' == 'a'. While-loop should then exit.
def neural_net_trainer(p, a):
print('prediction = ' + str('{:.2f}'.format(p)) + '. Actual Output = ' + str('{:.2f}'.format(a)))
while p != a:
p = update(p, a)
print('prediction = ' + str('{:.2f}'.format(p)))
else:
print('Prediction = ' + str('{:.2f}'.format(p)) + ', and actual output = ' + str('{:.2f}'.format(a)) + '. Our Neural Network is trained on this dataset.')
# - Testing 'neural_net_trainer' function
a = 4
p = 3
neural_net_trainer(p, a)
Basically the code simulates functions of a very simple neural network, where the functions receive two values - p (the predicted output) and a (the actual output). In the code - the slope function is a formula needed to correct the prediction (to become equal to the actual output). update function does the updating/correction. And neural_net_trainer function uses a while-loop to run update function enough times, until p (prediction) is equal to a (actual output). At which point the while-loop should exit.
Any help would be really appreciated.