Python深度学习基础(二)——反向传递概念透彻解析以及Python手动实现

221 阅读2分钟

持续创作,加速成长!这是我参与「掘金日新计划 · 6 月更文挑战」的第4天,点击查看活动详情

最简单的反向传递

我们在感知机中进行的最简单的操作就是加法和乘法,这里我们先以乘法和除法为例实现最简单的反向传递

乘法层

公式 我们假设x*y=z, 损失函数为L,那么我们分别对z求关于x和y的偏导得 zx=y\frac{ \partial z}{\partial x}=y zy=x \frac{\partial z}{\partial y}=x 得到结论乘法层的偏导为两个乘数互换位置 则 Lzzx=Lzy\frac{ \partial L}{\partial z}\frac{ \partial z}{\partial x}=\frac{ \partial L}{\partial z} \cdot y Lzzy=Lzx \frac{ \partial L}{\partial z}\frac{\partial z}{\partial y}=\frac{ \partial L}{\partial z} \cdot x 代码实现 在反向传递时要遵循链式法则,所以在这里我们每个偏导都要乘以后面一层反向传递来的偏导数dout才是应该传递给上一层的偏导数,下同。

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

加法层

公式 我们假设x+y=z, 损失函数为L那么我们分别对z求关于x和y的偏导得 zx=1\frac{ \partial z}{\partial x}=1 zy=1 \frac{\partial z}{\partial y}=1Lx=Lzzx=Lz \frac{\partial L}{\partial x}= \frac{\partial L}{\partial z} \frac{\partial z}{\partial x}= \frac{\partial L}{\partial z}

Ly=Lzzy=Lz \frac{\partial L}{\partial y}= \frac{\partial L}{\partial z} \frac{\partial z}{\partial y}= \frac{\partial L}{\partial z} 代码实现

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

激活函数的反向传递

Relu层

公式 y={x,x>00,x<=0y=\begin{cases} x, & {x>0} \\ 0, & {x<=0} \end{cases} dydx={1,x>00,x<=0\frac{dy}{dx}=\begin{cases} 1, & {x>0} \\ 0, & {x<=0} \end{cases}dLdx=dLdydydx={dLdy,x>00,x<=0 \frac{d L}{d x}= \frac{dL}{dy}\frac{dy}{dx}=\begin{cases} \frac{dL}{dy}, & {x>0} \\ 0, & {x<=0} \end{cases} 代码

class Relu:
    def __init__(self):
        self.mask = None

    def forward(self, x):
        self.mask = (x <= 0)
        out = x.copy()
        out[self.mask] = 0

        return out

    def backward(self, dout):
        dout[self.mask] = 0
        dx = dout

        return dx

Sigmoid层

公式 y=11+exy=\frac{1}{1+e^{-x}} dydx=ex(1+ex)2=11+exex1+ex=11+ex(111+ex)=y(1y)\frac{dy}{dx}=\frac{e^{-x}}{(1+e^{-x})^2}=\frac{1}{1+e^{-x}} \cdot\frac{e^{-x}}{1+e^{-x}}=\frac{1}{1+e^{-x}} \cdot(1-\frac{1}{1+e^{-x}})=y \cdot (1-y)dLdydydx=dLdyy(1y)\frac{dL}{dy} \frac{dy}{dx}=\frac{dL}{dy} \cdot y \cdot (1-y)

代码

def sigmoid(x):
    return 1 / (1 + np.exp(-x))    

class Sigmoid:
    def __init__(self):
        self.out = None

    def forward(self, x):
        out = sigmoid(x)
        self.out = out
        return out

    def backward(self, dout):
        dx = dout * (1.0 - self.out) * self.out

        return dx

带交叉熵误差的SoftMax层

公式 这个函数反向传递的实质是传回真实值与预测值的差距 代码

def cross_entropy_error(y, t):
    if y.ndim == 1:
        t = t.reshape(1, t.size)
        y = y.reshape(1, y.size)
        
    if t.size == y.size:
        t = t.argmax(axis=1)
             
    batch_size = y.shape[0]
    return -np.sum(np.log(y[np.arange(batch_size), t] + 1e-7)) / batch_size
    
class SoftmaxWithLoss:
    def __init__(self):
        self.loss = None
        self.y = None # softmax的输出
        self.t = None # 监督数据

    def forward(self, x, t):
        self.t = t
        self.y = softmax(x)
        self.loss = cross_entropy_error(self.y, self.t)
        
        return self.loss

    def backward(self, dout=1):
        batch_size = self.t.shape[0]
        if self.t.size == self.y.size: 
            dx = (self.y - self.t) / batch_size
        else:
            dx = self.y.copy()
            dx[np.arange(batch_size), self.t] -= 1
            dx = dx / batch_size
        
        return dx