动手写一个神经网络代码(附Backpropagation Algorithm代码分解)

本文介绍了如何动手实现一个神经网络,详细分解了反向传播算法的步骤,包括权重初始化、前馈传导、残差计算、偏导数求解以及权重更新。还探讨了权重初始化的改进策略,如使用随机分布,以及增加正则化项、通过validation寻找最优超参数等优化方案。并提到了Quadratic Cost作为损失函数的应用。
摘要由CSDN通过智能技术生成

先上Michal Daniel(传送门)的代码。类Network有六个成员函数,其中SGD、update_mini_batch、backprop负责计算每echo的残差、W和b偏导数、W和b的更新。feedforward、evaluation负责计算前向传导的值,可用于计算每echo训练集和验证集的error。cost_derivative计算网络最后一层的残差。

#### Libraries
# Standard library
import random
# Third-party libraries
import numpy as np

class Network(object):
    def __init__(self, sizes):
        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:])]
    def feedforward(self, a):
        """Return the output of the network if ``a`` is input."""
        for b, w in zip(self.biases, self.weights):
            a = sigmoid(np.dot(w, a)+b)
        return a

    def SGD(self, training_data, epochs, mini_batch_size, eta,
            test_data=None):
        if test_data: n_test = len(test_data)
        n = len(training_data)
        for j in xrange(epochs):
            random.shuffle(training_data)
            mini_batches = [
                training_data[k:k+mini_batch_size]
                for k in xrange(0, n, mini_batch_size)]
            for mini_batch in mini_batches:
                self.update_mini_batch(mini_batch, eta)
            if test_data:
                print "Epoch {0}: {1} / {2}".format(
                    j, self.evaluate(test_data), len(test_data))
            else:
                print "Epoch {0} complete".format(j)

    def update_mini_batch(self, mini_batch, eta):
        nabla_b = [np.zeros(b.shape) for b in self.biases]
        nabla_w = [np.zeros(w.shape) for w in self.weights]
        for x, y in mini_batch:
            delta_nabla_b, delta_nabla_w = self.backprop(x, y)
            nabla_b = [nb+dnb for nb, dnb in zip(nabla_b, delta_nabla_b)]
            nabla_w = [nw+dnw for nw, dnw in zip(nabla_w, delta_nabla_w)]
        self.weights = [w-(eta/len(mini_batch))*nw
                        for w, nw in zip(self.weights, nabla_w)]
        self.biases = [b-(eta/len(mini_batch))*nb
                       for b, nb in zip(self.biases, nabla_b)]

    def backprop(self, x, y): 
        nabla_b = [np.zeros(b.shape) for b in self.biases]
        nabla_w = [np.zeros(w.shape) for w in self.weights]
        # feedforward
        activation = x
        activations = [x] # list to store all the activations, layer by layer
        zs = [] # list to store all the z vectors, layer by layer
        for b, w in zip(self.biases, self.weights):  # feedforward 同时保存隐藏层计算的中间值结果
            z = np.dot(w, activation)+b
            zs.append(z)  # zs保存了每层神经元输入值
            activation = sigmoid(z)
            activations.append(activation) 

        delta = self.cost_derivative(activations[-1], y) * \
            sigmoid_prime(zs[-1])
        nabla_b[-1] = delta  
        nabla_w[-1] = np.dot(delta, activations[-2].transpose())   

        for l in xrange(2, self.num_layers):
            z = zs[-l]
            sp = sigmoid_prime(z)
            delta = np.dot(self.weights[-l+1].transpose(), delta) * sp
            nabla_b[-l] = delta
            nabla_w[-l] = np.dot(delta, activations[-l-1].transpose()) # l 不是 1
        return (nabla_b, nabla_w)
    def evaluate(self, test_data)
        test_results = [(np.argmax(self.feedforward(x)), y)
                        for (x, y) in test_data]
        # print test_results
        return sum(int(x == y) for (x, y) in test_results)
#cost的导数
    def cost_derivative(self, output_activations, y):
        return (output_activations-y)

#### Miscellaneous functions
def sigmoid(z):
    """The sigmoid function."""
    return 1.0/(1.0+np.exp(-z))

def sigmoid_prime(z):
    """Derivative of the sigmoid function."""
    return sigmoid(z)*(1-sigmoid(z))

步骤分解:

首先需要传入的参数有层数、每层的神经元个数

根据传入参数初始化权重W和b,注意初始值必须是随机值,比如使用服从N(0, ϵ2 )正态分布的随机值。如果初始化用全0,隐藏层会得到与输入值相同的函数,随机值目的是消除对称性。
输入数据X在每一epoch迭代前都要重新打乱,然后按照mini_batch_size大小切分数据,依次用每个batch训练更新W和b。每个epoch需要把所有batch训练完,训练完后可以测试下用现在的W和b能预测出什么样的结果来,并与真实值对比。然后进入下一epoch重复训练。

反馈传导步骤分解,公式代码可以对应:

1.进行前馈传导计算,利用前向传导公式,计算 L1 , L2 , …直到 Lnl 的激活值。这个过程类似feedforward函数,不过我们需要保存隐藏层的计算结果以便后面求残差和偏导数。

z=sigmoid(wx+b)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值