Constant.py 1.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
#!/usr/bin/env python
# vim: set fileencoding=utf-8 :
# @author: Tiago de Freitas Pereira <tiago.pereira@idiap.ch>
# @date: Mon 05 Sep 2016 16:35 CEST

import logging
logger = logging.getLogger("bob.learn.tensorflow")

from .Initialization import Initialization
import tensorflow as tf


class Constant(Initialization):
    """
    Implements the constant initialization.
    This is usually used to initialize biases.

    This tip were extracted from here
    http://www.deeplearningbook.org/contents/optimization.html

    page: 302

    """

25
    def __init__(self, constant_value=0.1, use_gpu=False, seed=None):
26 27 28 29 30 31 32 33

        self.constant_value = constant_value
        super(Constant, self).__init__(seed=None, use_gpu=use_gpu)

    def __call__(self, shape, name):
        initializer = tf.constant(self.constant_value, shape=shape)

        if self.use_gpu:
34
            with tf.device("/gpu:0"):
35 36 37 38 39
                return tf.get_variable(name, initializer=initializer, dtype=tf.float32)
        else:
            with tf.device("/cpu"):
                return tf.get_variable(name, initializer=initializer, dtype=tf.float32)