<jupyter_start><jupyter_text>PostNauka. RecipeHere goes an example of using the Baseline recipe which helps to build a generally good topic model.* Recipes in TopicNet: [recipes](https://github.com/machine-intelligence-laboratory/TopicNet/tree/master/topicnet/cooking_machine/recipes)  Contents* [Loading data](data)* [Formatting the recipe](formatting)* [Experiment](experiment)* [Best Model](best-model)    * [Score plots](plots)    * [Viewers (for more detailed exploration)](viewers)<jupyter_code>import numpy as np
import os
import shutil
import tempfile
import matplotlib.pyplot as plt
%matplotlib inline

from matplotlib import cm
from IPython.display import display_html
from topicnet.dataset_manager import load_dataset
from topicnet.cooking_machine.model_tracking import START
from topicnet.cooking_machine.pretty_output import make_notebook_pretty
from topicnet.cooking_machine.recipes import BaselineRecipe
from topicnet.viewers import (
    TopDocumentsViewer,
    TopTokensViewer
)
make_notebook_pretty()<jupyter_output><empty_output><jupyter_text>Loading dataBack to Contents Let's download some dataset from those [available in TopicNet](https://github.com/machine-intelligence-laboratory/TopicNet/tree/master/topicnet/dataset_manager).<jupyter_code>DATASET_NAME = 'postnauka'
DATASET = load_dataset(DATASET_NAME)
DATASET._data.head()
DATASET_PATH = DATASET._data_path
DATASET_PATH<jupyter_output><empty_output><jupyter_text>Formatting the recipeBack to Contents Below we load a recipe, which is a carcass for model training pipeline, and format it.<jupyter_code>training_pipeline = BaselineRecipe()

training_pipeline.format_recipe(
    modality_list           = ['@word'],     # first modality is the main one
    dataset_path            = DATASET_PATH,
    topic_number            = 15,
    background_topic_number = 1,
)

print(training_pipeline._recipe)<jupyter_output># This config follows a strategy described by <NAME>
# one of the core programmers of BigARTM library in personal correspondence.
# According to his letter 'decent' topic model can be obtained by
# Decorrelating model topics simultaneously looking at retrieved TopTokens


# Use .format(modality_list=modality_list, main_modality=main_modality, dataset_path=dataset_path,
# specific_topics=specific_topics, background_topics=background_topics)
# when loading the recipe to adjust for your dataset

topics:
# Describes number of model topics, better left to the user to define optimal topic number
    specific_topics: ['topic_0', 'topic_1', 'topic_2', 'topic_3', 'topic_4', 'topic_5', 'topic_6', 'topic_7', 'topic_8', 'topic_9', 'topic_10', 'topic_11', 'topic_12', 'topic_13', 'topic_14']
    background_topics: ['bcg_15']

# Here is example of model with one modality
regularizers:
    - DecorrelatorPhiRegularizer:
        name: decorrelation_phi
        topic_names: specific_topics
      [...]<jupyter_text>ExperimentBack to Contents Experiment folder should be unique:<jupyter_code>EXPERIMENT_PATH = tempfile.mkdtemp()
EXPERIMENT_PATH
experiment, dataset = training_pipeline.build_experiment_environment(
    save_path=EXPERIMENT_PATH,
    experiment_id=f'{DATASET_NAME}_Baseline',
)<jupyter_output><empty_output><jupyter_text>Running the experiment pipeline!<jupyter_code>%%time

experiment.run(dataset, verbose=True, nb_verbose=True)<jupyter_output><empty_output><jupyter_text>What models and stages we have:<jupyter_code>experiment.show()<jupyter_output><empty_output><jupyter_text>Best ModelBack to Contents Let's select a model which is the best on the last stage:<jupyter_code>BEST_MODEL = experiment.select(experiment.criteria[-1][0])[0]
BEST_MODEL.describe_regularizers()
BEST_MODEL.describe_scores()<jupyter_output><empty_output><jupyter_text>Score plotsBack to Contents If we want to make plots, we need to restore all score values for trained models first<jupyter_code>%%time

MODEL_SCORES = dict()

for model_id, model in experiment.models.items():
    if model_id == START:
        continue
    
    model = model.restore()
    MODEL_SCORES[model_id] = model.scores
MODEL_COLORS = np.linspace(0, 0.65, len(MODEL_SCORES))
ITERATION_LABEL = 'Iteration'<jupyter_output><empty_output><jupyter_text>Solid line represent the best model we have selected; dashed lines are the rest.<jupyter_code>plt.figure(figsize=(8,8))

score_name = 'BleiLaffertyScore'

for i, (tm_id, tm_scores) in enumerate(MODEL_SCORES.items()):
    score = tm_scores[score_name]
    ls = '-' if tm_id == BEST_MODEL.model_id else '--'

    plt.plot(score, color=cm.Reds(MODEL_COLORS[i]), ls=ls)

plt.ylabel(score_name)
plt.xlabel(ITERATION_LABEL)

plt.show()
plt.figure(figsize=(8,8))

score_name = 'PerplexityScore@all'

for i, (tm_id, tm_scores) in enumerate(MODEL_SCORES.items()):
    score = tm_scores[score_name]
    ls = '-' if tm_id == BEST_MODEL.model_id else '--'

    plt.plot(score, color=cm.Reds(MODEL_COLORS[i]), ls=ls)

plt.ylabel(score_name)
plt.xlabel(ITERATION_LABEL)

plt.show()
plt.figure(figsize=(8,8))

for i, (tm_id, tm_scores) in enumerate(MODEL_SCORES.items()):
    ls = '-' if tm_id == BEST_MODEL.model_id else '--'

    for score_name, color_map in zip([
                'TopicKernel@word.average_purity',
                'TopicKernel@word.average_contrast',
                'SparsityPhiScore@word',
            ], [cm.Blues, cm.Reds, cm.Greens]):
        
        score = tm_scores[score_name]
        label = score_name if tm_id == BEST_MODEL.model_id else None
        plt.plot(score, color=color_map(MODEL_COLORS[i]), ls=ls, label=label)


plt.xlabel(ITERATION_LABEL)
plt.legend()

plt.show()<jupyter_output><empty_output><jupyter_text>Viewers (for more detailed exploration)Back to Contents Applying viewers to examin the model:<jupyter_code>top_tok_viewer = TopTokensViewer(
    BEST_MODEL, num_top_tokens=10, method='phi'
)
top_doc_viewer = TopDocumentsViewer(
    BEST_MODEL, dataset=dataset
)
top_tokens_view = top_tok_viewer.view_from_jupyter(
    display_output=False,
    give_html=True,
)
display_html(' '.join(top_tokens_view), raw=True)
top_doc_viewer.view_from_jupyter(current_num_top_doc=5)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10088005215123859, 'fraction_numerical': 0.006681877444589309, 'mean_word_length': 5.714442013129103, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 41, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '436497', 'license': 'MIT', 'n_tokens_mistral': 2138, 'n_tokens_neox': 2065, 'n_words': 450, 'path': 'topicnet/demos/PostNauka-Recipe.ipynb', 'repo_name': 'DmitriyValetov/TopicNet', 'size': '6143'}