NN:神经网络学习,常见激活和损失函数的Python实现
激活函数
1、sigmoid
# sigmoid
# sigmoid输出总是大于零,因此前一层对后一层的神经元输入也总是大于零出现了漂移
def sigmoid(x):
# exp()自然常数e的x次方
y = 1 / (1 + math.exp(-x))
return y
# 生成随机数集
# 生成高斯(正太)分布的随机数,loc:中心点
# scl:高度
# size:生成随机数的形状,可为整数或者整数元组
# x = np.random.normal(0, 5, 1000)
# # 生成函数结果
# y = [sigmoid(s) for s in x]
# Fun_show(x, y, "sigmoid")
2、双曲正切tanh
# tanh双曲正切激活函数,输出均值都为零,所以收敛速度相比sigmoid要快,
# 在归一化之后,输入一般在零附近,此时的梯度相比sigmoid更大,所以收敛更快
# 但是仍然存在软饱和问题,容易出现梯度消失问题
def tanh(x):
y = (1 - math.exp(-2 * x)) / (1 + math.exp(-2 * x))
return y
# 生成随机数集
# x = np.random.normal(0, 5, 1000)
# # 生成函数结果
# y = [tanh(s) for s in x]
# Fun_show(x, y, "tanh")
3、ReLU,P-ReLU, Leaky-ReLU
ReLU
> P-ReLU a作为一个可学习的参数时则为P-ReLU,定值时为Leaky-ReLU
# Leaky-ReLU与P-ReLU
# 为了改进原本ReLU的硬饱和,和神经元坏死出现了Leaky-ReLU与P-ReLU
def Leaky_ReLU(x, a):
if x >= 0:
y = x
if x < 0:
y = a * x
return y
# # 生成随机数集
# x = np.random.normal(0, 5, 1000)
# # P-ReLU认为a可以作为一个可学习的参数,原文献建议初始化a为0.25
# y = [Leaky_ReLU(s, 0.25) for s in x]
# plt.subplot(122)
# Fun_show(x, y, "Leaky_ReLU")
4、ELU
# ELU
# 融合了sigmoid和ReLU,左侧具有软饱和性
def ELU(x, a):
if x >= 0:
y = x
if x < 0:
y = a * (math.exp(x) - 1)
return y
# # 生成随机数集
# x = np.random.normal(0, 5, 1000)
# # 生成函数结果
# y = [ELU(s, 0.25) for s in x]
# Fun_show(x, y, "ELU")
5、softmax
softmax常用来作为神经网络的最后一次进行多分类任务,在进行多分类的同时,将结果以概率的形式输出
softmax可以解释为将原始输出结果作为指数函数的输入得到指数函数的结果实现非负性
将某一类的结果除以所以结果的综合实现概率输出
# 用于多分类任务的softmax
def softmax(x):
sum = 0
for s in x:
sum = sum + math.exp(s)
y = []
for s in x:
y.append(math.exp(s) / sum)
return y
# x = np.random.normal(0, 5, 1000)
# y = softmax(x)
# Fun_show(x, y, title= 'softmax')
完整代码
import matplotlib.pyplot as plt
import numpy as np
import math
# plot可视化
def Fun_show(x, y, title):
plt.scatter(x, y)
plt.title(title)
plt.show()
# sigmoid
# sigmoid输出总是大于零,因此前一层对后一层的神经元输入也总是大于零出现了漂移
def sigmoid(x):
# exp()自然常数e的x次方
y = 1 / (1 + math.exp(-x))
return y
# 生成随机数集
# 生成高斯(正太)分布的随机数,loc:中心点
# scl:高度
# size:生成随机数的形状,可为整数或者整数元组
# x = np.random.normal(0, 5, 1000)
# # 生成函数结果
# y = [sigmoid(s) for s in x]
# Fun_show(x, y, "sigmoid")
# tanh双曲正切激活函数,输出均值都为零,所以收敛速度相比sigmoid要快,
# 在归一化之后,输入一般在零附近,此时的梯度相比sigmoid更大,所以收敛更快
# 但是仍然存在软饱和问题,容易出现梯度消失问题
def tanh(x):
y = (1 - math.exp(-2 * x)) / (1 + math.exp(-2 * x))
return y
# 生成随机数集
# x = np.random.normal(0, 5, 1000)
# # 生成函数结果
# y = [tanh(s) for s in x]
# Fun_show(x, y, "tanh")
#ReLU系列,P-ReLU, Leaky-ReLU
# ReLU
# x > 0不会出现饱和,在x < 0 部分出现硬饱和,更适用于监督学习(即在给定数据集的情况下进行训练拟合)
# 因此ReLU的输入值常为大于0的值
def ReLU(x):
y = max(0, x)
return y
# # 生成随机数集
# x = np.random.normal(0, 5, 1000)
# y = [ReLU(s) for s in x]
# plt.subplot(121)
# plt.scatter(x, y)
# plt.title("ReLU")
# Leaky-ReLU与P-ReLU
# 为了改进原本ReLU的硬饱和,和神经元坏死出现了Leaky-ReLU与P-ReLU
def Leaky_ReLU(x, a):
if x >= 0:
y = x
if x < 0:
y = a * x
return y
# # 生成随机数集
# x = np.random.normal(0, 5, 1000)
# # P-ReLU认为a可以作为一个可学习的参数,原文献建议初始化a为0.25
# y = [Leaky_ReLU(s, 0.25) for s in x]
# plt.subplot(122)
# Fun_show(x, y, "Leaky_ReLU")
# ELU
# 融合了sigmoid和ReLU,左侧具有软饱和性
def ELU(x, a):
if x >= 0:
y = x
if x < 0:
y = a * (math.exp(x) - 1)
return y
# # 生成随机数集
# x = np.random.normal(0, 5, 1000)
# # 生成函数结果
# y = [ELU(s, 0.25) for s in x]
# Fun_show(x, y, "ELU")
# 用于多分类任务的softmax
def softmax(x):
sum = 0
for s in x:
sum = sum + math.exp(s)
y = []
for s in x:
y.append(math.exp(s) / sum)
return y
# x = np.random.normal(0, 5, 1000)
# y = softmax(x)
# Fun_show(x, y, title= 'softmax')
if __name__ == "__main__":
x = np.random.normal(0, 5, 1000)
# 生成函数结果
y = [sigmoid(s) for s in x]
Fun_show(x, y, "sigmoid")
y = [tanh(s) for s in x]
Fun_show(x, y, "tanh")
y = [ReLU(s) for s in x]
plt.subplot(121)
plt.scatter(x, y)
plt.title("ReLU")
y = [Leaky_ReLU(s, 0.25) for s in x]
plt.subplot(122)
Fun_show(x, y, "Leaky_ReLU")
y = [ELU(s, 0.25) for s in x]
Fun_show(x, y, "ELU")
y = softmax(x)
Fun_show(x, y, title='softmax')
损失函数
这里两种,即平方差和交叉熵损失函数
import matplotlib.pyplot as plt
import numpy as np
import math
# 平方差损失函数
# 这个1/2可认为方便求导
# 从平方差损失函数看出,神经元输出值距离期望值越远,损失就越大,梯度也就越大,调整的比重也就越大
# 但是影响梯度的因数不光输出和期望的差值,还包括了w,b的影响,因此因为sigmoid的性质在输入较大的位置时容易出现梯度消失导致参数调整慢的问题
def mean(a, y):
L = (a - y) ** 2 / 2
return L
# 交叉熵
# 在求梯度时可以忽略w,b的影响
def Cross_entropy(a, y):
L = 0
for s in y:
L = L + -1 * y * math.ln(a)
return L