Completed
Push — master ( c8682f...5bbe2a )
by Raphael
01:33
created

experiments.highway_networks.HighwayLayer   A

Complexity

Total Complexity 3

Size/Duplication

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A output() 0 4 1
A __init__() 0 5 1
A prepare() 0 9 1
1
#!/usr/bin/env python
2
# -*- coding: utf-8 -*-
3
4
from deepy.layers import NeuralLayer
5
from deepy.utils import build_activation
6
import theano.tensor as T
7
8
class HighwayLayer(NeuralLayer):
9
    """
10
    Highway network layer.
11
    See http://arxiv.org/abs/1505.00387.
12
    """
13
14
    def __init__(self, activation='relu', init=None, gate_bias=-5):
15
        super(HighwayLayer, self).__init__("highway")
16
        self.activation = activation
17
        self.init = init
18
        self.gate_bias = gate_bias
19
20
    def prepare(self):
21
        self.output_dim = self.input_dim
22
        self._act = build_activation(self.activation)
23
        self.W_h = self.create_weight(self.input_dim, self.input_dim, "h", initializer=self.init)
24
        self.W_t = self.create_weight(self.input_dim, self.input_dim, "t", initializer=self.init)
25
        self.B_h = self.create_bias(self.input_dim, "h")
26
        self.B_t = self.create_bias(self.input_dim, "t", value=self.gate_bias)
27
28
        self.register_parameters(self.W_h, self.B_h, self.W_t, self.B_t)
29
30
    def output(self, x):
31
        t = self._act(T.dot(x, self.W_t) + self.B_t)
32
        h = self._act(T.dot(x, self.W_h) + self.B_h)
33
        return h * t + x * (1 - t)
34
35