代码:
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], [4, 1], [3, 2]] # 输入 y = [19, 26, 19, 20] # 输出 theta = [1, 1] # θ参数初始化 loss = 10 # loss先定义一个数,为了进入循环迭代 step_size = 0.001 # 步长 eps = 0.000001 # 精度要求 max_iters = 50 # 最大迭代次数 error = 0 # 损失值 iter_count = 0 # 当前迭代次数 while (loss > eps and iter_count < max_iters): loss = 0 i = random.randint(0, 3) pred_y = theta[0] * input_x[i][0] + theta[1] * input_x[i][1] theta[0] = theta[0] - step_size * (pred_y - y[i]) * input_x[i][0] theta[1] = theta[1] - step_size * (pred_y - y[i]) * input_x[i][1] for i in range(3): pred_y = theta[0] * input_x[i][0] + theta[1] * input_x[i][1] error = 0.5 * (pred_y - y[i]) ** 2 loss = loss + error iter_count += 1 print('当前迭代次数', iter_count) print('theta: ', theta) print('final lose: ', loss) print('iters: ', iter_count)
截图: