#NEWLINE# def KL_divergence( self , variational_posterior ) : #NEWLINE# #INDENT# ( mu , S , gamma , tau ) = ( variational_posterior . mean . values , variational_posterior . variance . values , variational_posterior . gamma_group . values , variational_posterior . tau . values ) #NEWLINE# var_mean = ( np . square ( mu ) / self . variance ) #NEWLINE# var_S = ( ( S / self . variance ) - np . log ( S ) ) #NEWLINE# part1 = ( ( gamma * ( ( ( np . log ( self . variance ) - 1.0 ) + var_mean ) + var_S ) ) . sum ( ) / 2.0 ) #NEWLINE# ad = ( self . alpha / self . input_dim ) #NEWLINE# from scipy.special import betaln , digamma #NEWLINE# part2 = ( ( ( ( ( ( ( gamma * np . log ( gamma ) ) . sum ( ) + ( ( 1.0 - gamma ) * np . log ( ( 1.0 - gamma ) ) ) . sum ( ) ) + ( betaln ( ad , 1.0 ) * self . input_dim ) ) - betaln ( tau [ : , 0 ] , tau [ : , 1 ] ) . sum ( ) ) + ( ( ( tau [ : , 0 ] - gamma ) - ad ) * digamma ( tau [ : , 0 ] ) ) . sum ( ) ) + ( ( ( tau [ : , 1 ] + gamma ) - 2.0 ) * digamma ( tau [ : , 1 ] ) ) . sum ( ) ) + ( ( ( ( 2.0 + ad ) - tau [ : , 0 ] ) - tau [ : , 1 ] ) * digamma ( tau . sum ( axis = 1 ) ) ) . sum ( ) ) #NEWLINE# return ( part1 + part2 )