1、阶跃函数
import numpy as np
import matplotlib.pylab as plt
def step_function(x):
#数组中每个数先做布尔运算看是否>0,结果是一个布尔数组,再讲此布尔数组转为int类型(只有0和1)
return np.array(x > 0, dtype = np.int)
x = np.arange(-5.0, 5.0, 0.1)#在−5.0到5.0的范围内,以0.1为单位,生成NumPy数组([-5.0, -4.9, ..., 4.9])
y = step_function(x)
plt.plot(x, y)#设置图像的x轴和y轴
plt.ylim(-0.1, 1.1) # 指定y轴的范围
plt.show()
2、sigmoid函数
import numpy as np
import matplotlib.pylab as plt
def sigmoid(x):
return 1 / (1 + np.exp(-x))
x = np.arange(-5.0, 5.0, 0.1)
y = sigmoid(x)
plt.plot(x, y)
plt.ylim(-0.1, 1.1) # 指定y轴的范围
plt.show()
3、ReLU函数
import numpy as np
import matplotlib.pylab as plt
def relu(x):
return np.maximum(0, x)
x = np.arange(-5.0, 5.0, 0.1)
y = relu(x)
plt.plot(x, y)
plt.ylim(-0.1, 5.0) # 指定y轴的范围
plt.show()
转载:https://blog.csdn.net/kenfan1647/article/details/101924876
查看评论