Hexadroid

Sigmoid

Sep 18th, 2020
889
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. #include <stdio.h>
  3. #include <math.h>
  4.  
  5.  
  6.  
  7. double fx(double x) //Sigmoid function
  8. {
  9.     return 1 / (1 + exp(-(x)));
  10. }
  11.  
  12.  
  13. double fd(double x) //Sigmoid derivative
  14. {
  15.    return x * (1 - x);
  16. }
  17.  
  18.  
  19. int _tmain(int argc, _TCHAR* argv[])
  20. {
  21.  
  22. double f_out = 0;  //output
  23. double input=0.50;
  24. double weight=0.10;
  25. double learn=0.1; //learning-rate
  26. double desired_output=0.12;
  27. double requested_data=0.65;
  28. double err;  //error value calculation, the error function 2(out-desired) is the derivative of the cost function C = (out-desired)^2
  29. int i; // iteration
  30.  
  31. for (i = 0; i <= 30000; i++) {
  32.  
  33.                             // 1 single node, sigmoid backprop.
  34.                             f_out = fx(input * weight);
  35.                             err = 2*( f_out - desired_output );
  36.                             weight = weight - learn * err *fd(f_out) * input;     // weight minus learning_rate * error * derivative_output * input
  37.                                                                                   //all numbers must be between -1 and 1.
  38.  
  39.                             printf("\n%d. desired output is %.2lf",i, desired_output);
  40.                             printf("\nweight %.9lf  error %.9lf", weight, err);
  41.                             printf("\nOutput for the current input (0.50) is %.9lf", f_out);
  42.                             printf("\nOutput for value 0.65 as new requested data would be %.9lf\n", fx(requested_data*weight));
  43.  
  44.                           }
  45.  
  46.  
  47.  
  48.     scanf("%lf", &input);
  49.  
  50.     return 0;
  51. }
  52.  
  53.  
RAW Paste Data