2

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.

2 Answers 2

3

The problem here is that you're checking for exact equality and p - a never reaches the exact 0. Try a weaker condition as a guard of the while loop, for example:

THRESHOLD = 1e-5

while abs(p - a) > THRESHOLD:
Sign up to request clarification or add additional context in comments.

Comments

2

p is not reaching 4.0, it is merely reaching 3.999999999999999..., which gets rounded to 4.00 when you use {:.2f} to format it. p != a remains True because 4.0 != 3.999999999999999.

This is more of an algorithmic problem than a python problem, but perhaps round() will come in handy to solve this based on p approaching a instead of a fixed number of iterations.

2 Comments

Super, thank you very much Daniel, and for the very quick reply. It's great to know the round function exists. I did use the other answer from Alessandro, because I find it easier to control the threshold number for equality. Though I'll experiment with round() as well. I wanted to mark both of your answers as valid, but SO doesn't seem to allow me.
I like his reply better too, his threshold approach is much better than using round()

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.