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

deepy.layers.PRelu   A

Complexity

Total Complexity 5

Size/Duplication

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A PRelu.compute_tensor() 0 4 1
A PRelu.__init__() 0 3 1
A PRelu.prepare() 0 7 3
1
#!/usr/bin/env python
2
# -*- coding: utf-8 -*-
3
4
from . import NeuralLayer
5
from conv import Convolution
6
7
class PRelu(NeuralLayer):
8
    """
9
    Probabilistic ReLU.
10
     - http://arxiv.org/pdf/1502.01852v1.pdf
11
    """
12
    def __init__(self, input_tensor=2):
13
        super(PRelu, self).__init__("prelu")
14
        self.input_tensor = input_tensor
15
16
    def prepare(self):
17
        self.alphas = self.create_bias(self.output_dim, "alphas")
18
        self.register_parameters(self.alphas)
19
        if self.input_tensor == 3:
20
            self.alphas = self.alphas.dimshuffle('x', 0, 'x')
21
        elif self.input_tensor == 4:
22
            self.alphas = self.alphas.dimshuffle('x', 0, 'x', 'x')
23
24
    def compute_tensor(self, x):
25
        positive_vector =  x * (x >= 0)
26
        negative_vector = self.alphas * (x * (x < 0))
27
        return positive_vector + negative_vector