# Path: emolga/utils/theano_utils.py
# def shared_zeros(shape, dtype=theano.config.floatX, name=None):
#     return sharedX(np.zeros(shape), dtype=dtype, name=name)
# 
# def shared_scalar(val=0., dtype=theano.config.floatX, name=None):
#     return theano.shared(np.cast[dtype](val), name=name)
# 
# def floatX(X):
#     return np.asarray(X, dtype=theano.config.floatX)
# 
# Path: emolga/utils/generic_utils.py
# def get_from_module(identifier, module_params, module_name, instantiate=False, kwargs=None):
#     if isinstance(identifier, six.string_types):
#         res = module_params.get(identifier)
#         if not res:
#             raise Exception('Invalid ' + str(module_name) + ': ' + str(identifier))
#         if instantiate and not kwargs:
#             return res()
#         elif instantiate and kwargs:
#             return res(**kwargs)
#         else:
#             return res
#     return identifier


import theano
import sys
import theano.tensor as T
import logging
from theano.sandbox.rng_mrg import MRG_RandomStreams
from emolga.utils.theano_utils import shared_zeros, shared_scalar, floatX
from emolga.utils.generic_utils import get_from_module
from six.moves import zip
from copy import copy, deepcopy
from __future__ import absolute_import



logger = logging.getLogger(__name__)


def clip_norm(g, c, n):
    if c > 0:
        g = T.switch(T.ge(n, c), g * c / n, g)
    return g


def kl_divergence(p, p_hat):
    return p_hat - p + p * T.log(p / p_hat)


class Optimizer(object):
    def __init__(self, **kwargs):
        self.__dict__.update(kwargs)
        self.updates   = []
        self.save_parm = []

    def add(self, v):

# Path: emolga/utils/theano_utils.py
# def shared_zeros(shape, dtype=theano.config.floatX, name=None):
#     return sharedX(np.zeros(shape), dtype=dtype, name=name)
# 
# def shared_scalar(val=0., dtype=theano.config.floatX, name=None):
#     return theano.shared(np.cast[dtype](val), name=name)
# 
# def floatX(X):
#     return np.asarray(X, dtype=theano.config.floatX)
# 
# Path: emolga/utils/generic_utils.py
# def get_from_module(identifier, module_params, module_name, instantiate=False, kwargs=None):
#     if isinstance(identifier, six.string_types):
#         res = module_params.get(identifier)
#         if not res:
#             raise Exception('Invalid ' + str(module_name) + ': ' + str(identifier))
#         if instantiate and not kwargs:
#             return res()
#         elif instantiate and kwargs:
#             return res(**kwargs)
#         else:
#             return res
#     return identifier


# Path: emolga/basic/optimizers.py
import theano
import sys
import theano.tensor as T
import logging
from theano.sandbox.rng_mrg import MRG_RandomStreams
from emolga.utils.theano_utils import shared_zeros, shared_scalar, floatX
from emolga.utils.generic_utils import get_from_module
from six.moves import zip
from copy import copy, deepcopy

from __future__ import absolute_import



logger = logging.getLogger(__name__)


def clip_norm(g, c, n):
    if c > 0:
        g = T.switch(T.ge(n, c), g * c / n, g)
    return g


def kl_divergence(p, p_hat):
    return p_hat - p + p * T.log(p / p_hat)


class Optimizer(object):
    def __init__(self, **kwargs):
        self.__dict__.update(kwargs)
        self.updates   = []
        self.save_parm = []

    def add(self, v):

        self.save_parm += [v]