code: def _posterior_loss(
      model,
      guide,
      observation_labels,
      target_labels,
      analytic_entropy=False,
  ):
      """Posterior loss: to evaluate directly use `posterior_eig` setting `num_steps=0`, `eig=False`."""
  
      def loss_fn(design, num_particles, evaluation=False, **kwargs):
          expanded_design = lexpand(design, num_particles)
  
          # Sample from p(y, theta | d)
          trace = poutine.trace(model).get_trace(expanded_design)
          y_dict = {
              l: trace.nodes[l]["value"] for l in observation_labels
          }
          theta_dict = {
              l: trace.nodes[l]["value"] for l in target_labels
          }
  
          # Run through q(theta | y, d)
          conditional_guide = pyro.condition(guide, data=theta_dict)
          cond_trace = poutine.trace(conditional_guide).get_trace(
              y_dict, expanded_design, observation_labels, target_labels
          )
          cond_trace.compute_log_prob()
          if evaluation and analytic_entropy:
              loss = (
                  mean_field_entropy(
                      guide,
                      [
                          y_dict,
                          expanded_design,
                          observation_labels,
                          target_labels,
                      ],
                      whitelist=target_labels,
                  ).sum(0)
                  / num_particles
              )
              agg_loss = loss.sum()
          else:
              terms = -sum(
                  cond_trace.nodes[l]["log_prob"] for l in target_labels
              )
              agg_loss, loss = _safe_mean_terms(terms)
  
          return agg_loss, loss
  
      return loss_fn
  
imports: import math
  import warnings
  import torch
  import pyro
  from pyro import poutine
  from pyro.contrib.oed.search import Search
  from pyro.contrib.util import lexpand
  from pyro.infer import SVI, EmpiricalMarginal, Importance
  from pyro.infer.autoguide.utils import mean_field_entropy
  from pyro.util import torch_isinf, torch_isnan
label_desc: Use image node_id 17 for calling a global function with example usage: _posterior_loss(model, guide, observation_labels, target_labels, analytic_entropy) and returns: loss_fn, agg_loss,  loss