首页人工智能10.链式法则(chain...

10.链式法则(chain relu)、反向传播和在简单层的实现

之前我们是通过数值微分计算了神经网络的权重参数的梯度,虽说数值微分简单易实现,但是缺点也很明显,就是比较费时间。这篇主要介绍一个能够高效计算权重参数的梯度的方法——误差反向传播法。

之前我们知道神经网络的前向计算过程,而反向传播将参数(局部导数)进行反方向传递,而这种传递过程是基于链式法则(chain relu)的。

1.链式法则(chain relu)与反向传播:

链式法则是关于复合函数(由多个函数构成的函数)的导数的性质,定义如下:
如果某个函数由复合函数表示,则该复合函数的导数可以用构成复合函数的各个函数的导数的乘积表示。

例如,现在有一个函数

$$
z=\left( x+y \right) ^2
$$

该函数就是复合函数,是由下面所示的两个式子构成:

$$
z=t^2
$$

$$
t=x+y
$$

那么上述函数中z关于x的导数用链式法则表示如下:

$$
\frac{\partial z}{\partial x}=\frac{\partial z}{\partial t}\frac{\partial t}{\partial x}
$$

现在使用链式法则计算z关于x的导数:

$$
\frac{\partial z}{\partial x}=\frac{\partial z}{\partial t}\frac{\partial t}{\partial x}=2t\times 1=2\left( x+y \right)
$$

2.简单层的实现:

实现书中给的购买橘子和苹果的例子,

# 构建乘法节点
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):
        pass

    def forward(self, x, y):
        out = x + y

        return out

    def backward(self, dout):
        dx = dout * 1
        dy = dout * 1

        return dx, dy

# 给定苹果和橘子的价格及购买数量,以及购买税
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)
orange_price = mul_orange_layer.forward(orange, orange_num)
all_price = add_apple_orange_layer.forward(apple_price, orange_price)
price = mul_tax_layer.forward(all_price, tax)

# backward反向传播
dprice = 1
dall_price, dtax = mul_tax_layer.backward(dprice)
dapple_price, dorange_price = add_apple_orange_layer.backward(dall_price)
dorange, dorange_num = mul_orange_layer.backward(dorange_price)
dapple, dapple_num = mul_apple_layer.backward(dapple_price)

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)

运行得到结果

price: 715
dApple: 2.2
dApple_num: 110
dOrange: 3.3000000000000003
dOrange_num: 165
dTax: 650

Reference:
《Deep Learning from Scratch》

RELATED ARTICLES

欢迎留下您的宝贵建议

Please enter your comment!
Please enter your name here

- Advertisment -

Most Popular

Recent Comments