<s> import numpy as np <EOL> import theano . tensor as T <EOL> import lasagne <EOL> from lasagne . layers . base import Layer <EOL> import math <EOL> class GaussianLogDensityLayer ( lasagne . layers . MergeLayer ) : <EOL> def __init__ ( self , x , mu , var , ** kwargs ) : <EOL> self . x , self . mu , self . var = None , None , None <EOL> if not isinstance ( x , Layer ) : <EOL> self . x , x = x , None <EOL> if not isinstance ( mu , Layer ) : <EOL> self . mu , mu = mu , None <EOL> if not isinstance ( var , Layer ) : <EOL> self . var , var = var , None <EOL> input_lst = [ i for i in [ x , mu , var ] if not i is None ] <EOL> super ( GaussianLogDensityLayer , self ) . __init__ ( input_lst , ** kwargs ) <EOL> def get_output_shape_for ( self , input_shapes ) : <EOL> return input_shapes [ <NUM_LIT:0> ] <EOL> def get_output_for ( self , input , ** kwargs ) : <EOL> x = self . x if self . x is not None else input . pop ( <NUM_LIT:0> ) <EOL> mu = self . mu if self . mu is not None else input . pop ( <NUM_LIT:0> ) <EOL> logvar = self . var if self . var is not None else input . pop ( <NUM_LIT:0> ) <EOL> c = - <NUM_LIT:0.5> * math . log ( <NUM_LIT:2> * math . pi ) <EOL> density = c - logvar / <NUM_LIT:2> - ( x - mu ) ** <NUM_LIT:2> / ( <NUM_LIT:2> * T . exp ( logvar ) ) <EOL> return T . sum ( density , axis = - <NUM_LIT:1> , keepdims = True ) <EOL> class GaussianMarginalLogDensityLayer ( lasagne . layers . MergeLayer ) : <EOL> def __init__ ( self , mu , var , ** kwargs ) : <EOL> self . mu , self . var = None , None <EOL> if not isinstance ( mu , Layer ) : <EOL> self . mu , mu = mu , None <EOL> if not isinstance ( var , Layer ) : <EOL> self . var , var = var , None <EOL> input_lst = [ i for i in [ mu , var ] if not i is None ] <EOL> super ( GaussianMarginalLogDensityLayer , self ) . __init__ ( input_lst , ** kwargs ) <EOL> def get_output_shape_for ( self , input_shapes ) : <EOL> return input_shapes [ <NUM_LIT:0> ] <EOL> def get_output_for ( self , input , ** kwargs ) : <EOL> mu = self . mu if self . mu is not None else input . pop ( <NUM_LIT:0> ) <EOL> logvar = self . var if self . var is not None else input . pop ( <NUM_LIT:0> ) <EOL> if mu == <NUM_LIT:1> : <EOL> density = - <NUM_LIT:0.5> * ( T . log ( <NUM_LIT:2> * np . pi ) + <NUM_LIT:1> + logvar ) <EOL> else : <EOL> density = - <NUM_LIT:0.5> * ( T . log ( <NUM_LIT:2> * np . pi ) + ( T . sqr ( mu ) + T . exp ( logvar ) ) ) <EOL> return T . sum ( density , axis = - <NUM_LIT:1> , keepdims = True ) <EOL> class BernoulliLogDensityLayer ( lasagne . layers . MergeLayer ) : <EOL> def __init__ ( self , x_mu , x , eps = <NUM_LIT> , ** kwargs ) : <EOL> input_lst = [ x_mu ] <EOL> self . eps = eps <EOL> self . x = None <EOL> if not isinstance ( x , Layer ) : <EOL> self . x , x = x , None <EOL> else : <EOL> input_lst += [ x ] <EOL> super ( BernoulliLogDensityLayer , self ) . __init__ ( input_lst , ** kwargs ) <EOL> def get_output_shape_for ( self , input_shapes ) : <EOL> return input_shapes [ <NUM_LIT:0> ] <EOL> def get_output_for ( self , input , ** kwargs ) : <EOL> x_mu = input . pop ( <NUM_LIT:0> ) <EOL> x = self . x if self . x is not None else input . pop ( <NUM_LIT:0> ) <EOL> if x_mu . ndim > x . ndim : <EOL> x = x . dimshuffle ( ( <NUM_LIT:0> , '<STR_LIT:x>' , '<STR_LIT:x>' , <NUM_LIT:1> ) ) <EOL> x_mu = T . clip ( x_mu , self . eps , <NUM_LIT:1> - self . eps ) <EOL> density = T . sum ( - T . nnet . binary_crossentropy ( x_mu , x ) , axis = - <NUM_LIT:1> , keepdims = True ) <EOL> return density <EOL> class MultinomialLogDensityLayer ( lasagne . layers . MergeLayer ) : <EOL> def __init__ ( self , x_mu , x , eps = <NUM_LIT> , ** kwargs ) : <EOL> input_lst = [ x_mu ] <EOL> self . eps = eps <EOL> self . x = None <EOL> if not isinstance ( x , Layer ) : <EOL> self . x , x = x , None <EOL> else : <EOL> input_lst += [ x ] <EOL> super ( MultinomialLogDensityLayer , self ) . __init__ ( input_lst , ** kwargs ) <EOL> def get_output_shape_for ( self , input_shapes ) : <EOL> return input_shapes [ <NUM_LIT:0> ] <EOL> def get_output_for ( self , input , ** kwargs ) : <EOL> x_mu = input . pop ( <NUM_LIT:0> ) <EOL> x = self . x if self . x is not None else input . pop ( <NUM_LIT:0> ) <EOL> x_mu += self . eps <EOL> if x_mu . ndim > x . ndim : <EOL> x = x . dimshuffle ( ( <NUM_LIT:0> , '<STR_LIT:x>' , '<STR_LIT:x>' , <NUM_LIT:1> ) ) <EOL> density = - ( - T . sum ( x * T . log ( x_mu ) , axis = - <NUM_LIT:1> , keepdims = True ) ) <EOL> return density </s>
