神经网络(二)

本文通过Python代码介绍神经网络的前向传播、sigmoid函数、向后传播(SGD)和性能评估,详细解析了神经网络的工作原理。重点在于网络的激活值计算、权重和偏置的更新以及梯度下降策略。
摘要由CSDN通过智能技术生成

在本章节,我们将结合一小段python代码来进一步地说明神经网络的思想。(源代码可以从https://github.com/mnielsen/neural-networks-and-deep-learning 下载,本文只是对该代码进行了学习,未做任何修改)。关于神经网络的具体理论请参考上一章节进行学习。

核心代码是Network对象:

class Network(object):
sizes变量指明神经网络共多少层,每一层包含多少个神经元。神经网络参数bias和weight都以随机值作为迭代优化的初始点。

    def __init__(self, sizes):
        """The list ``sizes`` contains the number of neurons in the
        respective layers of the network.  For example, if the list
        was [2, 3, 1] then it would be a three-layer network, with the
        first layer containing 2 neurons, the second layer 3 neurons,
        and the third layer 1 neuron.  The biases and weights for the
        network are initialized randomly, using a Gaussian
        distribution with mean 0, and variance 1.  Note that the first
        layer is assumed to be an input layer, and by convention we
        won't set any biases for those neurons, since biases are only
        ever used in computing the outputs from later layers."""

        self.num_layers = len(sizes)
        self.sizes = sizes
        self.biases = [np.random.randn(y, 1) for y in sizes[1:]]
        self.weights = [np.random.randn(y, x)
                        for x, y in zip(sizes[:-1], sizes[1:])]

前向传播代码:
根据公式,当前层神经元的激活值 al=σ(wlal1+bl) ,由第一层神经元依次向后推算每一层神经元的激活值,直至输出层神经元。

def feedforward(self, a):
    """Return the output of the network if ``a`` is input."""
    for b, w in zip(self.biases, self.weights):
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值