加速 Java 中的数学计算 [英] Speeding up Math calculations in Java

查看:28
本文介绍了加速 Java 中的数学计算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用 Java 编写的神经网络,它使用定义如下的 sigmoid 传递函数:

private static double sigmoid(double x){返回 1/(1 + Math.exp(-x));}

这在使用网络的训练和计算过程中被多次调用.有没有办法加快这个速度?不是慢,只是用的多,所以这里的小优化会带来很大的整体收益.

解决方案

对于神经网络,您不需要 sigmoid 函数的确切值.因此,您可以预先计算 100 个值并重新使用最接近您输入的值,或者甚至更好(如评论所述)从相邻值进行插值.

如您所见,只有 -10 <×<10个都很有趣.而且,正如另一条评论所述,该函数是对称的.您只需存储一半的值即可.

<小时>

很抱歉我在这里显示了错误的图表.我已经更正了.

I have a neural network written in Java which uses a sigmoid transfer function defined as follows:

private static double sigmoid(double x)
{
    return 1 / (1 + Math.exp(-x));
}

and this is called many times 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.

解决方案

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.

How you can do this is described in this article (link stolen from the answer of s-lott).

This is the sigmoid function:

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.


Edit: I'm sorry that I showed the wrong graph here. I've corrected it.

这篇关于加速 Java 中的数学计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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