使用numpy处理exp中的溢出 [英] Deal with overflow in exp using numpy

查看:426
本文介绍了使用numpy处理exp中的溢出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用numpy,我对函数的定义如下:

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.exp(-numpy.float(X[1])) - 1.0001
    return f1 + f2

此函数在优化例程中被评估了很多次.它经常会引发异常:

This function is evaluated a huge number of times on an optimization routine. It often raises exception:

RuntimeWarning: overflow encountered in exp

我知道操作数不能存储在为浮点数分配的空间中.但是我该如何克服这个问题呢?

I understand that operand cannot be stored in allocated space for a float. But how can I overcome the problem?

推荐答案

您可以使用bigfloat软件包.它支持任意精度的浮点运算.

You can use the bigfloat package. It supports arbitrary precision floating point operations.

http://packages.python.org/bigfloat/

import bigfloat
bigfloat.exp(5000,bigfloat.precision(100))
# -> BigFloat.exact('2.9676283840236670689662968052896e+2171', precision=100)

您是否正在使用功能优化框架?它们通常实现价值边界(使用惩罚性条款).试试看相关价值观真的那么极端吗?在优化中,最小化log(f)并不少见. (近似的对数可能性等).您确定要优化该exp值,而不是log(exp(f))== f. ?

Are you using a function optimization framework? They usually implement value boundaries (using penalty terms). Try that. Are the relevant values really that extreme? In optimization it's not uncommon to minimize log(f). (approximate log likelihood etc etc). Are you sure you want to optimize on that exp value and not log(exp(f)) == f. ?

看看我对这个问题的回答: logit和逆logit函数获得极高的值

Have a look at my answer to this question: logit and inverse logit functions for extreme values

顺便说一句,如果您要做的就是最小化powellBadlyScaled(x,y),则最小值为x-> + inf和y-> + inf,因此不需要数字.

Btw, if all you do is minimize powellBadlyScaled(x,y) then the minimum is at x -> + inf and y -> + inf, so no need for numerics.

这篇关于使用numpy处理exp中的溢出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆