Deal with overflow in exp using numpy

前端 未结 6 1914
你的背包
你的背包 2020-11-29 07:24

Using numpy, I have this definition of a function:

def powellBadlyScaled(X):
    f1 = 10**4 * X[0] * X[1] - 1
    f2 = numpy.exp(-numpy.float(X[0])) + numpy         


        
6条回答
  •  被撕碎了的回忆
    2020-11-29 07:59

    Depending on your specific needs, it may be useful to crop the input argument to exp(). If you actually want to get an inf out if it overflows or you want to get absurdly huge numbers, then other answers will be more appropriate.

    def powellBadlyScaled(X):
        f1 = 10**4 * X[0] * X[1] - 1
        f2 = numpy.exp(-numpy.float(X[0])) + numpy.exp(-numpy.float(X[1])) - 1.0001
        return f1 + f2
    
    
    def powellBadlyScaled2(X):
        f1 = 10**4 * X[0] * X[1] - 1
        arg1 = -numpy.float(X[0])
        arg2 = -numpy.float(X[1])
        too_big = log(sys.float_info.max / 1000.0)  # The 1000.0 puts a margin in to avoid overflow later
        too_small = log(sys.float_info.min * 1000.0)
        arg1 = max([min([arg1, too_big]), too_small])
        arg2 = max([min([arg2, too_big]), too_small])
        # print('    too_small = {}, too_big = {}'.format(too_small, too_big))  # Uncomment if you're curious
        f2 = numpy.exp(arg1) + numpy.exp(arg2) - 1.0001
        return f1 + f2
    
    print('\nTest against overflow: ------------')
    x = [-1e5, 0]
    print('powellBadlyScaled({}) = {}'.format(x, powellBadlyScaled(x)))
    print('powellBadlyScaled2({}) = {}'.format(x, powellBadlyScaled2(x)))
    
    print('\nTest against underflow: ------------')
    x = [0, 1e20]
    print('powellBadlyScaled({}) = {}'.format(x, powellBadlyScaled(x)))
    print('powellBadlyScaled2({}) = {}'.format(x, powellBadlyScaled2(x)))
    

    Result:

    Test against overflow: ------------
    *** overflow encountered in exp 
    powellBadlyScaled([-100000.0, 0]) = inf
    powellBadlyScaled2([-100000.0, 0]) = 1.79769313486e+305
    
    Test against underflow: ------------
    *** underflow encountered in exp    
    powellBadlyScaled([0, 1e+20]) = -1.0001
    powellBadlyScaled2([0, 1e+20]) = -1.0001
    

    Notice that powellBadlyScaled2 didn't over/underflow when the original powellBadlyScaled did, but the modified version gives 1.79769313486e+305 instead of inf in one of the tests. I imagine there are plenty of applications where 1.79769313486e+305 is practically inf and this would be fine, or even preferred because 1.79769313486e+305 is a real number and inf is not.

提交回复
热议问题