python绘制激活函数图像

1,Relu

函数表达式:f(x)=max⁡{0,x}, x∈(-∞,+∞)

绘图程序

#  __author__ = 'czx'
# coding=utf-8
import numpy as np
from numpy import *
import matplotlib.pyplot as plt

def relu(x):
    y = x.copy()
    y[y < 0] = 0
    return y

x = np.arange(-10, 10, 0.01)
plt.tick_params(labelsize=14)  # 刻度字体大小14
y_relu = relu(x)

plt.plot(x, y_relu, 'b', linewidth=2.5, label=u'relu')
plt.legend(loc='upper left',fontsize=16, frameon=False)  # 图例字体大小16
plt.tight_layout()  # 去除边缘空白
plt.savefig("F:/激活函数/relu.jpeg", dpi=600, format="jpeg")
#savefig要写在show前面,不然保存的就是空白图片
plt.show()

2,Sigmoid

函数表达式:f(x)=1/(1+e^(-x) ) , x∈(-∞,+∞)

绘图程序

#  __author__ = 'czx'
# coding=utf-8
import numpy as np
from numpy import *
import matplotlib.pyplot as plt


def sigmoid(x):
    return 1.0 / (1.0 + exp(-x))

x = np.arange(-10, 10, 0.01)
plt.tick_params(labelsize=14)  # 刻度字体大小14
y_sigmoid = sigmoid(x)

plt.plot(x, y_sigmoid, 'b', linewidth=2.5, label=u'sigmoid')
plt.legend(loc='upper left', fontsize=16, frameon=False)  # 图例字体大小16
plt.tight_layout()  # 去除边缘空白
plt.savefig("F:/激活函数/sigmoid.jpeg", dpi=600, format="jpeg")
# savefig要写在show前面,不然保存的就是空白图片
plt.show()

3,Tanh

tanh

绘图程序

#  __author__ = 'czx'
# coding=utf-8
import numpy as np
from numpy import *
import matplotlib.pyplot as plt

def tanh(x):
    return 2.0 / (1.0 + exp(-2 * x)) - 1

x = np.arange(-10, 10,0.01)
plt.tick_params(labelsize=14)  # 刻度字体大小14
y_tanh = tanh(x)

plt.plot(x, y_tanh, 'b', linewidth=2.5, label=u'tanh')
plt.legend(loc='upper left', fontsize=16, frameon=False)  # 图例字体大小16
plt.tight_layout()  # 去除边缘空白
plt.savefig("F:/激活函数/tanh.jpeg", dpi=600, format="jpeg")
# savefig要写在show前面,不然保存的就是空白图片
plt.show()

4,leakyrelu

python绘制激活函数图像_第1张图片

绘图程序

#  __author__ = 'czx'
# coding=utf-8
import numpy as np
from numpy import *
import matplotlib.pyplot as plt
import mpl_toolkits.axisartist as axisartist


def leakyrelu(x):
    y = x.copy()
    for i in range(y.shape[0]):
        if y[i] < 0:
            y[i] = 0.2 * y[i]
    return y

x = np.arange(-10, 10, 0.01)
plt.tick_params(labelsize=14)  # 刻度字体大小14
y_relu = leakyrelu(x)

plt.plot(x, y_relu, 'b', linewidth=2.5, label=u'leakyrelu')
plt.legend(loc='upper left', fontsize=16, frameon=False)  # 图例字体大小16
plt.tight_layout()  # 去除边缘空白
plt.savefig("F:/激活函数/leakyrelu.jpeg", dpi=600, format="jpeg")
# savefig要写在show前面,不然保存的就是空白图片
plt.show()

5,Elu

elu

绘图程序

在这里插入#  __author__ = 'czx'
# coding=utf-8
import numpy as np
from numpy import *
import matplotlib.pyplot as plt

def elu(x, a):
    y = x.copy()
    for i in range(y.shape[0]):
        if y[i] < 0:
            y[i] = a * (exp(y[i]) - 1)
    return y

x = np.arange(-10, 10, 0.01)
plt.tick_params(labelsize=14)  # 刻度字体大小14
y_relu = elu(x, 0.3)

plt.plot(x, y_relu, 'b', linewidth=2.5, label=u'elu')
plt.legend(loc='upper left', fontsize=16, frameon=False)  # 图例字体大小16
plt.tight_layout()  # 去除边缘空白
plt.savefig("F:/激活函数/elu.jpeg", dpi=600, format="jpeg")
# savefig要写在show前面,不然保存的就是空白图片
plt.show()
代码片

6,Gaussian

gaussian

绘图程序

#  __author__ = 'czx'
# coding=utf-8
import numpy as np
from numpy import *
import matplotlib.pyplot as plt
import mpl_toolkits.axisartist as axisartist

def gaussian(x):
    return exp(-x**2)

x = np.arange(-10, 10, 0.01)
plt.tick_params(labelsize=14)  # 刻度字体大小14
y_gaussian = gaussian(x)

plt.plot(x, y_gaussian, 'b', linewidth=2.5, label=u'gaussian')
plt.legend(loc='upper left', fontsize=16, frameon=False)  # 图例字体大小16
plt.tight_layout()  # 去除边缘空白
plt.savefig("F:/激活函数/gaussian.jpeg", dpi=600, format="jpeg")
# savefig要写在show前面,不然保存的就是空白图片
plt.show()

7,Binary

binary

绘图程序

#  __author__ = 'czx'
# coding=utf-8
import numpy as np
from numpy import *
import matplotlib.pyplot as plt
import mpl_toolkits.axisartist as axisartist

def binary(x):
    y = x.copy()
    y[y < 0] = 0
    y[y > 0] = 1
    return y

x = np.arange(-10, 10, 0.01)
plt.tick_params(labelsize=14)  # 刻度字体大小14
y_binary = binary(x)

plt.plot(x, y_binary, 'b', linewidth=2.5, label=u'binary')
plt.legend(loc='upper left',fontsize=16, frameon=False)  # 图例字体大小16
plt.tight_layout()  # 去除边缘空白
plt.savefig("F:/激活函数/binary.jpeg", dpi=600, format="jpeg")
#savefig要写在show前面,不然保存的就是空白图片
plt.show()

8,sinx

sinx

绘图程序

#  __author__ = 'czx'
# coding=utf-8
import numpy as np
from numpy import *
import matplotlib.pyplot as plt
import mpl_toolkits.axisartist as axisartist

def sinx(x):
    return sin(x)/x

x = np.arange(-10, 10, 0.01)
plt.tick_params(labelsize=14)  # 刻度字体大小14
y_sinx = sinx(x)

plt.plot(x, y_sinx, 'b', linewidth=2.5, label=u'sinx')
plt.legend(loc='upper left', fontsize=16, frameon=False)  # 图例字体大小16
plt.tight_layout()  # 去除边缘空白
plt.savefig("F:/激活函数/sinx.jpeg", dpi=600, format="jpeg")
# savefig要写在show前面,不然保存的就是空白图片
plt.show()

9,softpulx

softplux

绘图程序

#  __author__ = 'czx'
# coding=utf-8
import numpy as np
from math import log10
from numpy import *
import matplotlib.pyplot as plt
import mpl_toolkits.axisartist as axisartist

def Softplus(x):
    return log10(exp(x)+1)

x = np.arange(-10, 10, 0.01)
plt.tick_params(labelsize=14)  # 刻度字体大小14
y_Softplus = Softplus(x)

plt.plot(x, y_Softplus, 'b', linewidth=2.5, label=u'Softplus')
plt.legend(loc='upper left', fontsize=16, frameon=False)  # 图例字体大小16
plt.tight_layout()  # 去除边缘空白
plt.savefig("F:/激活函数/Softplus.jpeg", dpi=600, format="jpeg")
# savefig要写在show前面,不然保存的就是空白图片
plt.show()

合在一起的激活函数图

#  __author__ = 'czx'
# coding=utf-8
import numpy as np
from numpy import *
import matplotlib
import matplotlib.pyplot as plt


def sigmoid(x):
    return 1.0 / (1.0 + exp(-x))


def tanh(x):
    return 2.0 / (1.0 + exp(-2 * x)) - 1


def relu(x):
    y = x.copy()
    y[y < 0] = 0
    return y


def elu(x, a):
    y = x.copy()
    for i in range(y.shape[0]):
        if y[i] < 0:
            y[i] = a * (exp(y[i]) - 1)
    return y


if __name__ == '__main__':
    x = arange(-3.0, 3.0, 0.01)
    y_sigmoid = sigmoid(x)
    y_tanh = tanh(x)
    y_relu = relu(x)
    y_elu = elu(x, 0.25)

    plt.plot(x, y_sigmoid, 'r', linewidth=2.5, label=u'sigmoid')
    plt.plot(x, y_tanh, 'g', linewidth=2.5, label=u'tanh')
    plt.plot(x, y_relu, 'b', linewidth=2.5, label=u'relu')
    plt.plot(x, y_elu, 'k', linewidth=2.5, label=u'elu')
    plt.ylim([-1, 1])
    plt.xlim([-1, 1])
    plt.legend()
    plt.grid(color='b', linewidth='0.3', linestyle='--')
    plt.show()

你可能感兴趣的:(深度学习,python,深度学习,人工智能,神经网络)