# Path: seya/regularizers.py
# class LambdaRegularizer(Regularizer):
#     def __init__(self, cost):
#         super(LambdaRegularizer, self).__init__()
#         self.cost = cost
# 
#     def __call__(self, loss):
#         loss += self.cost
#         return loss
# 
#     def get_config(self):
#         return {"name": self.__class__.__name__}


import numpy as np
from keras import backend as K
from keras.layers.core import Layer
from seya.regularizers import LambdaRegularizer
        self.ksize = ksize
        self.distance = distance

    def _get_kernel(self, X, Z):
        G = K.sum((K.expand_dims(X, dim=1) - Z)**2, axis=-1)  # Gram matrix
        G = K.exp(-G/(self.ksize)) / K.sqrt(2*np.pi*self.ksize)
        return G

    def build(self):
        X = self.get_input()
        # if self.distribution == 'normal':
        #    Z = K.random_normal(K.shape(X), std=5)
        Z = self.distribution(K.shape(X))

        Gxx = self._get_kernel(X, X)
        Gzz = self._get_kernel(Z, Z)
        Gxz = self._get_kernel(X, Z)
        if self.distance == 'euclidean':  # same as minimum mean discrepancy
            itl_divergence = K.mean(Gxx) + K.mean(Gzz) - 2*K.mean(Gxz)

        elif self.distance == 'csd':  # Cauchy-Schwarz divergence
            itl_divergence = K.log(K.sqrt(K.mean(Gxx)*K.mean(Gzz)) /
                                   K.mean(Gxz))

        elif self.distance == 'entropy':  # Maximum entropy
            itl_divergence = K.mean(Gxx)

        elif self.distance == 'cip':  # cross-information potential
            itl_divergence = -K.mean(Gxz)


# Path: seya/regularizers.py
# class LambdaRegularizer(Regularizer):
#     def __init__(self, cost):
#         super(LambdaRegularizer, self).__init__()
#         self.cost = cost
# 
#     def __call__(self, loss):
#         loss += self.cost
#         return loss
# 
#     def get_config(self):
#         return {"name": self.__class__.__name__}


# Path: seya/layers/regularization.py
import numpy as np
from keras import backend as K
from keras.layers.core import Layer
from seya.regularizers import LambdaRegularizer

        self.ksize = ksize
        self.distance = distance

    def _get_kernel(self, X, Z):
        G = K.sum((K.expand_dims(X, dim=1) - Z)**2, axis=-1)  # Gram matrix
        G = K.exp(-G/(self.ksize)) / K.sqrt(2*np.pi*self.ksize)
        return G

    def build(self):
        X = self.get_input()
        # if self.distribution == 'normal':
        #    Z = K.random_normal(K.shape(X), std=5)
        Z = self.distribution(K.shape(X))

        Gxx = self._get_kernel(X, X)
        Gzz = self._get_kernel(Z, Z)
        Gxz = self._get_kernel(X, Z)
        if self.distance == 'euclidean':  # same as minimum mean discrepancy
            itl_divergence = K.mean(Gxx) + K.mean(Gzz) - 2*K.mean(Gxz)

        elif self.distance == 'csd':  # Cauchy-Schwarz divergence
            itl_divergence = K.log(K.sqrt(K.mean(Gxx)*K.mean(Gzz)) /
                                   K.mean(Gxz))

        elif self.distance == 'entropy':  # Maximum entropy
            itl_divergence = K.mean(Gxx)

        elif self.distance == 'cip':  # cross-information potential
            itl_divergence = -K.mean(Gxz)


        self.regularizers = [LambdaRegularizer(self.scale * itl_divergence), ]