Typical python example¶
This section describe all steps, included in a typical BigARTM script (for example example02_parse_collection.py).
The following python script parses docword.kos.txt and vocab.kos.txt files and converts them into a set of binary-serialized batches, stored on disk. In addition the script creates a dictionary with all unique tokens in the collection and stored it on disk. The script also detects if it had been already executed, and in this case it just loads the dictionary and save it in unique_tokens variable.
The same logic is implemented in a helper-method ParseCollectionOrLoadDictionary method.
data_folder = sys.argv if (len(sys.argv) >= 2) else '' target_folder = 'kos' collection_name = 'kos' batches_found = len(glob.glob(target_folder + "/*.batch")) if batches_found == 0: print "No batches found, parsing them from textual collection...", parser_config = artm.messages_pb2.CollectionParserConfig(); parser_config.format = artm.library.CollectionParserConfig_Format_BagOfWordsUci parser_config.docword_file_path = data_folder + 'docword.'+ collection_name + '.txt' parser_config.vocab_file_path = data_folder + 'vocab.'+ collection_name + '.txt' parser_config.target_folder = target_folder parser_config.dictionary_file_name = 'dictionary' unique_tokens = artm.library.Library().ParseCollection(parser_config); print " OK." else: print "Found " + str(batches_found) + " batches, using them." unique_tokens = artm.library.Library().LoadDictionary(target_folder + '/dictionary');
You may also download larger collections from the following links. You can get the original collection (docword file and vocab file) or an already precompiled batches and dictionary.
Master component is you main entry-point to all BigARTM functionality. The following script creates master component and configures it with several regularizers and score calculators.
with artm.library.MasterComponent(disk_path = target_folder) as master: perplexity_score = master.CreatePerplexityScore() sparsity_theta_score = master.CreateSparsityThetaScore() sparsity_phi_score = master.CreateSparsityPhiScore() top_tokens_score = master.CreateTopTokensScore() theta_snippet_score = master.CreateThetaSnippetScore() dirichlet_theta_reg = master.CreateDirichletThetaRegularizer() dirichlet_phi_reg = master.CreateDirichletPhiRegularizer() decorrelator_reg = master.CreateDecorrelatorPhiRegularizer()
Master component must be configured with a disk path, which should contain a set of batches produced in the previous step of this tutorial.
Score calculators allows you to retrieve important quality measures for your topic model. Perplexity, sparsity of theta and phi matrices, lists of tokens with highest probability within each topic are all examples of such scores. By default BigARTM does not calculate any scores, so you have to create in master component. The same is true for regularizers, that allow you to customize your topic model.
For further details about master component refer to MasterComponentConfig.
Configure Topic Model¶
Topic model configuration defins the number of topics in the model, the list of scores to be calculated, and the list of regularizers to apply to the model. For further details about model configuration refer to ModelConfig.
model = master.CreateModel(topics_count = 10, inner_iterations_count = 10) model.EnableScore(perplexity_score) model.EnableScore(sparsity_phi_score) model.EnableScore(sparsity_theta_score) model.EnableScore(top_tokens_score) model.EnableScore(theta_snippet_score) model.EnableRegularizer(dirichlet_theta_reg, -0.1) model.EnableRegularizer(dirichlet_phi_reg, -0.2) model.EnableRegularizer(decorrelator_reg, 1000000) model.Initialize(unique_tokens) # Setup initial approximation for Phi matrix.
Note that on the last step we configured the initial approximation of Phi matrix. This step is optional — BigARTM is able to collect all tokens dynamically during first scan of the collection. However, a deterministic initial approximation helps to reproduce the same results from run to run.
The following script performs several scans over the set of batches. Depending on the size of the collection this step might be quite time-consuming. It is good idea to output some information after every step.
for iter in range(0, 8): master.InvokeIteration(1) # Invoke one scan of the entire collection... master.WaitIdle(); # and wait until it completes. model.Synchronize(); # Synchronize topic model. print "Iter#" + str(iter), print ": Perplexity = %.3f" % perplexity_score.GetValue(model).value, print ", Phi sparsity = %.3f" % sparsity_phi_score.GetValue(model).value, print ", Theta sparsity = %.3f" % sparsity_theta_score.GetValue(model).value
If your collection is very large you may want to utilize online algorithm that updates topic model several times during each iteration, as it is demonstrated by the following script:
master.InvokeIteration(1) # Invoke one scan of the entire collection... while True: done = master.WaitIdle(100) # wait 100 ms model.Synchronize(0.9) # decay weights in current topic model by 0.9, if (done): # append all increments and invoke all regularizers. break;
Retrieve and visualize scores¶
Finally, you are interested in retrieving and visualizing all collected scores.