#NEWLINE# def update_gradients_KL( 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# variational_posterior . mean . gradient -= ( ( gamma * mu ) / self . variance ) #NEWLINE# variational_posterior . variance . gradient -= ( ( ( ( 1.0 / self . variance ) - ( 1.0 / S ) ) * gamma ) / 2.0 ) #NEWLINE# from scipy.special import digamma , polygamma #NEWLINE# dgamma = ( ( ( np . log ( ( gamma / ( 1.0 - gamma ) ) ) + digamma ( tau [ : , 1 ] ) ) - digamma ( tau [ : , 0 ] ) ) / variational_posterior . num_data ) #NEWLINE# variational_posterior . binary_prob . gradient -= ( dgamma + ( ( ( ( ( ( np . square ( mu ) + S ) / self . variance ) - np . log ( S ) ) + np . log ( self . variance ) ) - 1.0 ) / 2.0 ) ) #NEWLINE# ad = ( self . alpha / self . input_dim ) #NEWLINE# common = ( ( ( ( ad + 2 ) - tau [ : , 0 ] ) - tau [ : , 1 ] ) * polygamma ( 1 , tau . sum ( axis = 1 ) ) ) #NEWLINE# variational_posterior . tau . gradient [ : , 0 ] = ( - ( ( ( ( tau [ : , 0 ] - gamma ) - ad ) * polygamma ( 1 , tau [ : , 0 ] ) ) + common ) ) #NEWLINE# variational_posterior . tau . gradient [ : , 1 ] = ( - ( ( ( ( tau [ : , 1 ] + gamma ) - 2 ) * polygamma ( 1 , tau [ : , 1 ] ) ) + common ) )