<jupyter_start><jupyter_text>Bayesian ModelA simplistic Bayesian unigral LM.<jupyter_code># Add "beer" to the PYTHONPATH
import sys
sys.path.insert(0, '../')
import random

import beer
import torch

# For plotting.
from bokeh.io import show, output_notebook
from bokeh.plotting import figure, gridplot
output_notebook()

# Convenience functions for plotting.
import plotting

%load_ext autoreload
%autoreload 2<jupyter_output><empty_output><jupyter_text>DataGenerate some normally distributed data:<jupyter_code>voc_size = 10
X = torch.LongTensor(random.choices(range(voc_size), k=500))<jupyter_output><empty_output><jupyter_text>Model CreationWe create two types of Normal distribution: one diagonal covariance matrix and another one with full covariance matrix.<jupyter_code>model = beer.UnigramLM.create(voc_size)<jupyter_output><empty_output><jupyter_text>Variational Bayes Training<jupyter_code>epochs = 10
lrate = 1.

optim = beer.BayesianModelCoordinateAscentOptimizer(model.mean_field_groups, 
                                                    lrate)

elbos = []
for epoch in range(epochs):
    optim.zero_grad()
    elbo = beer.evidence_lower_bound(model, X, datasize=len(X))
    elbo.natural_backward()
    elbos.append(float(elbo) / len(X))
    optim.step()
# Plot the ELBO.
fig = figure(title='ELBO', width=400, height=400, x_axis_label='step',
              y_axis_label='ln p(X)')
fig.line(range(len(elbos)), elbos)

show(fig)
model.weights<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10363391655450875, 'fraction_numerical': 0.010767160161507403, 'mean_word_length': 5.144628099173554, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '563323', 'license': 'MIT', 'n_tokens_mistral': 532, 'n_tokens_neox': 501, 'n_words': 106, 'path': 'examples/LM.ipynb', 'repo_name': 'RobinAlgayres/beer', 'size': '1486'}