<jupyter_start><jupyter_text>Simple Directed Graphical Models in TF Probability- toc: true - badges: true- comments: true- author: <NAME>- categories: [ML, TFP, TF]  Basic Imports<jupyter_code>#import silence_tensorflow.auto

import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
import functools
import seaborn as sns
import tensorflow_probability as tfp
import pandas as pd

tfd = tfp.distributions
tfl = tfp.layers
tfb = tfp.bijectors

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.optimizers import RMSprop
from tensorflow.keras.callbacks import Callback

sns.reset_defaults()
sns.set_context(context="talk", font_scale=1)
%matplotlib inline
%config InlineBackend.figure_format='retina'
import pygraphviz as pgv

A = pgv.AGraph(directed=True)
A.node_attr["style"] = "filled"
A.add_edge("Rain", "Sprinkler", minlen=1, arrowsize=1, directed=True)
A.layout("dot")
A.graph_attr.update(size="4,4!")
A.draw("dgm.png")<jupyter_output><empty_output><jupyter_text>![](dgm.png)<jupyter_code>import tensorflow as tf
tf.Variable(2.)
def grass_wet_model(rain_prob, rain_to_sprinkler_probs):
    rain = yield tfp.distributions.JointDistributionCoroutine.Root(
        tfp.distributions.Bernoulli(probs=rain_prob, name="Rain")
    )

    sprinkler = yield tfp.distributions.Bernoulli(
        probs=rain_to_sprinkler_probs[rain], name="Sprinkler"
    )
theta_rain = tf.constant(0.2)
theta_sprinkler = tf.constant([0.8, 0.3])
model_joint_original = tfp.distributions.JointDistributionCoroutineAutoBatched(
    lambda: grass_wet_model(theta_rain, theta_sprinkler), name="Original"
)
model_joint_original
model_joint_original.sample(10)
dataset = model_joint_original.sample(500)
dataset
dataset.Sprinkler
theta_hat_rain = tfp.util.TransformedVariable(
    0.5, bijector=tfp.bijectors.SoftClip(0.0, 1.0), name="theta_hat_rain"
)

theta_hat_sprinkler = tfp.util.TransformedVariable(
    [0.6, 0.4], bijector=tfp.bijectors.SoftClip(0.0, 1.0), name="theta_hat_sprinkler"
)
model_fit = tfp.distributions.JointDistributionCoroutineAutoBatched(
    lambda: grass_wet_model(theta_hat_rain, theta_hat_sprinkler), name="Fit"
)

print(model_fit)
model_fit.trainable_variables
neg_ll = lambda: -tf.reduce_mean(model_fit.log_prob(dataset))
trace_fn = lambda traceable_quantities: {
    "loss": traceable_quantities.loss,
    "theta_hat_rain": theta_hat_rain,
    "theta_hat_sprinkler": theta_hat_sprinkler,
}
trace = tfp.math.minimize(
    loss_fn=neg_ll,
    optimizer=tf.optimizers.Adam(learning_rate=0.01),
    num_steps=100,
    trace_fn=trace_fn,
)
plt.plot(trace["theta_hat_rain"])
plt.plot(trace["loss"])
plt.plot(trace["theta_hat_sprinkler"])<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11496350364963503, 'fraction_numerical': 0.013503649635036497, 'mean_word_length': 6.613888888888889, 'pattern_counts': {'":': 3, '<': 10, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '643524', 'license': 'Apache-2.0', 'n_tokens_mistral': 1061, 'n_tokens_neox': 1008, 'n_words': 169, 'path': '_notebooks/2022-02-05-simple-dgm.ipynb', 'repo_name': 'nipunbatra/blog3', 'size': '2745'}