推导LR损失函数(1)
假定:
LR逻辑回归假设样本服从泊松0–1分布,因此p(y|x)表达式:
求最大似然估计:
进而求最大对数似然估计:
损失函数:
损失函数表征预测值与真实值之间的差异程度,如果预测值与真实值越接近则损失函数应该越小。在此损失函数可以取为最大似然估计函数的相反数,其次除以m这一因子并不改变最终求导极值结果,通过除以m可以得到平均损失值,避免样本数量对于损失值的影响。
学习LR梯度下降(2)
这里采用随机梯度下降,损失函数对于\Theta j偏导:
参数\Theta 更新
利用代码描述梯度下降(选做)(3)
# BGD 批梯度下降代码实现
# SGD 随机梯度下降代码实现
import numpy as np
import random
def batchGradientDescent(x, y, theta, alpha, m, maxInteration):
x_train = x.transpose()
for i in range(0, maxInteration):
hypothesis = np.dot(x, theta)
# 损失函数
loss = hypothesis - y
# 下降梯度
gradient = np.dot(x_train, loss) / m
# 求导之后得到theta
theta = theta - alpha * gradient
return theta
def stochasticGradientDescent(x, y, theta, alpha, m, maxInteration):
data = []
for i in range(4):
data.append(i)
x_train = x.transpose()
for i in range(0, maxInteration):
hypothesis = np.dot(x, theta)
# 损失函数
loss = hypothesis - y
# 选取一个随机数
index = random.sample(data, 1)
index1 = index[0]
# 下降梯度
gradient = loss[index1] * x[index1]
# 求导之后得到theta
theta = theta - alpha * gradient
return theta
Softmax原理(4)
softmax用于多分类过程中,它将多个神经元的输出,映射到(0,1)区间内,可以看成概率来理解,从而来进行多分类!
假设我们有一个数组,V,Vi表示V中的第i个元素,那么这个元素的softmax值就是
尤其在多分类的场景中使用广泛。他把一些输入映射为0-1之间的实数,并且归一化保证和为1,因此多分类的概率之和也刚好为1。
更形象的如下图表示:
softmax损失函数(5)
在神经网络反向传播中,要求一个损失函数,这个损失函数其实表示的是真实值与网络的估计值的误差,知道误差了,才能知道怎样去修改网络中的权重。
损失函数可以有很多形式,这里用的是交叉熵函数,主要是由于这个求导结果比较简单,易于计算,并且交叉熵解决某些损失函数学习缓慢的问题。交叉熵的函数是这样的:
softmax梯度下降(6)
参考文献
https://www.cnblogs.com/stAr-1/p/9020537.html
https://blog.csdn.net/u014106644/article/details/83660226
https://www.cnblogs.com/zongfa/p/8971213.html