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