<
>

求解逻辑回归&#8212;-梯度下降

2020-06-28 14:38:03 来源:易采站长站 作者:易采站长站整理



X[:5]

array([[  1.        ,  30.28671077,  43.89499752],
[ 1. , 35.84740877, 72.90219803],
[ 1. , 60.18259939, 86.3085521 ],
[ 1. , 79.03273605, 75.34437644],
[ 1. , 45.08327748, 56.31637178]])

y[:5]

array([[ 0.],
[ 0.],
[ 1.],
[ 1.],
[ 0.]])

theta

array([[ 0.],
[ 0.],
[ 0.]])

X.shape,y.shape,theta.shape

((99, 3), (99, 1), (3, 1))

cost : 根据参数计算损失

目的是求平均损失的最小值

def costFunction(X,y,theta):
left = np.multiply(-y,np.log(model(X,theta))) # 同*,元素级乘法
right = np.multiply((1-y),np.log(1-model(X,theta)))
return np.sum(left-right)/(len(X))

costFunction(X,y,theta)

0.69314718055994529

gradient : 计算每个参数的梯度方向
def gradient(X,y,theta):
grad = np.zeros(theta.shape)
error = np.matmul(X.T,(model(X,theta)-y))
grad = error/len(X)
return grad
gradient(X,y,theta)

array([[ -0.10606061],
[-12.30538878],
[-11.77067239]])

descent : 进行参数更新
# 首先设定三种停止策略【迭代次数、损失值差距、梯度】
STOP_ITER = 0
STOP_COST = 1
STOP_GRAD = 2

def stopCriterion(type,value,threshold):
# 设定三种不同的停止策略
if type == STOP_ITER: return value > threshold
elif type == STOP_COST: return abs(value[-1]-value[-2]) < threshold
elif type == STOP_GRAD: return np.linalg.norm(value) < threshold


import time

def descent(data,theta,batchSize, stopType, thresh,alpha):
#梯度下降求解

init_time = time.time()
i = 0 # 迭代次数
k = 0 # batch
X,y = shuffleData(data)
grad = np.zeros(theta.shape) # 计算梯度
costs = [costFunction(X,y,theta)] # 损失值

while True:
grad = gradient(X[k:k+batchSize],y[k:k+batchSize],theta)
k += batchSize # 取batch数量个数据
if k >= n:
k = 0;
X,y = shuffleData(data) # 重新洗牌

暂时禁止评论

微信扫一扫

易采站长站微信账号