class_docstr: Base class for models in the Inference Gym.
  
  A model, at the minimum, describes its support and allows evaluating its
  un-normalized log density via the `unnormalized_log_prob` method.
  
  Given a Bayesian model conditioned on evidence, you can access the associated
  un-normalized density via the `unnormalized_log_prob` method.
  
  The dtype, shape, and constraints over the support are returned by the
  `dtype`, `event_shape`, and `default_event_space_bijector` properties. Note
  that `x` could be structured, in which case `dtype` and `shape` will be
  structured as well (parallel to the structure of `x`).
  `default_event_space_bijector` need not be structured, but could operate on
  the structured `x`. A generic way of constructing a random number that is
  within the event space of this model is to do:
  
  ```python
  model = LogisticRegression(...)
  unconstrained_values = tf.nest.map_structure(
      lambda d, s: tf.random.normal(s, dtype=d),
      model.dtype,
      model.event_shape,
    )
  constrained_values = tf.nest.map_structure_up_to(
      model.default_event_space_bijector,
      lambda b, v: b(v),
      model.default_event_space_bijector,
      unconstrained_values,
    )
  ```
  
  A model has two names. First, the `name` property is used for various name
  scopes inside the implementation of the model. Second, a pretty name which is
  meant to be suitable for a table inside a publication, accessed via the
  `__str__` method.
  
  Models come with associated sample transformations, which describe useful ways
  of looking at the samples from the posterior distribution. Each transformation
  optionally comes equipped with various ground truth values (computed
  analytically or via Monte Carlo averages). You can apply the transformations
  to samples from the model like so:
  
  ```python
  model = LogisticRegression(...)
  for name, sample_transformation in model.sample_transformations.items():
    transformed_samples = sample_transformation(samples)
    if sample_transformation.ground_truth_mean is not None:
      square_diff = tf.nest.map_structure(
          lambda gtm, sm: (gtm - tf.reduce_mean(sm, axis=0))**2,
          sample_transformation.ground_truth_mean,
          transformed_samples,
      )
  ```
  
  #### Examples
  
  A simple 2-variable model:
  
  ```python
  class SimpleModel(gym.targets.Model):
  
    def __init__(self):
      super(SimpleModel, self).__init__(
          default_event_space_bijector=tfb.Exp(),
          event_shape=[],
          dtype=tf.float32,
          name='simple_model',
          pretty_name='SimpleModel',
          sample_transformations=dict(
              identity=gym.targets.Model.SampleTransformation(
                  fn=lambda x: x,
                  pretty_name='Identity',
              ),),
      )
  
    def _unnormalized_log_prob(self, value):
      return 1. + value - tf.math.exp(value)
  ```
  
  We don't specify the ground truth values for the `identity` sample
  transformation for demonstration purposes, which you'd normally only do if
  they are not known analytically. See `GermanCreditNumericLogisticRegression`
  Bayesian model for an example of how to incorporate Monte-Carlo derived values
  for ground truth into a sample transformation.
class_docstr_tok: ["Base","class","for","models","in","the","Inference","Gym",".","A","model",",","at","the","minimum",",","describes","its","support","and","allows","evaluating","its","un-normalized","log","density","via","the","`","unnormalized_log_prob","`","method",".","Given","a","Bayesian","model","conditioned","on","evidence",",","you","can","access","the","associated","un-normalized","density","via","the","`","unnormalized_log_prob","`","method",".","The","dtype",",","shape",",","and","constraints","over","the","support","are","returned","by","the","`","dtype","`",",","`","event_shape","`",",","and","`","default_event_space_bijector","`","properties",".","Note","that","`","x","`","could","be","structured",",","in","which","case","`","dtype","`","and","`","shape","`","will","be","structured","as","well","(","parallel","to","the","structure","of","`","x","`",")",".","`","default_event_space_bijector","`","need","not","be","structured",",","but","could","operate","on","the","structured","`","x","`",".","A","generic","way","of","constructing","a","random","number","that","is","within","the","event","space","of","this","model","is","to","do",":","``","`","python","model","=","LogisticRegression","(","...",")","unconstrained_values","=","tf.nest.map_structure","(","lambda","d",",","s",":","tf.random.normal","(","s",",","dtype=d",")",",","model.dtype",",","model.event_shape",",",")","constrained_values","=","tf.nest.map_structure_up_to","(","model.default_event_space_bijector",",","lambda","b",",","v",":","b","(","v",")",",","model.default_event_space_bijector",",","unconstrained_values",",",")","``","`","A","model","has","two","names",".","First",",","the","`","name","`","property","is","used","for","various","name","scopes","inside","the","implementation","of","the","model",".","Second",",","a","pretty","name","which","is","meant","to","be","suitable","for","a","table","inside","a","publication",",","accessed","via","the","`","__str__","`","method",".","Models","come","with","associated","sample","transformations",",","which","describe","useful","ways","of","looking","at","the","samples","from","the","posterior","distribution",".","Each","transformation","optionally","comes","equipped","with","various","ground","truth","values","(","computed","analytically","or","via","Monte","Carlo","averages",")",".","You","can","apply","the","transformations","to","samples","from","the","model","like","so",":","``","`","python","model","=","LogisticRegression","(","...",")","for","name",",","sample_transformation","in","model.sample_transformations.items","(",")",":","transformed_samples","=","sample_transformation","(","samples",")","if","sample_transformation.ground_truth_mean","is","not","None",":","square_diff","=","tf.nest.map_structure","(","lambda","gtm",",","sm",":","(","gtm","-","tf.reduce_mean","(","sm",",","axis=0",")",")","*","*","2",",","sample_transformation.ground_truth_mean",",","transformed_samples",",",")","``","`","#","#","#","#","Examples","A","simple","2-variable","model",":","``","`","python","class","SimpleModel","(","gym.targets.Model",")",":","def","__init__","(","self",")",":","super","(","SimpleModel",",","self",")",".__init__","(","default_event_space_bijector=tfb.Exp","(",")",",","event_shape=","[","]",",","dtype=tf.float32",",","name='simple_model","'",",","pretty_name='SimpleModel","'",",","sample_transformations=dict","(","identity=gym.targets.Model.SampleTransformation","(","fn=lambda","x",":","x",",","pretty_name='Identity","'",",",")",",",")",",",")","def","_unnormalized_log_prob","(","self",",","value",")",":","return","1",".","+","value","-","tf.math.exp","(","value",")","``","`","We","do","n't","specify","the","ground","truth","values","for","the","`","identity","`","sample","transformation","for","demonstration","purposes",",","which","you","'d","normally","only","do","if","they","are","not","known","analytically",".","See","`","GermanCreditNumericLogisticRegression","`","Bayesian","model","for","an","example","of","how","to","incorporate","Monte-Carlo","derived","values","for","ground","truth","into","a","sample","transformation","."]
docstr: The `DType` of `Tensor`s handled by this model.
docstr_tok: ["The","`","DType","`","of","`","Tensor","`","s","handled","by","this","model","."]
code: def dtype(self):
      """The `DType` of `Tensor`s handled by this model."""
      return self._dtype
  
file_path: probability/spinoffs/inference_gym/inference_gym/targets/model.py
imports: import collections
  import tensorflow.compat.v2
  from inference_gym.internal import ground_truth_encoding
  from tensorflow.python.util import nest
label_desc: Use image node_id 6 for calling the Model obj's underlying member method code with example usage: obj.dtype() and returns: self