numpy实现BN层

 BN层的主要逻辑是将数据进行一个标准化,然后添加了,两个可以学习的参数,来减弱标准化。

class BN():
    def __init__(self,channel,weight=[],bias=[],eps=1e-05):
        self.channel = channel
        self.weight = weight
        self.bias = bias
        self.eps =eps
        self.running_mean = []
        self.running_var = []
    def forward(self,x):



        data = x.transpose((1, 0, 2, 3)).reshape(self.channel, -1)
        if len(self.running_mean)==0 and len(self.running_var)==0:
            mu1 = data.mean(axis=1).reshape(1, self.channel, 1, 1)
            std1 = data.std(axis=1).reshape(1, self.channel, 1, 1)
        else:
            mu1  = self.running_mean.reshape(1, self.channel, 1, 1)
            std1 = np.sqrt(self.running_var).reshape(1, self.channel, 1, 1)

        if not len(self.weight) and not len(self.bias):
            numpy_bn = (x - mu1) / (std1 + self.eps)
        else:
            self.weight = self.weight.reshape(1, self.channel, 1, 1)
            self.bias= self.bias.reshape(1, self.channel, 1, 1)
            numpy_bn = (x - mu1) / (std1 + self.eps)*self.weight + self.bias
        return numpy_bn

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值