<s> import numpy as np <EOL> from bayespy import nodes <EOL> from bayespy . inference import VB <EOL> from bayespy . inference . vmp . nodes . constant import Constant <EOL> from bayespy . inference . vmp . nodes . categorical import CategoricalMoments <EOL> import bayespy . plot as bpplt <EOL> def model ( n_documents , n_topics , n_vocabulary , corpus , word_documents , plates_multiplier = <NUM_LIT:1> ) : <EOL> '''<STR_LIT>''' <EOL> p_topic = nodes . Dirichlet ( np . ones ( n_topics ) , <EOL> plates = ( n_documents , ) , <EOL> name = '<STR_LIT>' ) <EOL> p_word = nodes . Dirichlet ( np . ones ( n_vocabulary ) , <EOL> plates = ( n_topics , ) , <EOL> name = '<STR_LIT>' ) <EOL> word_documents = Constant ( CategoricalMoments ( n_documents ) , word_documents , <EOL> name = '<STR_LIT>' ) <EOL> topics = nodes . Categorical ( nodes . Gate ( word_documents , p_topic ) , <EOL> plates = ( len ( corpus ) , ) , <EOL> plates_multiplier = ( plates_multiplier , ) , <EOL> name = '<STR_LIT>' ) <EOL> words = nodes . Categorical ( nodes . Gate ( topics , p_word ) , <EOL> name = '<STR_LIT>' ) <EOL> words . observe ( corpus ) <EOL> p_topic . initialize_from_random ( ) <EOL> p_word . initialize_from_random ( ) <EOL> return VB ( words , topics , p_word , p_topic , word_documents ) <EOL> def generate_data ( n_documents , n_topics , n_vocabulary , n_words ) : <EOL> word_documents = nodes . Categorical ( np . ones ( n_documents ) / n_documents , <EOL> plates = ( n_words , ) ) . random ( ) <EOL> p_topic = nodes . Dirichlet ( <NUM_LIT> * np . ones ( n_topics ) , <EOL> plates = ( n_documents , ) ) . random ( ) <EOL> p_word = nodes . Dirichlet ( <NUM_LIT> * np . ones ( n_vocabulary ) , <EOL> plates = ( n_topics , ) ) . random ( ) <EOL> topic = nodes . Categorical ( p_topic [ word_documents ] , <EOL> plates = ( n_words , ) ) . random ( ) <EOL> corpus = nodes . Categorical ( p_word [ topic ] , <EOL> plates = ( n_words , ) ) . random ( ) <EOL> bpplt . pyplot . figure ( ) <EOL> bpplt . hinton ( p_topic ) <EOL> bpplt . pyplot . title ( "<STR_LIT>" ) <EOL> bpplt . pyplot . xlabel ( "<STR_LIT>" ) <EOL> bpplt . pyplot . ylabel ( "<STR_LIT>" ) <EOL> bpplt . pyplot . figure ( ) <EOL> bpplt . hinton ( p_word ) <EOL> bpplt . pyplot . title ( "<STR_LIT>" ) <EOL> bpplt . pyplot . xlabel ( "<STR_LIT>" ) <EOL> bpplt . pyplot . ylabel ( "<STR_LIT>" ) <EOL> return ( corpus , word_documents ) <EOL> def run ( n_documents = <NUM_LIT:30> , n_topics = <NUM_LIT:5> , n_vocabulary = <NUM_LIT:10> , n_words = <NUM_LIT> , stochastic = False , maxiter = <NUM_LIT:1000> , seed = None ) : <EOL> if seed is not None : <EOL> np . random . seed ( seed ) <EOL> ( corpus , word_documents ) = generate_data ( n_documents , n_topics , n_vocabulary , n_words ) <EOL> if not stochastic : <EOL> Q = model ( n_documents = n_documents , n_topics = n_topics , n_vocabulary = n_vocabulary , <EOL> corpus = corpus , word_documents = word_documents ) <EOL> Q . update ( repeat = maxiter ) <EOL> else : <EOL> subset_size = <NUM_LIT:1000> <EOL> Q = model ( n_documents = n_documents , n_topics = n_topics , n_vocabulary = n_vocabulary , <EOL> corpus = corpus [ : subset_size ] , word_documents = word_documents [ : subset_size ] , <EOL> plates_multiplier = n_words / subset_size ) <EOL> Q . ignore_bound_checks = True <EOL> delay = <NUM_LIT:1> <EOL> forgetting_rate = <NUM_LIT> <EOL> for n in range ( maxiter ) : <EOL> subset = np . random . choice ( n_words , subset_size ) <EOL> Q [ '<STR_LIT>' ] . observe ( corpus [ subset ] ) <EOL> Q [ '<STR_LIT>' ] . set_value ( word_documents [ subset ] ) <EOL> Q . update ( '<STR_LIT>' ) <EOL> step = ( n + delay ) ** ( - forgetting_rate ) <EOL> Q . gradient_step ( '<STR_LIT>' , '<STR_LIT>' , scale = step ) <EOL> bpplt . pyplot . figure ( ) <EOL> bpplt . pyplot . plot ( Q . L ) <EOL> bpplt . pyplot . figure ( ) <EOL> bpplt . hinton ( Q [ '<STR_LIT>' ] ) <EOL> bpplt . pyplot . title ( "<STR_LIT>" ) <EOL> bpplt . pyplot . xlabel ( "<STR_LIT>" ) <EOL> bpplt . pyplot . ylabel ( "<STR_LIT>" ) <EOL> bpplt . pyplot . figure ( ) <EOL> bpplt . hinton ( Q [ '<STR_LIT>' ] ) <EOL> bpplt . pyplot . title ( "<STR_LIT>" ) <EOL> bpplt . pyplot . xlabel ( "<STR_LIT>" ) <EOL> bpplt . pyplot . ylabel ( "<STR_LIT>" ) <EOL> return <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> import sys , getopt , os <EOL> try : <EOL> opts , args = getopt . getopt ( sys . argv [ <NUM_LIT:1> : ] , <EOL> "<STR_LIT>" , <EOL> [ "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" ] ) <EOL> except getopt . GetoptError : <EOL> print ( '<STR_LIT>' ) <EOL> print ( '<STR_LIT>' ) <EOL> print ( '<STR_LIT>' ) <EOL> print ( '<STR_LIT>' ) <EOL> print ( '<STR_LIT>' ) <EOL> print ( '<STR_LIT>' ) <EOL> print ( '<STR_LIT>' ) <EOL> print ( '<STR_LIT>' ) <EOL> sys . exit ( <NUM_LIT:2> ) <EOL> kwargs = { } <EOL> for opt , arg in opts : <EOL> if opt == "<STR_LIT>" : <EOL> kwargs [ "<STR_LIT>" ] = int ( arg ) <EOL> elif opt == "<STR_LIT>" : <EOL> kwargs [ "<STR_LIT>" ] = int ( arg ) <EOL> elif opt == "<STR_LIT>" : <EOL> kwargs [ "<STR_LIT>" ] = int ( arg ) <EOL> elif opt == "<STR_LIT>" : <EOL> kwargs [ "<STR_LIT>" ] = int ( arg ) <EOL> elif opt == "<STR_LIT>" : <EOL> kwargs [ "<STR_LIT>" ] = int ( arg ) <EOL> elif opt == "<STR_LIT>" : <EOL> kwargs [ "<STR_LIT>" ] = int ( arg ) <EOL> elif opt == "<STR_LIT>" : <EOL> kwargs [ "<STR_LIT>" ] = True <EOL> run ( ** kwargs ) <EOL> bpplt . pyplot . show ( ) </s>
