|
1
|
|
|
from deepy.utils import build_activation, FLOATX |
|
2
|
|
|
import theano.tensor as T |
|
3
|
|
|
from . import NeuralLayer |
|
4
|
|
|
|
|
5
|
|
|
class Dense(NeuralLayer): |
|
6
|
|
|
""" |
|
7
|
|
|
Fully connected layer. |
|
8
|
|
|
""" |
|
9
|
|
|
|
|
10
|
|
|
def __init__(self, size, activation='linear', init=None, disable_bias=False, random_bias=False): |
|
11
|
|
|
super(Dense, self).__init__("dense") |
|
12
|
|
|
self.activation = activation |
|
13
|
|
|
self.output_dim = size |
|
14
|
|
|
self.disable_bias = disable_bias |
|
15
|
|
|
self.random_bias = random_bias |
|
16
|
|
|
self.initializer = init |
|
17
|
|
|
|
|
18
|
|
|
def prepare(self): |
|
19
|
|
|
self._setup_params() |
|
20
|
|
|
self._setup_functions() |
|
21
|
|
|
|
|
22
|
|
|
def output(self, x): |
|
23
|
|
|
return self._activation(T.dot(x, self.W) + self.B) |
|
24
|
|
|
|
|
25
|
|
|
def _setup_functions(self): |
|
26
|
|
|
self._activation = build_activation(self.activation) |
|
27
|
|
|
|
|
28
|
|
|
def _setup_params(self): |
|
29
|
|
|
self.W = self.create_weight(self.input_dim, self.output_dim, self.name, initializer=self.initializer) |
|
30
|
|
|
self.register_parameters(self.W) |
|
31
|
|
|
if self.disable_bias: |
|
32
|
|
|
self.B = T.constant(0, dtype=FLOATX) |
|
33
|
|
|
elif self.random_bias: |
|
34
|
|
|
self.B = self.create_weight(suffix=self.name + "_bias", |
|
35
|
|
|
initializer=self.initializer, |
|
36
|
|
|
shape=(self.output_dim, )) |
|
37
|
|
|
self.register_parameters(self.B) |
|
38
|
|
|
else: |
|
39
|
|
|
self.B = self.create_bias(self.output_dim, self.name) |
|
40
|
|
|
self.register_parameters(self.B) |
|
41
|
|
|
|