[ Activation ] improve tanh compuataion
authorjijoong.moon <jijoong.moon@samsung.com>
Mon, 18 Jul 2022 04:43:16 +0000 (13:43 +0900)
committerJijoong Moon <jijoong.moon@samsung.com>
Fri, 5 Aug 2022 08:22:19 +0000 (17:22 +0900)
This patch improves the computation of tanh.
Rather than calling tanh fuction, it is faster when
sigmoid is used.

tanh(x) = 2.0*sigmoid(2.0*x) -1.0;

**Self evaluation:**
1. Build test:  [X]Passed [ ]Failed [ ]Skipped
2. Run test:  [X]Passed [ ]Failed [ ]Skipped

Signed-off-by: jijoong.moon <jijoong.moon@samsung.com>
nntrainer/layers/acti_func.cpp

index af9fca20bdf2204d96b8929e832f4fd114fe24ff..0962f9cc0eed47b6a513cda0f22ed19a272aaa95 100644 (file)
@@ -255,7 +255,10 @@ float ActiFunc::sigmoidPrime(float x) {
   return x * (1.0f - x);
 }
 
-float ActiFunc::tanhFloat(float x) { return (float)tanh(x); }
+float ActiFunc::tanhFloat(float x) {
+  // return (float)tanh(x); Using sigmoid implementaion for latency reason.
+  return 2.0 * sigmoid(2.0 * x) - 1.0;
+}
 
 float ActiFunc::tanhPrime(float x) {
   // float th = (float)tanh(x);