Numpy实战BP神经网络

代码如下

def sigmoid(z):
    return 1.0/(1.0+np.exp(-z))

def sigmoid_prime(z): # 导数
    return sigmoid(z)*(1-sigmoid(z))

class MLP:
    def __init__(self, sizes):
        self.sizes = sizes
        self.num_layers = len(sizes)
        # sizes: [784, 30, 10]
        # w : [ch_out, ch_in]
        # b : [ch_out]
        self.weights = [np.random.randn(ch_out, ch_in) for ch_in, ch_out in zip(sizes[:-1], sizes[1:])]
	    self.biases = [np.random.randn(ch_out, 1) for ch_out in sizes[1:]]
        
    def forward(self, x):
        # param x: [784, 1]
        # return: [10, 1]
        for b, w in zip(self.biases, self.weights):
            # [30, 784]@[784, 1] + [30, 1]=> [30, 1]
            # [10, 30]@[30, 1] + [10, 1]=> [10, 1]
            z = np.dot(w, x) + b
            x = sigmoid(z)
        return x
    
    def backward(self, x, y):
        # param x : [784, 1]
        # param y : [10, 1], one-hot encoding
        nabla_w = [np.zeros(w.shape) for w in self.weights]
        nabla_b = [np.zeros(b.shape) for b in self.biases]
        
        # 1. forward
        # save activation for every layer
        activation = [x]
        # save z for every layer(z是激活函数前面那层)
        zs = []
        activation = x
        for b, w in zip(self.biases, self.weights):
            z = np.dot(w, x) + b
            activation = sigmoid(z)
            zs.append(z)
            activations.append(activation)
        loss = np.power(activations[-1] - y, 2).sum()
        
		# 2. backward
        # 2.1 compute gradient on output layer
        delta = activations[-1] * (1 - activations[-1]) * (activations[-1] - y)
        nabla_b[-1] = delta
        # [10, 1]@[1, 30] => [10, 30]
        nabla_w[-1] = np.dot(delta, activations[-2].T)
        # 2.2 compute hidden gradient
        for l in range(2, self.num_layers + 1):
            l = -l
            z = zs[l]
            a = activations[l]
            delta = np.dot(self.weights[l+1].T, delta) * a * (1 - a)
            nabla_b[l] = delta
            nabla_w[l] = np.dot(delta, activations[l-1].T)
        return nabla_w, nabla_b, loss
    
    def train(self, training_data, epochs, batchsz, lr, test_data):
		# training_data : list of (x, y)
        # epochs : 1000
        # batchsz : 10
        # lr : 0.01
        # test_data : list of (x, y)
        if test_data: 
            n_test = len(test_data)
        n = len(training_data)
        for j in range(epochs):
            random.shuffle(training_data)
            batches = [training_data[k:k+batchsz] for k in range(0, n, batchsz)]
            
            # for every batch in current data
            for batch in batches:
                loss = self.update_batch(batch, lr)
            if test_data:
                print("Epoch {0}: {1} / {2}, Loss: {3}".format(j, self.evaluate(test_data), n_test, loss))
            else:
                print("Epoch {0} complete".format(j))
                
	def updata_batch(self, batch, lr):
        nabla_b = [np.zeros(b.shape) for b in self.biases]
        nabla_w = [np.zeros(w.shape) for w in self.weights]
        loss = 0
        # for every sample in current batch
        for x, y in batch:
            nabla_w, nabla_b, loss_ = self.backward(x, y)
            nabla_b = [accu + cur for accu, cur in zip(nabla_b, nabla_b_)]
            nabla_w = [accu + cur for accu, cur in zip(nabla_w, nabla_w_)]
            loss += loss_
            
		nabla_w = [w/len(batch) for w in nabla_w]
        nabla_b = [b/len(batch) for b in nabla_b]
        loss = loss / len(batch)
        # w = w - lr * nabla_w
        self.weights = [w - lr * nabla for w, nabla in zip(self.weights, nabla_w)]
        self.biases = [b - lr * nabla for b, nabla in zip(self.biases, nabla_b)]
        return loss
    
    def evaluate(self, test_data):
        # test_data : list of (x, y)
        result = [(np.argmax(self.forward(x)), y) for x, y in test_data]
		correct = sum(int(pred == y) for pred, y in result)
        return correct
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值