RicardasSim icon

Leaky ReLU, Parametric ReLU

RicardasSim | PRO | 03/17/24 09:09:31 AM UTC (Edited) | 0 ⭐ | 13420 👁️ | Never ⏰ | []
C |

3.09 KB

|

None

|

0 👍

/

0 👎

#include <stdio.h>
#include <stdlib.h>
 
#define LEAKY_RELU(x) ((x) > 0 ? (x) : 0.01 * (x))
#define PARAMETRIC_RELU(x, leak_factor) ((x) > 0 ? (x) : (leak_factor) * (x))
 
static float LeakyReLU( float x )
{
    return ( x > 0 ) ? x : 0.01f * x;
}
 
static float ParametricReLU( float x, float leak_factor )
{
    return ( x > 0 ) ? x : leak_factor * x;
}
 
int main()
{
 
    float x, l_f;
 
    // Leaky ReLU with fnc
 
    x = -2.0f;
 
    printf( "x: %f result: %f\n", x, LeakyReLU( x ));
 
    x = -1.0f;
 
    printf( "x: %f result: %f\n", x, LeakyReLU( x ));
 
    x = 0.0f;
 
    printf( "x: %f result: %f\n", x, LeakyReLU( x ));
 
    x = 1.0f;
 
    printf( "x: %f result: %f\n", x, LeakyReLU( x ));
 
    x = 2.0f;
 
    printf( "x: %f result: %f\n", x, LeakyReLU( x ));
 
    printf("\n");
 
    // Leaky ReLU with macro
 
    x = -2.0f;
 
    printf( "x: %f result: %f\n", x, LEAKY_RELU( x ));
 
    x = -1.0f;
 
    printf( "x: %f result: %f\n", x, LEAKY_RELU( x ));
 
    x = 0.0f;
 
    printf( "x: %f result: %f\n", x, LEAKY_RELU( x ));
 
    x = 1.0f;
 
    printf( "x: %f result: %f\n", x, LEAKY_RELU( x ));
 
    x = 2.0f;
 
    printf( "x: %f result: %f\n", x, LEAKY_RELU( x ));
 
    printf("\n");
 
    // Parametric ReLU with fnc
 
    l_f = 0.02f;
 
    x = -2.0f;
 
    printf( "x: %f l_f: %f result: %f\n", x, l_f, ParametricReLU( x, l_f ));
 
    x = -1.0f;
 
    printf( "x: %f l_f: %f result: %f\n", x, l_f, ParametricReLU( x, l_f ));
 
    x = 0.0f;
 
    printf( "x: %f l_f: %f result: %f\n", x, l_f, ParametricReLU( x, l_f ));
 
    x = 1.0f;
 
    printf( "x: %f l_f: %f result: %f\n", x, l_f, ParametricReLU( x, l_f ));
 
    x = 2.0f;
 
    printf( "x: %f l_f: %f result: %f\n", x, l_f, ParametricReLU( x, l_f ));
 
    printf("\n");
 
    // Parametric ReLU with macro
 
    x = -2.0f;
 
    printf( "x: %f l_f: %f result: %f\n", x, l_f, PARAMETRIC_RELU( x, l_f ));
 
    x = -1.0f;
 
    printf( "x: %f l_f: %f result: %f\n", x, l_f, PARAMETRIC_RELU( x, l_f ));
 
    x = 0.0f;
 
    printf( "x: %f l_f: %f result: %f\n", x, l_f, PARAMETRIC_RELU( x, l_f ));
 
    x = 1.0f;
 
    printf( "x: %f l_f: %f result: %f\n", x, l_f, PARAMETRIC_RELU( x, l_f ));
 
    x = 2.0f;
 
    printf( "x: %f l_f: %f result: %f\n", x, l_f, PARAMETRIC_RELU( x, l_f ));
 
    printf("\n");
 
}
 
/*
 
output:
 
x: -2.000000 result: -0.020000
x: -1.000000 result: -0.010000
x: 0.000000 result: 0.000000
x: 1.000000 result: 1.000000
x: 2.000000 result: 2.000000
 
x: -2.000000 result: -0.020000
x: -1.000000 result: -0.010000
x: 0.000000 result: 0.000000
x: 1.000000 result: 1.000000
x: 2.000000 result: 2.000000
 
x: -2.000000 l_f: 0.020000 result: -0.040000
x: -1.000000 l_f: 0.020000 result: -0.020000
x: 0.000000 l_f: 0.020000 result: 0.000000
x: 1.000000 l_f: 0.020000 result: 1.000000
x: 2.000000 l_f: 0.020000 result: 2.000000
 
x: -2.000000 l_f: 0.020000 result: -0.040000
x: -1.000000 l_f: 0.020000 result: -0.020000
x: 0.000000 l_f: 0.020000 result: 0.000000
x: 1.000000 l_f: 0.020000 result: 1.000000
x: 2.000000 l_f: 0.020000 result: 2.000000
 
 
*/
 

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎

    
        
  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎