Completed
Push — master ( f73e69...91b7c0 )
by Raphael
01:35
created

BatchNormalization   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 21
Duplicated Lines 0 %
Metric Value
dl 0
loc 21
rs 10
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __init__() 0 3 1
A compute_tensor() 0 7 1
A prepare() 0 4 1
1
#!/usr/bin/env python
2
# -*- coding: utf-8 -*-
3
4
import theano.tensor as T
5
6
from . import NeuralLayer
7
8
class BatchNormalization(NeuralLayer):
9
    """
10
    Batch normalization.
11
    http://arxiv.org/pdf/1502.03167v3.pdf
12
    """
13
    def __init__(self, epsilon=1e-6, weights=None):
14
        super(BatchNormalization,self).__init__("norm")
15
        self.epsilon = epsilon
16
17
    def prepare(self):
18
        self.gamma = self.create_weight(shape=(self.input_dim,), suffix="gamma")
19
        self.beta = self.create_bias(self.input_dim, suffix="beta")
20
        self.register_parameters(self.gamma, self.beta)
21
22
    def compute_tensor(self, x):
23
24
        m = x.mean(axis=0)
25
        std = T.mean((x-m)**2 + self.epsilon, axis=0) ** 0.5
26
        x_normed = (x - m) / (std + self.epsilon)
27
        out = self.gamma * x_normed + self.beta
28
        return out