#NEWLINE# def KL_divergence( self , variational_posterior ) : #NEWLINE# #INDENT# mu = variational_posterior . mean #NEWLINE# S = variational_posterior . variance #NEWLINE# if self . group_spike : #NEWLINE# #INDENT# gamma = variational_posterior . binary_prob [ 0 ] #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# gamma = variational_posterior . binary_prob #NEWLINE# #UNINDENT# if ( len ( self . pi . shape ) == 2 ) : #NEWLINE# #INDENT# idx = np . unique ( ( gamma . _raveled_index ( ) / gamma . shape [ ( - 1 ) ] ) ) #NEWLINE# pi = self . pi [ idx ] #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# pi = self . pi #NEWLINE# #UNINDENT# var_mean = ( np . square ( mu ) / self . variance ) #NEWLINE# var_S = ( ( S / self . variance ) - np . log ( S ) ) #NEWLINE# var_gamma = ( ( gamma * np . log ( ( gamma / pi ) ) ) . sum ( ) + ( ( 1 - gamma ) * np . log ( ( ( 1 - gamma ) / ( 1 - pi ) ) ) ) . sum ( ) ) #NEWLINE# return ( var_gamma + ( ( ( 1.0 - self . _b_prob_all ) * ( ( ( np . log ( self . variance ) - 1.0 ) + var_mean ) + var_S ) ) . sum ( ) / ( 2.0 * self . nModels ) ) )