<s> import numpy as np <EOL> from itertools import product <EOL> from ldacgsseq import LdaCgsSeq <EOL> __all__ = [ '<STR_LIT>' ] <EOL> def uniquify ( l ) : <EOL> """<STR_LIT>""" <EOL> mem = set ( [ ] ) <EOL> out = [ ] <EOL> for e in l : <EOL> if e not in mem : <EOL> mem . add ( e ) <EOL> out . append ( e ) <EOL> return out <EOL> def productoid ( A , n ) : <EOL> prod = product ( A , repeat = n ) <EOL> d = dict ( ( i , A [ : i ] ) for i in xrange ( <NUM_LIT:1> , len ( A ) + <NUM_LIT:1> ) ) <EOL> for t in prod : <EOL> elems = uniquify ( t ) <EOL> if elems == d [ len ( elems ) ] : <EOL> yield t <EOL> class LdaExact ( LdaCgsSeq ) : <EOL> @ property <EOL> def arg_maxima ( self ) : <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> return self . _arg_maxima <EOL> return [ ] <EOL> @ arg_maxima . setter <EOL> def arg_maxima ( self , l ) : <EOL> self . _arg_maxima = l <EOL> def _Z_values ( self ) : <EOL> A = range ( self . K ) <EOL> p = productoid ( A , len ( self . corpus ) ) <EOL> for t in p : <EOL> yield np . array ( t , dtype = np . int ) <EOL> def _init_model ( self , Z ) : <EOL> m = LdaCgsSeq ( context_type = self . context_type , <EOL> K = self . K , V = self . V , alpha = self . alpha , beta = self . beta ) <EOL> m . corpus = self . corpus <EOL> m . V = self . V <EOL> m . indices = self . indices <EOL> m . Z = Z <EOL> m . _compute_top_doc ( ) <EOL> m . _compute_word_top ( ) <EOL> m . inv_top_sums = <NUM_LIT:1.> / self . word_top . sum ( <NUM_LIT:0> ) <EOL> m . iteration = <NUM_LIT:1> <EOL> m . log_probs = [ ( <NUM_LIT:1> , m . _compute_log_prob ( ) ) ] <EOL> return m <EOL> def _log_probs ( self ) : <EOL> Z = self . _Z_values ( ) <EOL> for next_Z in Z : <EOL> m = self . _init_model ( next_Z ) <EOL> yield ( next_Z , m . log_probs [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] ) <EOL> def arg_max ( self , verbose = <NUM_LIT:1> ) : <EOL> max_log_prob = - np . inf <EOL> maxima = [ ] <EOL> log_probs = self . _log_probs ( ) <EOL> for ( Z , log_prob ) in log_probs : <EOL> if log_prob == max_log_prob : <EOL> maxima . append ( ( Z , log_prob ) ) <EOL> elif log_prob > max_log_prob : <EOL> max_log_prob = log_prob <EOL> maxima = [ ( Z , log_prob ) ] <EOL> self . arg_maxima = maxima <EOL> self . Z = maxima [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] <EOL> self . _compute_top_doc ( ) <EOL> self . _compute_word_top ( ) <EOL> self . inv_top_sums = <NUM_LIT:1.> / self . word_top . sum ( <NUM_LIT:0> ) <EOL> self . iteration = <NUM_LIT:1> <EOL> self . log_probs = [ ( <NUM_LIT:1> , max_log_prob ) ] <EOL> if verbose > <NUM_LIT:0> : <EOL> print '<STR_LIT>' , len ( self . arg_maxima ) <EOL> def all_estimates ( self ) : <EOL> for ( Z , log_prob ) in self . arg_maxima : <EOL> yield self . _init_model ( Z ) </s>
