code: def run_vi_on_target(
      target: gym.targets.Model,
      init_x: jnp.ndarray,
      num_steps: int,
      learning_rate: float,
      seed: jax.Array,
  ):
      """Run VI on a target.
  
      Args:
        target: Target model.
        init_x: Initial constrained chain state.
        num_steps: Number of steps to take.
        learning_rate: Learning rate for adam.
        seed: Random seed.
  
      Returns:
         A tuple of final and traced results.
      """
      init_z = target.default_event_space_bijector.inverse(init_x)
  
      def loss_fn(loc, isp_scale, seed):
          q = tfd.Independent(
              tfd.Normal(loc, jax.nn.softplus(isp_scale)), 1
          )
          q_sg = tfd.Independent(
              tfd.Normal(
                  jax.lax.stop_gradient(loc),
                  jax.nn.softplus(jax.lax.stop_gradient(isp_scale)),
              ),
              1,
          )
  
          z = q.sample(seed=seed)
  
          x = target.default_event_space_bijector(z)
          tlp = target.unnormalized_log_prob(x)
          elbo = tlp - q_sg.log_prob(z)
  
          return -elbo, ()
  
      def kernel(opt_state, seed):
          seed, vi_seed = jax.random.split(seed)
          opt_state, opt_extra = fun_mc.adam_step(
              opt_state,
              functools.partial(loss_fn, seed=vi_seed),
              learning_rate,
          )
  
          traced = {
              "loc": opt_state.state[0],
              "isp_scale": opt_state.state[1],
              "loss": opt_extra.loss,
          }
  
          return (opt_state, seed), traced
  
      opt_state = fun_mc.adam_init(
          (
              init_z,
              tfp.math.softplus_inverse(jnp.full_like(init_z, 1e-3)),
          )
      )
  
      _, trace = fun_mc.trace((opt_state, seed), kernel, num_steps)
  
      final = {
          "final_x": target.default_event_space_bijector.forward(
              trace["loc"][-1]
          ),
      }
      return trace, final
  
imports: import functools
  import os
  from typing import Any, Callable, Dict, Optional, Mapping, NamedTuple, Tuple, Union
  from etils import epath
  import gin
  import immutabledict
  import jax
  import jax.numpy
  import numpy
  from discussion.adaptive_malt import utils
  from fun_mc import using_jax
  from inference_gym import using_jax
  import tensorflow_probability.substrates.jax
label_desc: Use image node_id 21 for calling a global function with example usage: run_vi_on_target(target, init_x, num_steps, learning_rate, seed) and returns: trace,  final, unknown,  , opt_state,  seed,  traced