人工智能实战第2次作业_许骏鹏

第二次作业:双变量的反向传播

1、作业要求

  • 阅读文章线性反向传播,用python程序实现具体迭代过程
  • 思考是否重新计算贡献值的影响

    2、python代码实现

    考虑贡献值

aim_z = 150.0
w = 3.0
b = 4.0
z = 162.0
eps = 1e-5;
count = 0

while (abs(aim_z - z) > eps):
    count = count + 1
    x = 2 * w + 3 * b
    y = 2 * b + 1
    z = x * y
    delta_b = ((z - aim_z) / ((2 * x + 3 * y) * 2))
    delta_w = ((z - aim_z) / ((2 * y) * 2))
    w = w - delta_w
    b = b - delta_b
    print("iteration_times: %d, w = %.8f, b = %.8f, z = %.8f, error = %.8f" % (count, w, b, z, abs(aim_z - z)));

print("last iteration = %d"%(count));
print("w = %.8f, b = %.8f,z = %.8f,error = %.8f" % (w, b, z, abs(aim_z - z)));

运行结果

iteration_times: 1, w = 2.66666667, b = 3.90476190, z = 162.00000000, error = 12.00000000
iteration_times: 2, w = 2.66151866, b = 3.90326327, z = 150.18140590, error = 0.18140590
iteration_times: 3, w = 2.66151740, b = 3.90326291, z = 150.00004434, error = 0.00004434
iteration_times: 4, w = 2.66151740, b = 3.90326291, z = 150.00000000, error = 0.00000000
last iteration = 4
w = 2.66151740, b = 3.90326291,z = 150.00000000,error = 0.00000000

不考虑贡献值

仅替换如下代码

   delta_b = ((z - aim_z) / (63 * 2))
   delta_w = ((z - aim_z) / (18 * 2))

运行结果

iteration_times: 1, w = 2.66666667, b = 3.90476190, z = 162.00000000, error = 12.00000000
iteration_times: 2, w = 2.66162761, b = 3.90332218, z = 150.18140590, error = 0.18140590
iteration_times: 3, w = 2.66147411, b = 3.90327832, z = 150.00552614, error = 0.00552614
iteration_times: 4, w = 2.66146940, b = 3.90327697, z = 150.00016964, error = 0.00016964
iteration_times: 5, w = 2.66146925, b = 3.90327693, z = 150.00000521, error = 0.00000521
last iteration = 5
w = 2.66146925, b = 3.90327693,z = 150.00000521,error = 0.00000521

3、总结与反思

在进行迭代的过程中,明显实时考虑w,b的贡献值要更优。因为在z=f(w,b)的三维图像中,每次改变(w,b)的位置从而改变z的值,在这个过程中,随点的位置不同,z对w,b的偏导都会发生改变。实时记录这种改变,并带入迭代,会得到更加精确的逼近。

你可能感兴趣的:(人工智能实战第2次作业_许骏鹏)