DL之BP:利用乘法层/加法层(forward+backward)算法结合计算图(CG)求解反向求导应用题

DL之BP:利用乘法层/加法层(forward+backward)算法结合计算图(CG)求解反向求导应用题

导读
计算图中层的实现(加法层/乘法层),其实非常简单,使用这些层可以进行复杂的导数计算。可以进一步利用计算图思路,来理解神经网络中的运作机制。


利用乘法层(forward+backward)算法结合计算图(CG)求解反向求导应用题

实现购买2个苹果

输出结果

CG思路

实现代码

apple = 100
apple_num = 2
tax = 1.1

apple_price = mul_apple_layer.forward(apple, apple_num)
price = mul_tax_layer.forward(apple_price, tax)

dprice = 1
dapple_price, dtax = mul_tax_layer.backward(dprice)
dapple, dapple_num = mul_apple_layer.backward(dapple_price)

print('仅使用乘法层:购买2个苹果和消费税的例子')
print("price:", int(price))
print("dApple:", dapple)
print("dApple_num:", int(dapple_num))
print("dTax:", dtax)

利用加法层(forward+backward)算法结合计算图(CG)求解反向求导应用题

实现购买2个苹果和3个橘子的例子

输出结果

CG思路

实现代码

apple = 100
apple_num = 2
orange = 150
orange_num = 3
tax = 1.1

apple_price = mul_apple_layer.forward(apple, apple_num)                # (1)
orange_price = mul_orange_layer.forward(orange, orange_num)            # (2)
all_price = add_apple_orange_layer.forward(apple_price, orange_price)  # (3)
price = mul_tax_layer.forward(all_price, tax)                          # (4)

dprice = 1
dall_price, dtax = mul_tax_layer.backward(dprice)                          # (4)
dapple_price, dorange_price = add_apple_orange_layer.backward(dall_price)  # (3)
dorange, dorange_num = mul_orange_layer.backward(dorange_price)            # (2)
dapple, dapple_num = mul_apple_layer.backward(dapple_price)                # (1)

print('混合使用加法层和乘法层,实现购买2个苹果和3个橘子的例子')
print("price:", int(price))
print("dApple:", dapple)
print("dApple_num:", int(dapple_num))
print("dOrange:", dorange)
print("dOrange_num:", int(dorange_num))
print("dTax:", dtax)

参考文章
DL之CG:Computational Graph计算图的简介、入门、使用之详细攻略DL之BP:利用乘法层/加法层(forward+backward)算法结合计算图(CG)求解反向求导应用题

(0)

相关推荐