FullyConnected.py 2.14 KB
Newer Older
Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
1 2 3 4 5
#!/usr/bin/env python
# vim: set fileencoding=utf-8 :
# @author: Tiago de Freitas Pereira <tiago.pereira@idiap.ch>
# @date: Wed 11 May 2016 17:38 CEST

6
import tensorflow as tf
Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
7 8
from bob.learn.tensorflow.util import *
from .Layer import Layer
9
from operator import mul
Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
10 11 12 13 14 15 16 17


class FullyConnected(Layer):

    """
    2D Convolution
    """

18
    def __init__(self, name, output_dim, activation=None,
Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
19 20 21 22 23 24 25 26 27 28 29 30 31 32
                 initialization='xavier',
                 use_gpu=False,
                 seed=10
                 ):
        """
        Constructor

        **Parameters**
        input: Layer input
        activation: Tensor Flow activation
        initialization: Initialization type
        use_gpu: Store data in the GPU
        seed: Seed for the Random number generation
        """
33 34 35 36 37
        super(FullyConnected, self).__init__(name, activation=activation,
                                             initialization=initialization, use_gpu=use_gpu, seed=seed)
        self.output_dim = output_dim
        self.W = None
        self.b = None
Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
38
        self.shape = None
Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
39

Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
40 41
    def create_variables(self, input_layer):
        self.input_layer = input_layer
42
        if self.W is None:
Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
43
            input_dim = reduce(mul, self.input_layer.get_shape().as_list())
Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
44

45 46
            self.W = create_weight_variables([input_dim, self.output_dim],
                                             seed=self.seed, name=str(self.name), use_gpu=self.use_gpu)
Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
47 48
            #if self.activation is not None:
            self.b = create_bias_variables([self.output_dim], name=str(self.name)+"_bias", use_gpu=self.use_gpu)
Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
49 50

    def get_graph(self):
Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
51 52

        with tf.name_scope(str(self.name)):
53

Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
54 55 56
            if len(self.input_layer.get_shape()) == 4:
                shape = self.input_layer.get_shape().as_list()
                fc = tf.reshape(self.input_layer, [shape[0], shape[1] * shape[2] * shape[3]])
Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
57 58
            else:
                fc = self.input_layer
Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
59

Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
60
            if self.activation is not None:
61
                non_linear_fc = tf.nn.tanh(tf.matmul(fc, self.W) + self.b)
Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
62
                output = non_linear_fc
Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
63 64
            else:
                output = tf.matmul(fc, self.W) + self.b
Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
65

Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
66
            return output