import matplotlib.pyplot as plt
import numpy as np
# 神经网络激活函数
# 阶跃函数
def step_function(x):
return np.array(x > 0, dtype=np.int)
# sigmoid函数 --二元分类问题
def sigmoid(x):
return 1 / (1 + np.exp(-x))
# relu函数
def ReLU(x):
return np.maximum(0, x)
# 输出层设计,回归问题用恒等函数,分类问题用softmax函数
# 恒等函数 --原样输出
def equal(x):
return x
# softmax函数 --用于多元分类问题,输出层神经元数量即为类别数量
def softmax(x):
c = np.max(x)
a = np.exp(x-c) # 解决溢出
y = a / np.sum(a)
return y
x = np.arange(-5.0, 5.0, 0.1)
y = step_function(x)
plt.plot(x, y)
y = sigmoid(x)
plt.plot(x, y)
y = ReLU(x)
plt.plot(x, y)
y = equal(x)
plt.plot(x, y)
y = softmax(x)
print(np.sum(y)) # softmax函数特性,所有相加为1 和概率类似
plt.plot(x, y)
plt.ylim(-0.1, 1.1)
plt.show()