简体   繁体   中英

While infinite loop python

I understand what's going on here, I just want tips on how to catch my program before he goes into infinite loop. Here's my code :

while abs(error) > 50:
    if Co > To:
        c = c + 0.1
    else:
        c = c - 0.1

    ##Here I recalculate Co, the function is Co = 1/b type curve
    ##Here I recalculate To, the function is To = e^b type curve

    error = Co - To

The problem is that depending on the problem, I sometimes need to be more precise (I would need to change the iteration line to c+0.00001 for example) because the error jumps from a value below 50 to a value over 50 on each loop. How can I catch such an infinite loop.

I guess this is a question of convergence. My maths foo is failing me a little bit these days but if you're worried you could end up in a position where you may not converge, you could just add some counter to your loop.

limit = 1000
while abs(error) > 50:
    limit -= 1
    if limit == 0:
        raise SomeError
    your_calcs

It's a bit brutal, but at least it solves the issue of it hanging. It sounds as though you can't guarantee this loop will ever finish because of the nature of the changing code / data. If that infinite loop situation happened, I'd like to see an error raised so I could look into it. Depends on your problem domain really.

If I understand what you're asking, I think you're looking for some kind of adaptive learning rate like you might see applied to the gradient descent method in an ANN, as well as a way to stop if things are no longer improving.

The basic idea is to slowly decrease the amount you are perturbing your values when you don't see a change in your abs error while maintaining stability in the overall process. If you've reduced your learning rate and you're still not seeing any improvement, you're done (or at least at some kind of local minima). This method can be a little slower, and there are different ways of calculating the variables I put in (eg, sigErrChange), so you'll have to toy around with it a bit. There are some other caveats I can't think of off the top of my head, but hopefully this gets the general idea across.

eg,

lR = 1.0
updatedLR = False # Have we updated the learning rate this iteration?
while abs(error) > 50 and ( sigErrChange or updatedLR ):
    sigErrChange = False # Has there been a significant improvement in the error? (Probably shouldn't just use a single iteration for this...)

    # Are we adding or subtracting 
    if C > T:
        sign = 1.
    else:
        sign = -1.

    # Should we update the learning rate?
    if (~sigErrChange)
        updatedLR = True
        lR = .95 * lR

    # Calculate our values
    c = c + sign*lR*.001

    C = calcC(c, C, T)
    T = calcT(c, C, T)

    error = C - T

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM