激活函数及python实现

系统 2079 0

1 threshold激活函数

f ( x ) = { 1 , x ≥ 0 0 , x < 0 f\left(x\right)=\begin{cases}1,&\text {$x\geq0$}\\0,&\text{x < 0}\end{cases} f ( x ) = { 1 , 0 , x 0 x < 0

            
              
                import
              
               matplotlib
              
                .
              
              pyplot 
              
                as
              
               plt

              
                import
              
               mpl_toolkits
              
                .
              
              axisartist 
              
                as
              
               axisartist

              
                import
              
               numpy 
              
                as
              
               np
fig 
              
                =
              
               plt
              
                .
              
              figure
              
                (
              
              figsize
              
                =
              
              
                (
              
              
                6
              
              
                ,
              
              
                6
              
              
                )
              
              
                )
              
              
ax 
              
                =
              
               axisartist
              
                .
              
              Subplot
              
                (
              
              fig
              
                ,
              
              
                111
              
              
                )
              
              
fig
              
                .
              
              add_axes
              
                (
              
              ax
              
                )
              
              
                # 隐藏坐标轴
              
              
ax
              
                .
              
              axis
              
                [
              
              
                :
              
              
                ]
              
              
                .
              
              set_visible
              
                (
              
              
                False
              
              
                )
              
              
                # 添加坐标轴
              
              
ax
              
                .
              
              axis
              
                [
              
              
                'x'
              
              
                ]
              
              
                =
              
               ax
              
                .
              
              new_floating_axis
              
                (
              
              
                0
              
              
                ,
              
              
                0
              
              
                )
              
              
ax
              
                .
              
              axis
              
                [
              
              
                'y'
              
              
                ]
              
              
                =
              
               ax
              
                .
              
              new_floating_axis
              
                (
              
              
                1
              
              
                ,
              
              
                0
              
              
                )
              
              
                # x轴添加箭头
              
              
ax
              
                .
              
              axis
              
                [
              
              
                'x'
              
              
                ]
              
              
                .
              
              set_axisline_style
              
                (
              
              
                '-|>'
              
              
                ,
              
               size
              
                =
              
              
                1.0
              
              
                )
              
              
ax
              
                .
              
              axis
              
                [
              
              
                'y'
              
              
                ]
              
              
                .
              
              set_axisline_style
              
                (
              
              
                '-|>'
              
              
                ,
              
               size
              
                =
              
              
                1.0
              
              
                )
              
              
                # 设置坐标轴刻度显示方向
              
              
ax
              
                .
              
              axis
              
                [
              
              
                'x'
              
              
                ]
              
              
                .
              
              set_axis_direction
              
                (
              
              
                'top'
              
              
                )
              
              
ax
              
                .
              
              axis
              
                [
              
              
                'y'
              
              
                ]
              
              
                .
              
              set_axis_direction
              
                (
              
              
                'right'
              
              
                )
              
              
plt
              
                .
              
              ylim
              
                (
              
              
                -
              
              
                0.2
              
              
                ,
              
              
                1.25
              
              
                )
              
              
x_1 
              
                =
              
               np
              
                .
              
              arange
              
                (
              
              
                0
              
              
                ,
              
              
                10
              
              
                ,
              
              
                0.1
              
              
                )
              
              
y_1 
              
                =
              
               x_1 
              
                -
              
               x_1 
              
                +
              
              
                1
              
              
x_axis 
              
                =
              
               np
              
                .
              
              arange
              
                (
              
              
                -
              
              
                10
              
              
                ,
              
              
                10
              
              
                ,
              
              
                0.2
              
              
                )
              
              
y_axis 
              
                =
              
               np
              
                .
              
              arange
              
                (
              
              
                -
              
              
                0
              
              
                ,
              
              
                1
              
              
                ,
              
              
                0.2
              
              
                )
              
              
plt
              
                .
              
              plot
              
                (
              
              x_1
              
                ,
              
               y_1
              
                ,
              
              
                'r'
              
              
                ,
              
               label
              
                =
              
              r
              
                'threshold=$\{\stackrel{1, x>=0}{0, x<0}$'
              
              
                )
              
              
plt
              
                .
              
              legend
              
                (
              
              
                )
              
              
x_2 
              
                =
              
               np
              
                .
              
              arange
              
                (
              
              
                -
              
              
                5
              
              
                ,
              
              
                0
              
              
                ,
              
              
                0.1
              
              
                )
              
              
y_2 
              
                =
              
               x_2 
              
                -
              
               x_2
plt
              
                .
              
              plot
              
                (
              
              x_2
              
                ,
              
               y_2
              
                ,
              
              
                'r'
              
              
                ,
              
               label
              
                =
              
              
                'threshold'
              
              
                )
              
              
plt
              
                .
              
              scatter
              
                (
              
              
                0
              
              
                ,
              
              
                1
              
              
                ,
              
               color
              
                =
              
              
                'r'
              
              
                )
              
              
                # 绘制圆圈:color设置为空
              
              
plt
              
                .
              
              scatter
              
                (
              
              
                0
              
              
                ,
              
              
                0
              
              
                ,
              
               marker
              
                =
              
              
                'o'
              
              
                ,
              
               color
              
                =
              
              
                ''
              
              
                ,
              
               edgecolors
              
                =
              
              
                'r'
              
              
                )
              
              
plt
              
                .
              
              savefig
              
                (
              
              
                "./image_test/threshold.png"
              
              
                ,
              
              
                format
              
              
                =
              
              
                "png"
              
              
                )
              
            
          

激活函数及python实现_第1张图片

图1 Threshold激活函数

2 sigmoid激活函数

f ( x ) = 1 1 + e − x f\left(x\right)=\frac{1}{1+e^{-x}} f ( x ) = 1 + e x 1

            
              
                import
              
               matplotlib
              
                .
              
              pyplot 
              
                as
              
               plt

              
                import
              
               mpl_toolkits
              
                .
              
              axisartist 
              
                as
              
               axisartist

              
                import
              
               numpy 
              
                as
              
               np
fig 
              
                =
              
               plt
              
                .
              
              figure
              
                (
              
              figsize
              
                =
              
              
                (
              
              
                6
              
              
                ,
              
              
                6
              
              
                )
              
              
                )
              
              
ax 
              
                =
              
               axisartist
              
                .
              
              Subplot
              
                (
              
              fig
              
                ,
              
              
                111
              
              
                )
              
              
fig
              
                .
              
              add_axes
              
                (
              
              ax
              
                )
              
              
                # 隐藏坐标轴
              
              
ax
              
                .
              
              axis
              
                [
              
              
                :
              
              
                ]
              
              
                .
              
              set_visible
              
                (
              
              
                False
              
              
                )
              
              
                # 添加坐标轴
              
              
ax
              
                .
              
              axis
              
                [
              
              
                'x'
              
              
                ]
              
              
                =
              
               ax
              
                .
              
              new_floating_axis
              
                (
              
              
                0
              
              
                ,
              
              
                0
              
              
                )
              
              
ax
              
                .
              
              axis
              
                [
              
              
                'y'
              
              
                ]
              
              
                =
              
               ax
              
                .
              
              new_floating_axis
              
                (
              
              
                1
              
              
                ,
              
              
                0
              
              
                )
              
              
                # x轴添加箭头
              
              
ax
              
                .
              
              axis
              
                [
              
              
                'x'
              
              
                ]
              
              
                .
              
              set_axisline_style
              
                (
              
              
                '-|>'
              
              
                ,
              
               size
              
                =
              
              
                1.0
              
              
                )
              
              
ax
              
                .
              
              axis
              
                [
              
              
                'y'
              
              
                ]
              
              
                .
              
              set_axisline_style
              
                (
              
              
                '-|>'
              
              
                ,
              
               size
              
                =
              
              
                1.0
              
              
                )
              
              
                # 设置坐标轴刻度显示方向
              
              
ax
              
                .
              
              axis
              
                [
              
              
                'x'
              
              
                ]
              
              
                .
              
              set_axis_direction
              
                (
              
              
                'top'
              
              
                )
              
              
ax
              
                .
              
              axis
              
                [
              
              
                'y'
              
              
                ]
              
              
                .
              
              set_axis_direction
              
                (
              
              
                'right'
              
              
                )
              
              

plt
              
                .
              
              xlim
              
                (
              
              
                -
              
              
                10
              
              
                ,
              
              
                10
              
              
                )
              
              
plt
              
                .
              
              ylim
              
                (
              
              
                -
              
              
                0.1
              
              
                ,
              
              
                1.2
              
              
                )
              
              
                # x轴添加箭头
              
              
x 
              
                =
              
               np
              
                .
              
              arange
              
                (
              
              
                -
              
              
                10
              
              
                ,
              
              
                10
              
              
                ,
              
              
                0.1
              
              
                )
              
              
y 
              
                =
              
              
                1
              
              
                /
              
              
                (
              
              
                1
              
              
                +
              
              np
              
                .
              
              exp
              
                (
              
              
                -
              
              x
              
                )
              
              
                )
              
              
plt
              
                .
              
              plot
              
                (
              
              x
              
                ,
              
               y
              
                ,
              
               label
              
                =
              
              r
              
                "$sigmoid=\frac{1}{1+e^{-x}}$"
              
              
                ,
              
               c
              
                =
              
              
                'r'
              
              
                )
              
              
plt
              
                .
              
              legend
              
                (
              
              
                )
              
              
plt
              
                .
              
              savefig
              
                (
              
              
                "./image_test/sigmoid.png"
              
              
                ,
              
              
                format
              
              
                =
              
              
                "png"
              
              
                )
              
            
          

激活函数及python实现_第2张图片

图2 sigmoid激活函数

3 Relu激活函数

f ( x ) = m a x ( 0 , x ) = { x , x ≥ 0 0 , x < 0 f\left(x\right)=max\left(0,x\right)=\begin{cases}x,&\text{$x\geq0$}\\0,&\text{x < 0} \end{cases} f ( x ) = m a x ( 0 , x ) = { x , 0 , x 0 x < 0

            
              
                import
              
               matplotlib
              
                .
              
              pyplot 
              
                as
              
               plt

              
                import
              
               mpl_toolkits
              
                .
              
              axisartist 
              
                as
              
               axisartist

              
                import
              
               numpy 
              
                as
              
               np
fig 
              
                =
              
               plt
              
                .
              
              figure
              
                (
              
              figsize
              
                =
              
              
                (
              
              
                6
              
              
                ,
              
              
                6
              
              
                )
              
              
                )
              
              
ax 
              
                =
              
               axisartist
              
                .
              
              Subplot
              
                (
              
              fig
              
                ,
              
              
                111
              
              
                )
              
              
fig
              
                .
              
              add_axes
              
                (
              
              ax
              
                )
              
              
                # 隐藏坐标轴
              
              
ax
              
                .
              
              axis
              
                [
              
              
                :
              
              
                ]
              
              
                .
              
              set_visible
              
                (
              
              
                False
              
              
                )
              
              
                # 添加坐标轴
              
              
ax
              
                .
              
              axis
              
                [
              
              
                'x'
              
              
                ]
              
              
                =
              
               ax
              
                .
              
              new_floating_axis
              
                (
              
              
                0
              
              
                ,
              
              
                0
              
              
                )
              
              
ax
              
                .
              
              axis
              
                [
              
              
                'y'
              
              
                ]
              
              
                =
              
               ax
              
                .
              
              new_floating_axis
              
                (
              
              
                1
              
              
                ,
              
              
                0
              
              
                )
              
              
                # x轴添加箭头
              
              
ax
              
                .
              
              axis
              
                [
              
              
                'x'
              
              
                ]
              
              
                .
              
              set_axisline_style
              
                (
              
              
                '-|>'
              
              
                ,
              
               size
              
                =
              
              
                1.0
              
              
                )
              
              
ax
              
                .
              
              axis
              
                [
              
              
                'y'
              
              
                ]
              
              
                .
              
              set_axisline_style
              
                (
              
              
                '-|>'
              
              
                ,
              
               size
              
                =
              
              
                1.0
              
              
                )
              
              
                # 设置坐标轴刻度显示方向
              
              
ax
              
                .
              
              axis
              
                [
              
              
                'x'
              
              
                ]
              
              
                .
              
              set_axis_direction
              
                (
              
              
                'top'
              
              
                )
              
              
ax
              
                .
              
              axis
              
                [
              
              
                'y'
              
              
                ]
              
              
                .
              
              set_axis_direction
              
                (
              
              
                'right'
              
              
                )
              
              
plt
              
                .
              
              xlim
              
                (
              
              
                -
              
              
                10
              
              
                ,
              
              
                10
              
              
                )
              
              
plt
              
                .
              
              ylim
              
                (
              
              
                -
              
              
                0.1
              
              
                ,
              
              
                10
              
              
                )
              
              
x_1 
              
                =
              
               np
              
                .
              
              arange
              
                (
              
              
                0
              
              
                ,
              
              
                10
              
              
                ,
              
              
                0.1
              
              
                )
              
              
y_1 
              
                =
              
               x_1
x_axis 
              
                =
              
               np
              
                .
              
              arange
              
                (
              
              
                -
              
              
                10
              
              
                ,
              
              
                10
              
              
                ,
              
              
                0.2
              
              
                )
              
              
y_axis 
              
                =
              
               np
              
                .
              
              arange
              
                (
              
              
                -
              
              
                0
              
              
                ,
              
              
                1
              
              
                ,
              
              
                0.2
              
              
                )
              
              
plt
              
                .
              
              plot
              
                (
              
              x_1
              
                ,
              
               y_1
              
                ,
              
              
                'r-'
              
              
                ,
              
               label
              
                =
              
              r
              
                'Relu=$\{\stackrel{1, x>=0}{0, x<0}$'
              
              
                )
              
              
x_2 
              
                =
              
               np
              
                .
              
              arange
              
                (
              
              
                -
              
              
                5
              
              
                ,
              
              
                0
              
              
                ,
              
              
                0.1
              
              
                )
              
              
y_2 
              
                =
              
               x_2 
              
                -
              
               x_2
plt
              
                .
              
              plot
              
                (
              
              x_2
              
                ,
              
               y_2
              
                ,
              
              
                'r-'
              
              
                )
              
              
plt
              
                .
              
              legend
              
                (
              
              
                )
              
              
plt
              
                .
              
              savefig
              
                (
              
              
                "./image_test/relu.png"
              
              
                ,
              
              
                format
              
              
                =
              
              
                "png"
              
              
                )
              
            
          

激活函数及python实现_第3张图片

图3 Relu激活函数

4 PRelu激活函数

f ( x ) = m a x ( 0 , x ) = { x , x ≥ 0 α x , x < 0  f\left(x\right)=max\left(0,x\right)=\begin{cases}x,&\text{$x\geq0$}\\\alpha x,&\text{x < 0 }\end{cases} f ( x ) = m a x ( 0 , x ) = { x , α x , x 0 x < 0 

            
              
                import
              
               matplotlib
              
                .
              
              pyplot 
              
                as
              
               plt

              
                import
              
               mpl_toolkits
              
                .
              
              axisartist 
              
                as
              
               axisartist

              
                import
              
               numpy 
              
                as
              
               np
fig 
              
                =
              
               plt
              
                .
              
              figure
              
                (
              
              figsize
              
                =
              
              
                (
              
              
                6
              
              
                ,
              
              
                6
              
              
                )
              
              
                )
              
              
ax 
              
                =
              
               axisartist
              
                .
              
              Subplot
              
                (
              
              fig
              
                ,
              
              
                111
              
              
                )
              
              
fig
              
                .
              
              add_axes
              
                (
              
              ax
              
                )
              
              
                # 隐藏坐标轴
              
              
ax
              
                .
              
              axis
              
                [
              
              
                :
              
              
                ]
              
              
                .
              
              set_visible
              
                (
              
              
                False
              
              
                )
              
              
                # 添加坐标轴
              
              
ax
              
                .
              
              axis
              
                [
              
              
                'x'
              
              
                ]
              
              
                =
              
               ax
              
                .
              
              new_floating_axis
              
                (
              
              
                0
              
              
                ,
              
              
                0
              
              
                )
              
              
ax
              
                .
              
              axis
              
                [
              
              
                'y'
              
              
                ]
              
              
                =
              
               ax
              
                .
              
              new_floating_axis
              
                (
              
              
                1
              
              
                ,
              
              
                0
              
              
                )
              
              
                # x轴添加箭头
              
              
ax
              
                .
              
              axis
              
                [
              
              
                'x'
              
              
                ]
              
              
                .
              
              set_axisline_style
              
                (
              
              
                '-|>'
              
              
                ,
              
               size
              
                =
              
              
                1.0
              
              
                )
              
              
ax
              
                .
              
              axis
              
                [
              
              
                'y'
              
              
                ]
              
              
                .
              
              set_axisline_style
              
                (
              
              
                '-|>'
              
              
                ,
              
               size
              
                =
              
              
                1.0
              
              
                )
              
              
                # 设置坐标轴刻度显示方向
              
              
ax
              
                .
              
              axis
              
                [
              
              
                'x'
              
              
                ]
              
              
                .
              
              set_axis_direction
              
                (
              
              
                'top'
              
              
                )
              
              
ax
              
                .
              
              axis
              
                [
              
              
                'y'
              
              
                ]
              
              
                .
              
              set_axis_direction
              
                (
              
              
                'right'
              
              
                )
              
              
plt
              
                .
              
              xlim
              
                (
              
              
                -
              
              
                10
              
              
                ,
              
              
                10
              
              
                )
              
              
plt
              
                .
              
              ylim
              
                (
              
              
                -
              
              
                10
              
              
                ,
              
              
                10
              
              
                )
              
              
x_1 
              
                =
              
               np
              
                .
              
              arange
              
                (
              
              
                0
              
              
                ,
              
              
                10
              
              
                ,
              
              
                0.1
              
              
                )
              
              
y_1 
              
                =
              
               x_1
x_axis 
              
                =
              
               np
              
                .
              
              arange
              
                (
              
              
                -
              
              
                10
              
              
                ,
              
              
                10
              
              
                ,
              
              
                0.2
              
              
                )
              
              
y_axis 
              
                =
              
               np
              
                .
              
              arange
              
                (
              
              
                -
              
              
                0
              
              
                ,
              
              
                1
              
              
                ,
              
              
                0.2
              
              
                )
              
              
plt
              
                .
              
              plot
              
                (
              
              x_1
              
                ,
              
               y_1
              
                ,
              
              
                'r-'
              
              
                ,
              
               label
              
                =
              
              r
              
                'PRelu=$\{\stackrel{x,x>=0}{\alpha x, x<0}$'
              
              
                )
              
              
x_2 
              
                =
              
               np
              
                .
              
              arange
              
                (
              
              
                -
              
              
                10
              
              
                ,
              
              
                0
              
              
                ,
              
              
                0.1
              
              
                )
              
              
y_2 
              
                =
              
              
                0.3
              
              
                *
              
              x_2
plt
              
                .
              
              plot
              
                (
              
              x_2
              
                ,
              
               y_2
              
                ,
              
              
                'r-'
              
              
                )
              
              
plt
              
                .
              
              legend
              
                (
              
              
                )
              
              
plt
              
                .
              
              savefig
              
                (
              
              
                "./image_test/PRelu.png"
              
              
                ,
              
              
                format
              
              
                =
              
              
                "png"
              
              
                )
              
            
          

激活函数及python实现_第4张图片

图4 PRelu激活函数

5 tanh激活函数

f ( x ) = t a n h ( x ) = e x − e − x e x + e − x f\left(x\right)=tanh\left(x\right)=\frac{e^x-e^{-x}}{e^x+e^{-x}} f ( x ) = t a n h ( x ) = e x + e x e x e x

            
              
                import
              
               matplotlib
              
                .
              
              pyplot 
              
                as
              
               plt

              
                import
              
               mpl_toolkits
              
                .
              
              axisartist 
              
                as
              
               axisartist

              
                import
              
               numpy 
              
                as
              
               np
fig 
              
                =
              
               plt
              
                .
              
              figure
              
                (
              
              figsize
              
                =
              
              
                (
              
              
                6
              
              
                ,
              
              
                6
              
              
                )
              
              
                )
              
              
ax 
              
                =
              
               axisartist
              
                .
              
              Subplot
              
                (
              
              fig
              
                ,
              
              
                111
              
              
                )
              
              
fig
              
                .
              
              add_axes
              
                (
              
              ax
              
                )
              
              
                # 隐藏坐标轴
              
              
ax
              
                .
              
              axis
              
                [
              
              
                :
              
              
                ]
              
              
                .
              
              set_visible
              
                (
              
              
                False
              
              
                )
              
              
                # 添加坐标轴
              
              
ax
              
                .
              
              axis
              
                [
              
              
                'x'
              
              
                ]
              
              
                =
              
               ax
              
                .
              
              new_floating_axis
              
                (
              
              
                0
              
              
                ,
              
              
                0
              
              
                )
              
              
ax
              
                .
              
              axis
              
                [
              
              
                'y'
              
              
                ]
              
              
                =
              
               ax
              
                .
              
              new_floating_axis
              
                (
              
              
                1
              
              
                ,
              
              
                0
              
              
                )
              
              
                # x轴添加箭头
              
              
ax
              
                .
              
              axis
              
                [
              
              
                'x'
              
              
                ]
              
              
                .
              
              set_axisline_style
              
                (
              
              
                '-|>'
              
              
                ,
              
               size
              
                =
              
              
                1.0
              
              
                )
              
              
ax
              
                .
              
              axis
              
                [
              
              
                'y'
              
              
                ]
              
              
                .
              
              set_axisline_style
              
                (
              
              
                '-|>'
              
              
                ,
              
               size
              
                =
              
              
                1.0
              
              
                )
              
              
                # 设置坐标轴刻度显示方向
              
              
ax
              
                .
              
              axis
              
                [
              
              
                'x'
              
              
                ]
              
              
                .
              
              set_axis_direction
              
                (
              
              
                'top'
              
              
                )
              
              
ax
              
                .
              
              axis
              
                [
              
              
                'y'
              
              
                ]
              
              
                .
              
              set_axis_direction
              
                (
              
              
                'right'
              
              
                )
              
              
plt
              
                .
              
              xlim
              
                (
              
              
                -
              
              
                10
              
              
                ,
              
              
                10
              
              
                )
              
              
plt
              
                .
              
              ylim
              
                (
              
              
                -
              
              
                1
              
              
                ,
              
              
                1
              
              
                )
              
              
x 
              
                =
              
               np
              
                .
              
              arange
              
                (
              
              
                -
              
              
                10
              
              
                ,
              
              
                10
              
              
                ,
              
              
                0.1
              
              
                )
              
              
y 
              
                =
              
              
                (
              
              np
              
                .
              
              exp
              
                (
              
              x
              
                )
              
              
                -
              
              np
              
                .
              
              exp
              
                (
              
              
                -
              
              x
              
                )
              
              
                )
              
              
                /
              
              
                (
              
              np
              
                .
              
              exp
              
                (
              
              x
              
                )
              
              
                +
              
              np
              
                .
              
              exp
              
                (
              
              
                -
              
              x
              
                )
              
              
                )
              
              
plt
              
                .
              
              plot
              
                (
              
              x
              
                ,
              
               y
              
                ,
              
              
                'r'
              
              
                ,
              
               label
              
                =
              
              r
              
                "tanh=$\frac{e^{x}-e^{-x}}{e^{x}+e^{-x}}$"
              
              
                )
              
              
plt
              
                .
              
              legend
              
                (
              
              
                )
              
              
plt
              
                .
              
              savefig
              
                (
              
              
                "./image_test/tanh.png"
              
              
                ,
              
              
                format
              
              
                =
              
              
                "png"
              
              
                )
              
            
          

激活函数及python实现_第5张图片

图5 双正切激活函数

【参考文献】
[1]https://blog.csdn.net/Xin_101/article/details/81844355
[2]https://matplotlib.org/users/mathtext.html


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论