八度-inf和NaN [英] Octave -inf and NaN

查看:79
本文介绍了八度-inf和NaN的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我搜索了论坛并找到了这个主题,但是它没有涵盖我的问题 围绕-inf的两种方法

I searched the forum and found this thread, but it does not cover my question Two ways around -inf

在第3周的机器学习课程中,使用log(0)时我得到了-inf,后来又变成了NaN. NaN导致总和公式中没有给出答案,因此J(成本函数是矩阵数学的结果)没有标量.

From a Machine Learning class, week 3, I am getting -inf when using log(0), which later turns into an NaN. The NaN results in no answer being given in a sum formula, so no scalar for J (a cost function which is the result of matrix math).

这是对我的功能的测试

>> sigmoid([-100;0;100])
ans =
3.7201e-44
5.0000e-01
1.0000e+00

这是预期的.但该假设要求ans = 1-sigmoid

This is as expected. but the hypothesis requires ans = 1-sigmoid

>> 1-ans
ans =
1.00000
0.50000
0.00000

和Log(0)给出-Inf

>> log(ans)
ans =
0.00000
-0.69315
-Inf

-Inf行未添加到cost函数,但-Inf保留了NaN的内容,但我没有得到结果.我在-Inf上找不到任何材料,但我认为S型函数有问题.

-Inf rows do not add to the cost function, but the -Inf carries through to NaN, and I do not get a result. I cannot find any material on -Inf, but am thinking there is a problem with my sigmoid function.

您能提供任何指导吗?

推荐答案

在这里添加答案,我真的希望您能为您的问题提供更多的背景信息(尤其是

Adding to the answers here, I really do hope you would provide some more context to your question (in particular, what are you actually trying to do.

我会费力地猜测一下上下文,以防万一这很有用.您可能正在进行机器学习,并试图基于模型的负对数似然定义一个成本函数,然后尝试对其进行区分,以找到该成本最小的点.

I will go out on a limb and guess the context, just in case this is useful. You are probably doing machine learning, and trying to define a cost function based on the negative log likelihood of a model, and then trying to differentiate it to find the point where this cost is at its minimum.

通常,对于一个合理的模型,它很有可能遵循克伦威尔法则 ,您不应该遇到这些问题,但是在实际中会发生.大概是在尝试计算零概率的负对数似然的过程中,您得到了inf,并试图计算两点之间的差,从而得出inf/inf = nan.

In general for a reasonable model with a useful likelihood that adheres to Cromwell's rule, you shouldn't have these problems, but, in practice it happens. And presumably in the process of trying to calculate a negative log likelihood of a zero probability you get inf, and trying to calculate a differential between two points produces inf / inf = nan.

在这种情况下,这是一个边缘案例",通常在计算机科学中,边缘案例需要作为特殊情况加以发现并进行适当处理.现实情况是,您可以合理地预期inf不会成为函数的最小值!因此,无论是从计算中删除它,还是将其替换为非常大的数目(无论是任意还是通过机器精度),都没有任何区别.

In this case, this is an 'edge case', and generally in computer science edge cases need to be spotted as exceptional circumstances and dealt with appropriately. The reality is that you can reasonably expect that inf isn't going to be your function's minimum! Therefore, whether you remove it from the calculations, or replace it by a very large number (whether arbitrarily or via machine precision) doesn't really make a difference.

因此,在实践中,您可以执行此处其他人员建议的两项操作中的任何一项,甚至可以检测到此类实例并从计算中跳过它们.实际结果应该是相同的.

So in practice you can do either of the two things suggested by others here, or even just detect such instances and skip them from the calculation. The practical result should be the same.

这篇关于八度-inf和NaN的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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