加速 Java 中的数学计算
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2887815/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Speeding up Math calculations in Java
提问by Simon
I have a neural network written in Java which uses a sigmoid transfer function defined as follows:
我有一个用 Java 编写的神经网络,它使用定义如下的 sigmoid 传递函数:
private static double sigmoid(double x)
{
return 1 / (1 + Math.exp(-x));
}
and this is called manytimes during training and computation using the network. Is there any way of speeding this up? It's not that it's slow, it's just that it is used a lot, so a small optimisation here would be a big overall gain.
而这就是所谓的许多培训和使用网络的计算过程中的时间。有没有办法加快这个速度?并不是它慢,而是它使用得很多,所以这里的一个小优化将是一个很大的整体收益。
回答by tangens
For neural networks, you don't need the exact value of the sigmoid function. So you can precalculate 100 values and reuse the value that is closest to your input, or even better (as a comment stated) do an interpolation from the neighbour values.
对于神经网络,您不需要 sigmoid 函数的确切值。因此,您可以预先计算 100 个值并重新使用最接近您输入的值,或者甚至更好(如评论所述)从相邻值进行插值。
How you can do this is described in this article(link stolen from the answer of s-lott).
这篇文章中描述了如何做到这一点(从s-lott的答案中窃取了链接)。
As you can see, only values of -10 < x < 10 are interesting at all. And, as another comment stated, the function is symmetric. You only have to store half of the values at all.
如您所见,只有 -10 < x < 10 的值才有意义。而且,正如另一条评论所述,该函数是对称的。您只需要存储一半的值。
Edit:I'm sorry that I showed the wrong graph here. I've corrected it.
编辑:很抱歉我在这里显示了错误的图表。我已经改正了。
回答by JustJeff
If you have a lot of nodes where the value of x is outside the -10..+10 box, you can just omit to calculate those values at all, e.g., like so ..
如果您有很多节点,其中 x 的值在 -10..+10 框之外,您可以完全省略计算这些值,例如,像这样 ..
if( x < -10 )
y = 0;
else if( x > 10 )
y = 1;
else
y = 1 / (1 + Math.exp(-x));
return y;
Of course, this incurs the overhead of the conditional checks for EVERY calculation, so it's only worthwhile if you have lots of saturated nodes.
当然,这会导致每次计算的条件检查的开销,因此只有当您有很多饱和节点时才值得。
Another thing worth mentioning is, if you are using backpropagation, and you have to deal with the slope of the function, it's better to compute it in pieces rather than 'as written'.
另一件值得一提的事情是,如果您正在使用反向传播,并且您必须处理函数的斜率,最好将其分块计算而不是“按书面形式”计算。
I can't recall the slope at the moment, but here's what I'm talking about using a bipolar sigmoid as an example. Rather than compute this way
我现在不记得斜率,但这里是我正在谈论的以双极 sigmoid 为例的内容。而不是这样计算
y = (1 - exp(-x)) / (1 + exp(-x));
which hits exp() twice, you can cache up the costly calculations in temporary variables, like so
它命中 exp() 两次,你可以在临时变量中缓存代价高昂的计算,就像这样
temp = exp(-x);
y = (1 - temp) / (1 + temp);
There are lots of places to put this sort of thing to use in BP nets.
有很多地方可以在 BP 网络中使用这种东西。
回答by duffymo
It's a pretty smooth function, so a lookup and interpolation scheme is likely to be more than sufficient.
这是一个非常平滑的函数,因此查找和插值方案可能绰绰有余。
When I plot the function over a range of -10 <= x <= 10, I get five place accuracy at the extremes. Is that good enough for your application?
当我在 范围内绘制函数时-10 <= x <= 10,我会在极端情况下获得五位精度。这对您的应用程序来说足够好了吗?
回答by Femaref
From a math point of view, I don't see any possibility to optimize it.
从数学的角度来看,我看不到任何优化它的可能性。


