人工智能实战第二次作业_张绍恺
0.导航
项目 | 内容 |
---|---|
这个作业属于哪个课程 | 人工智能实战 |
这个作业的要求在哪里 | 第二次作业 - 双变量的反向传播 |
我在这个课程的目标是 | 开拓视野,积累AI实战经验 |
这个作业在哪个具体方面帮助我 | 熟悉BP求导的具体实现过程 |
1.具体作业内容
- 阅读文章线性反向传播,用python程序实现具体迭代计算过程
- 思考重新计算贡献值的影响
2.具体Python代码实现展示
重新计算贡献值
z_aim = 150.0
w = 3.0
b = 4.0
z = 162.0
delta_z = z - z_aim
error_range = 1e-5
count = 0
while(abs(delta_z) > error_range):
count += 1
x = 2 * w + 3 * b
y = 2 * b + 1
z = x * y
delta_z = z - z_aim
delta_b = ((delta_z / 2) / (2 * x + 3 * y))
delta_w = ((delta_z / 2) / (2 * y))
w = w - delta_w
b = b - delta_b
print("Interation %d: w = %.8f, b = %.8f, z = %.8f, error = %.8f" % (count, w, b, z, abs(delta_z)))
print("")
print("Final interation:")
print("Total interation times = %d, w = %.8f, b = %.8f, z = %.8f, error = %.8f" % (count, w, b, z, abs(delta_z)))
运行结果
Interation 1: w = 2.66666667, b = 3.90476190, z = 162.00000000, error = 12.00000000
Interation 2: w = 2.66151866, b = 3.90326327, z = 150.18140590, error = 0.18140590
Interation 3: w = 2.66151740, b = 3.90326291, z = 150.00004434, error = 0.00004434
Interation 4: w = 2.66151740, b = 3.90326291, z = 150.00000000, error = 0.00000000
Final interation:
Total interation times = 4, w = 2.66151740, b = 3.90326291, z = 150.00000000, error = 0.00000000
[Finished in 0.2s]
不考虑贡献值
仅需将上述代码中的以下两行
delta_b = ((delta_z / 2) / (2 * x + 3 * y))
delta_w = ((delta_z / 2) / (2 * y))
替换为
delta_b = ((delta_z / 2) / 63)
delta_w = ((delta_z / 2) / 18)
结果如下
Interation 1: w = 2.66666667, b = 3.90476190, z = 162.00000000, error = 12.00000000
Interation 2: w = 2.66162761, b = 3.90332218, z = 150.18140590, error = 0.18140590
Interation 3: w = 2.66147411, b = 3.90327832, z = 150.00552614, error = 0.00552614
Interation 4: w = 2.66146940, b = 3.90327697, z = 150.00016964, error = 0.00016964
Interation 5: w = 2.66146925, b = 3.90327693, z = 150.00000521, error = 0.00000521
Final interation:
Total interation times = 5, w = 2.66146925, b = 3.90327693, z = 150.00000521, error = 0.00000521
[Finished in 0.1s]
3.思考和比较
在反向传播的迭代过程中,随着z的值不断变化,delta_z的值也随之变化,因此即时更新z对w和b的偏导也十分必要,既能加快收敛速度,更快得到结果,从理论上也更加符合梯度下降的实现过程。