欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

线性回归之随机梯度下降(sgd)

程序员文章站 2022-06-27 10:37:19
...

梯度下降的原理:梯度下降
普通梯度下降bgd的方法简单暴力,但是调整速度比较慢。
如果不想等所有数据都计算完了才开始调整w,而是计算完数据的一部分(batch_size)后就立即调整w,说白了就是在训练过程中进行权重的更新。
这样就成了随机梯度下降
主要优点有:
* 收敛速度更快,
* 避免过拟合的问题。
代码更新如下:

'''
随机全梯度下降方法
改进:进行到一部分的时候即更新权重

'''
import numpy as np
import math


print(__doc__)

sample = 10
num_input = 5

#加入训练数据
np.random.seed(0)
normalRand = np.random.normal(0,0.1,sample)      # 10个均值为0方差为0.1 的随机数  (b)
weight = [7,99,-1,-333,0.06]                     # 1 * 5 权重
x_train = np.random.random((sample, num_input))  #x 数据(10 * 5)
y_train = np.zeros((sample,1))                   # y数据(10 * 1)


for i in range (0,len(x_train)):
    total = 0
    for j in range(0,len(x_train[i])):
        total += weight[j]*x_train[i,j]
    y_train[i] = total+ normalRand[i]


# 训练
np.random.seed(0)
weight = np.random.random(num_input+1)
rate = 0.04
batch = 3

def train(x_train,y_train):
    #计算损失
    global weight,rate
    predictY = np.zeros((len(x_train)))
    for i in range(0,len(x_train)):
        predictY[i] = np.dot(x_train[i],weight[0:num_input])+ weight[num_input]
        loss = 0
        for i in range(0,len(x_train)):
            loss += (predictY[i]-y_train[i])**2

    for i in range(0,len(weight)-1):
        grade = 0
        for j in range(0,len(x_train)):
            grade += 2*(predictY[j]-y_train[j])*x_train[j,i]
        weight[i] = weight[i] - rate*grade

    grade = 0
    for j in range(0,len(x_train)):
        grade += 2*(predictY[j]-y_train[j])
        weight[num_input] = weight[num_input] - rate*grade

    return loss


for epoch in range(0,100):
     begin = 0
     while begin < len(x_train):
          end = begin + batch
          if end > len(x_train):
               end = len(x_train)

          loss = train(x_train[begin:end],y_train[begin:end])

          begin = end

          print("epoch: %d-loss: %f"%(epoch,loss))      #打印迭代次数和损失函数
print(weight)