Greetings. I'm currently implementing small ANN on my microcontroller. I need to calculate sigmoid activation function. The problem is that I cannot use standard c libraries like math.h so I need custom written pow function.
This is how it will be used:pow(e,-(someFloat));
Did anyone have already implemented something similar ?

Edited 4 Years Ago by Dr_Freeman: n/a

You could use Maclaurin series expansion for e^x = Sum(x^n/n!,0..N) were you pick N to give the you precision want. The power here is always an integer so that is just a for loop. And the factorial is just another loop.

See if this helps. I don't think an overflow of the double should be a problem considering after t < -6, P(t) ~ 0 and t > 6, P(t) ~ 1. It is just if the precision is OK.

#include <stdio.h>
// Don't need math.h, I have it here to check my answers
// Comment it out in you app
#include <math.h>
double Pow(double x, double n) {   
    double ret=x;
    int i;
    if ( n )
       for ( i=0; i <n-1; i++)
           ret = x*ret;
    else 
       ret = 1;
    return ret;
}
double fac(int i) {
   if ( !i ) 
     return 1;
   else
     return(i*fac(--i));
}
double eToPow(double toPow){
   double ret=0;
   int i;
   // Change the number of loops to 
   // increase precision.  Large loop could overflow double
   for (i=0; i<50; i++)
      ret += Pow(toPow,i)/fac(i);
   return ret;
}
double sigmoid(double t){
   return (1.0/(1+eToPow(-t)));
}
int main()
{
   double toPow=9.555333;
   double ans=eToPow(toPow);
   // Testing e^x function
   printf("My func: e^%f      = %.15f\n",toPow,ans);
   // Comment out line below, they are just for testing
   printf("Math func: exp(%f) = %.15f\n",toPow,exp(toPow));
   printf("Diff                 %.15f\n",fabs(exp(toPow)-ans));
   // Test sigmoid
   printf("Sigmoid(-2) = %.15f\n",sigmoid(-2));
   printf("Sigmoid(0) = %.15f\n",sigmoid(0));
   printf("Sigmoid(2) = %.15f\n",sigmoid(2));
   return 0;
}

Output:

$ ./a.out
My func: e^9.555333      = 14119.795105499684723
Math func: exp(9.555333) = 14119.795105499677447
Diff                 0.000000000007276
Sigmoid(-2) = 0.119202922022118
Sigmoid(0) = 0.500000000000000
Sigmoid(2) = 0.880797077977882
$
This article has been dead for over six months. Start a new discussion instead.