使用numpy构建多层感知机

本文详述了如何使用numpy构建多层感知机,包括全连接层的正反向传播、激活函数如sigmoid和relu的计算、代价函数如MES和交叉熵、优化器SGD的实现,并通过乳腺癌数据集进行网络训练和测试。
摘要由CSDN通过智能技术生成

本文公式较多,由于简书不支持公式渲染,公式完整版请移步个人博客

import numpy as np

目标

使用numpy实现多层感知机的正向和反向传播

层次构建

全连接层

正向传播

正向传播的公式为:$Y = f(W \times X + b)$,其中,Y为输出,W为权值,b为偏置

反向传播

对于反向传播,已知上一层传回的梯度为dY,对应的反向传播公式为:
$$dX = (W^{T} \times dY) \cdot f'(Y)$$
$$dW = \cfrac{1}{m} dY \times X^{T}$$
$$db = \cfrac{1}{m} \sum dY$$

代码实现

class numpy_fc(object):

    def __init__(self, in_channel, out_channel, optim):
        self.weight = np.float64(np.random.randn(out_channel, in_channel) * 0.1)
        self.bias = np.zeros((out_channel, 1),dtype=np.float64)
        self.in_data = np.zeros((1, in_channel))
        self.out_data = None
        self.weight_grad = None
        self.bias_grad = None
        self.optimizer = optim

    def forward(self, data):
        self.in_data = data
        self.out_data = np.dot(self.weight, data) + self.bias
        return self.out_data

    def backward(self, grad):
        data_grad = np.dot(self.weight.T, grad)
        self.weight_grad = np.dot(grad, self.in_data.T)
        self.bias_grad = np.sum(grad, axis=1).reshape((-1,1))
        return data_grad

    def step(self):
#         print(self.bias_grad.shape,self.bias.shape)
        self.weight += self.optimizer(self.weight_grad)
        self.bias += self.optimizer(self.bias_grad)

代码测试

test_fc = numpy_fc(16,8,None)
test_fc_forward = test_fc.forward(np.random.rand(16,10))
print(test_fc_forward.shape)
test_fc_back = test_fc.backward(test_fc_forward)
print(test_fc_back.shape)
print(test_fc.weight_grad.shape,test_fc.weight.shape)
print(test_fc.bias_grad.shape,test_fc.bias.shape)
(8, 10)
(16, 10)
(8, 16) (8, 16)
(8, 1) (8, 1)

激活函数

sigmoid函数

sigmoid函数是常用的二分类问题输出层激活函数,前向传播和反向传播分别如下所示:
$$ sigmoid(x) = \cfrac{1}{1 + e^{-x}}$$
$$ sigmoid'(x) = sigmoid(x) \cdot (1

  • 3
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值