Math optimization in C#

后端 未结 25 2326
悲&欢浪女
悲&欢浪女 2020-12-07 10:25

I\'ve been profiling an application all day long and, having optimized a couple bits of code, I\'m left with this on my todo list. It\'s the activation function for a neural

25条回答
  •  庸人自扰
    2020-12-07 10:53

    If it's for an activation function, does it matter terribly much if the calculation of e^x is completely accurate?

    For example, if you use the approximation (1+x/256)^256, on my Pentium testing in Java (I'm assuming C# essentially compiles to the same processor instructions) this is about 7-8 times faster than e^x (Math.exp()), and is accurate to 2 decimal places up to about x of +/-1.5, and within the correct order of magnitude across the range you stated. (Obviously, to raise to the 256, you actually square the number 8 times -- don't use Math.Pow for this!) In Java:

    double eapprox = (1d + x / 256d);
    eapprox *= eapprox;
    eapprox *= eapprox;
    eapprox *= eapprox;
    eapprox *= eapprox;
    eapprox *= eapprox;
    eapprox *= eapprox;
    eapprox *= eapprox;
    eapprox *= eapprox;
    

    Keep doubling or halving 256 (and adding/removing a multiplication) depending on how accurate you want the approximation to be. Even with n=4, it still gives about 1.5 decimal places of accuracy for values of x beween -0.5 and 0.5 (and appears a good 15 times faster than Math.exp()).

    P.S. I forgot to mention -- you should obviously not really divide by 256: multiply by a constant 1/256. Java's JIT compiler makes this optimisation automatically (at least, Hotspot does), and I was assuming that C# must do too.

提交回复
热议问题