pyro-ppl/pyro

[Feature Request] 1) More details in `render_model` (esp. for `pyro.params`); 2) Returning dictionary from render_mode

Open

#3,023 opened on 2022年2月25日

GitHub で見る
 (5 comments) (2 reactions) (0 assignees)Python (8,211 stars) (981 forks)batch import
enhancementhelp wanted

説明

Hi, From the MLE-MAP tutorial, we have the following models

MLE model

def model_mle(data):
    f = pyro.param("latent_fairness", torch.tensor(0.5), 
                   constraint=constraints.unit_interval)
    with pyro.plate("data", data.size(0)):
        pyro.sample("obs", dist.Bernoulli(f), obs=data)

If we render this, we get something like the following image

MAP model

def original_model(data):
    f = pyro.sample("latent_fairness", dist.Beta(10.0, 10.0))
    with pyro.plate("data", data.size(0)):
        pyro.sample("obs", dist.Bernoulli(f), obs=data)

If we render this, we get something like the following image

Coin toss graphical model images from the popular Maths for ML book

This is from Figure 8.10

We'd expect our MLE model render to look like 8.10 b) and our MAP model to look like 8.10 c)

So, when we have latent_fairness as a parameter, it should perhaps just be written as latent_fairness and under the MAP model, it should be parameterised by the Beta distribution.

From the pyro render of the MLE model, it is not easily visible how observations are related to latent_fairness.

Feature Requests

So, I have two questions/requests

  1. Would it make sense to have pyro.params also show in renders. The difference in the renders between pyro.sample and pyro.parameter would be the associated distribution (and thus hyperparams) in pyro.sample
  2. Would it be possible to allow render to additionally return a dictionary when calling render_model? For example, once can then use that dictionary to create their own graphical models, for example using tikz-bayesnet. For example, the code below reproduces the Figure 8.10 from MML book shown above.3.

image

\documentclass[a4paper]{article}
\usepackage{caption}
\usepackage{subcaption}
\usepackage{tikz}
\usetikzlibrary{bayesnet}
\usepackage{booktabs}


\setlength{\tabcolsep}{12pt}
\begin{document}
	
	\begin{figure}[ht]
		\begin{center}
			\begin{tabular}{@{}cccc@{}}
				\toprule
				
				$x_N$ explicit & Plate Notation & Hyperparameters on $\mu$ & Factor\\  \midrule
				&                &            &              \\
				\begin{tikzpicture}
					
					
					\node[obs]                               (x1) {$x_1$};
					\node[const, right=0.5cm of x1]                               (dots) {$\cdots$};
					\node[obs, right=0.5cm of dots]                               (xn) {$x_N$};
					\node[latent, above=of dots] (mu) {$\mathbf{\mu}$};
					
					
					\edge {mu} {x1,dots,xn} ; %
					
				\end{tikzpicture}&
				\begin{tikzpicture}
					
					
					\node[obs]                               (xn) {$x_n$};
					\node[latent, above=of xn] (mu) {$\mathbf{\mu}$};
					
					\plate{}{(xn)}{$n = 1, \cdots, N$};
					
					
					\edge {mu} {xn} ; %
					
				\end{tikzpicture} &
				
				\begin{tikzpicture}
					
					
					\node[obs]                               (xn) {$x_n$};
					\node[latent, above=of xn] (mu) {$\mathbf{\mu}$};
					\node[const, right=0.5cm of mu] (beta) {$\mathbf{\beta}$};
					\node[const, left=0.5cm of mu] (alpha) {$\mathbf{\alpha}$};
					
					\plate{}{(xn)}{$n = 1, \cdots, N$};
					
					
					\edge {mu} {xn} ; %
					\edge {alpha,beta} {mu} ; %
					
					
				\end{tikzpicture}
			& 	
			\begin{tikzpicture}
				
				
				\node[obs]                               (xn) {$x_n$};
				\node[latent, above=of xn] (mu) {$\mathbf{\mu}$};
				\factor[above=of xn] {y-f} {left:${Ber}$} {} {} ; %
				\node[const, above=1 of mu, xshift=0.5cm] (beta) {$\mathbf{\beta}$};
				\node[const, above=1 of mu, xshift=-0.5cm] (alpha) {$\mathbf{\alpha}$};
				\factor[above=of mu] {mu-f} {left:${Beta}$} {} {} ; %
				\plate{}{(xn)}{$n = 1, \cdots, N$};
				

				
				\edge {mu} {xn} ; %
				\edge {alpha,beta} {mu-f} ; %
				\edge  {mu-f}{mu} ; %
				
				
			\end{tikzpicture}
			
				
			\end{tabular}
			
		\end{center}
		\caption{Graphical models for a repeated Bernoulli experiment.}
	\end{figure}

	
\end{document}

コントリビューターガイド