“层”是神经网络中功能的单位。通常把神经网络的“层”实现为一个类。“层”的实现中有两个共通的方法 forward() 和 backward()。forward() 对应正向传播,backward() 对应反向传播。
加法结点的反向传播将上游的值原封不动地输出到下游,乘法结点的反向传播将上游的值乘以正向传播时输入信号的“翻转值”后传递给下游。
本文涉及两个代码:一是神经网络加法层与乘法层的实现代码 layer_naive.py,另一个是神经网络加法层与乘法层的应用代码 buy_apple_orange.py。
【加法层与乘法层的实现代码 layer_naive.py】
# layer_naive.py
class MulLayer:
def __init__(self):
self.x = None
self.y = None
def forward(self, x, y):
self.x = x
self.y = y
out = x * y
return out
def backward(self, dout):
dx = dout * self.y
dy = dout * self.x
return dx, dy
class AddLayer:
def __init__(self):
self.x = None
self.y = None
def forward(self, x, y):
self.x = x
self.y = y
out = x + y
return out
def backward(self, dout):
dx = dout * 1
dy = dout * 1
return dx, dy
【加法层与乘法层的应用代码 buy_apple_orange.py】
本例中,神经网络加法层与乘法层的应用代码 buy_apple_orange.py,是依据下图进行编写的。需要提醒的是,下图是购买2个苹果和3个橘子的计算图。
# buy_apple_orange.py
from layer_naive import MulLayer,AddLayer
apple = 100
apple_num = 2
orange = 150
orange_num = 3
tax = 1.1
# layer
mul_apple_layer = MulLayer()
mul_orange_layer = MulLayer()
add_apple_orange_layer = AddLayer()
mul_tax_layer = MulLayer()
# forward
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)
# backward
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("price:", int(price))
print("dApple:", dapple)
print("dApple_num:", int(dapple_num))
print("dOrange:", dorange)
print("dOrange_num:", int(dorange_num))
print("dTax:", dtax)
程序 buy_apple_orange.py 运行后的结果如下:
price: 715
dApple: 2.2
dApple_num: 110
dOrange: 3.3000000000000003
dOrange_num: 165
dTax: 650