• 鱼书学习笔记:激活函数层的实现


    此处的神经网络层都利用了计算图的正向传播和反向传播的概念,关于详细内容参考[1][2],此处只列出Python的代码实现

    ReLU层

    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

    Relu类有实例变量mask。这个变量mask是由True/False构成的NumPy数组,它会把正向传播时的输入x的元素中小于等于0的地方保存为True,其他地方(大于0的元素)保存为False。

    Sigmoid层

    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

    这个实现中,正向传播时将输出保存在了实例变量out中。然后,反向传播时,使用该变量out进行计算。

    Affine层    神经网络的正向传播中进行的矩阵的乘积运算在几何学领域被称为“仿射变换”(几何中,仿射变换包括一次线性变换和一次平移,分别对应神经网络的加权和运算与加偏置运算)。因此,这里将进行仿射变换的处理实现为“Affine层”

    class Affine:
        def __init__(self, W, b):
            self.W =W
            self.b = b
            
            self.x = None
            self.original_x_shape = None
            # 权重和偏置参数的导数
            self.dW = None
            self.db = None
    
        def forward(self, x):
            # 对应张量
            self.original_x_shape = x.shape
            x = x.reshape(x.shape[0], -1)
            self.x = x
    
            out = np.dot(self.x, self.W) + self.b
    
            return out
    
        def backward(self, dout):
            dx = np.dot(dout, self.W.T)
            self.dW = np.dot(self.x.T, dout)
            self.db = np.sum(dout, axis=0)
            
            dx = dx.reshape(*self.original_x_shape)  # 还原输入数据的形状(对应张量)
            return dx

    Softmax(Softmax-with-Loss)层

        神经网络中进行的处理有推理(inference)学习两个阶段。神经网络的推理在只需要给出一个答案的情况下,只对推理的得分最大值感兴趣,因此神经网络的推理通常不使用Softmax层。不过,神经网络的学习阶段则需要Softmax层

    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: # 监督数据是one-hot-vector的情况
                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

    参考资料:

    [1]http://karpathy.github.io/neuralnets/

    [2]https://cs231n.github.io/

  • 相关阅读:
    vue-cli3安装使用
    document.readyState
    js的堆与栈
    常用方法
    js常见排序算法
    微信小程序swiper高度问题
    微信小程序滑动菜单
    数据筛选和排序------的解析
    使用Windows实现数据绑定----------的解析
    实现Windoes程序的数据更新------的详细解析
  • 原文地址:https://www.cnblogs.com/J14nWe1/p/14572821.html
Copyright © 2020-2023  润新知