-
Notifications
You must be signed in to change notification settings - Fork 0
/
MBGB.py
38 lines (36 loc) · 1.66 KB
/
MBGB.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#https://blog.csdn.net/studyvcmfc/article/details/103064840
# -*- coding: utf-8 -*-
import random
# 用y = Θ1*x1 + Θ2*x2来拟合下面的输入和输出
# input1 1 2 5 4
# input2 4 5 1 2
# output 19 26 19 20
input_x = [[1, 4], [2, 5], [5, 1], [4, 2]] # 输入
y = [19, 26, 19, 20] # 输出
theta = [1, 1] # θ参数初始化
loss = 10 # loss先定义一个数,为了进入循环迭代
step_size = 0.01 # 步长
eps = 0.0001 # 精度要求
max_iters = 10000 # 最大迭代次数
error = 0 # 损失值
iter_count = 0 # 当前迭代次数
while (loss > eps and iter_count < max_iters): # 迭代条件
loss = 0
# 这里每次批量选取的是2组样本进行更新,另一个点是随机点+1的相邻点
i = random.randint(0, 3) # 随机抽取一组样本
j = (i + 1) % 4 # 抽取另一组样本,j=i+1
pred_y0 = theta[0] * input_x[i][0] + theta[1] * input_x[i][1] # 预测值1
pred_y1 = theta[0] * input_x[j][0] + theta[1] * input_x[j][1] # 预测值2
theta[0] = theta[0] - step_size * (1 / 2) * (
(pred_y0 - y[i]) * input_x[i][0] + (pred_y1 - y[j]) * input_x[j][0]) # 对应5式
theta[1] = theta[1] - step_size * (1 / 2) * (
(pred_y0 - y[i]) * input_x[i][1] + (pred_y1 - y[j]) * input_x[j][1]) # 对应5式
for i in range(3):
pred_y = theta[0] * input_x[i][0] + theta[1] * input_x[i][1] # 总预测值
error = (1 / (2 * 2)) * (pred_y - y[i]) ** 2 # 损失值
loss = loss + error # 总损失值
iter_count += 1
print('iters_count', iter_count)
print('theta: ', theta)
print('final loss: ', loss)
print('iters: ', iter_count)