代码实现bp算法示例

本文详细介绍了BP(反向传播)算法的工作原理,并通过一个具体的示例展示了如何用代码实现这一经典的人工神经网络训练算法。内容涵盖神经网络的基本结构、权重更新规则以及误差反向传播的过程。
摘要由CSDN通过智能技术生成
# encoding:utf-8
import numpy as np
import random


class Network(object):

    def __init__(self, sizes):
        self.num_layers = len(sizes)
        print("self.num_layers", self.num_layers)
        self.sizes = sizes
        self.biases = [np.random.randn(y, 1) for y in sizes[1:]]
        print("biases", self.biases)
        # biases [array([[-0.55676967],
        #        [-1.60486518],
        #        [ 0.28285971],
        #        [ 0.50856141]]), array([[-1.02563044]])]
        print("sizes[1:]", sizes[1:])
        # [4, 1]
        self.weights = [np.random.randn(y, x)
                        for x, y in zip(sizes[:-1], sizes[1:])]
        print("weights", self.weights)
        print("sizes[:-1]", sizes[:-1])
        # [3, 4]
        #  [array([[-0.60968692,  2.86107499,  0.25978906],
        #        [ 0.20304469, -1.42226893,  1.66564988],
        #        [-0.73320158,  0.53635735,  1.49307876],
        #        [-0.27301879, -1.42973446, -0.17142598]]), array([[ 0.39620414, -1.0758598 ,
        #        0.17447957, -0.67224015]])]

    def backprop(self, x, y):
        """return a tuple
        """

        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]  # 存放激活值

        zs = []  # list用来存放z 向量

        # 前向传递
        for b, w in zip(self.biases, self.weights):
            z = np.dot(w, activation) + b
            zs.append(z)
            activation = self.sigmoid(z)
            activations.append(activation)

        # 后向传递
        delta = self.cost_derivative(activations[-1], y) * self.sigmoid(zs[-1])

        nabla_b[-1] = delta
        nabla_w[-1] = np.dot(delta, activations[-2].transpose())

        for l in range(2, self.num_layers):
            z = zs[-l]
            sp = self.sigmoid_prime(z)
            delta = np.dot(self.weights[-l + 1].transpose(), delta) * sp
         
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值