class_docstr: A policy that outputs actions based on observation and latent context.
  
  In PEARL, policies are conditioned on current state and a latent context
  (adaptation data) variable Z. This inference network estimates the
  posterior probability of z given past transitions. It uses context
  information stored in the encoder to infer the probabilistic value of z and
  samples from a policy conditioned on z.
  
  Args:
      latent_dim (int): Latent context variable dimension.
      context_encoder (garage.torch.embeddings.ContextEncoder): Recurrent or
          permutation-invariant context encoder.
      policy (garage.torch.policies.Policy): Policy used to train the
          network.
      use_information_bottleneck (bool): True if latent context is not
          deterministic; false otherwise.
      use_next_obs (bool): True if next observation is used in context
          for distinguishing tasks; false otherwise.
class_docstr_tok: ["A","policy","that","outputs","actions","based","on","observation","and","latent","context",".","In","PEARL",",","policies","are","conditioned","on","current","state","and","a","latent","context","(","adaptation","data",")","variable","Z",".","This","inference","network","estimates","the","posterior","probability","of","z","given","past","transitions",".","It","uses","context","information","stored","in","the","encoder","to","infer","the","probabilistic","value","of","z","and","samples","from","a","policy","conditioned","on","z.","Args",":","latent_dim","(","int",")",":","Latent","context","variable","dimension",".","context_encoder","(","garage.torch.embeddings.ContextEncoder",")",":","Recurrent","or","permutation-invariant","context","encoder",".","policy","(","garage.torch.policies.Policy",")",":","Policy","used","to","train","the","network",".","use_information_bottleneck","(","bool",")",":","True","if","latent","context","is","not","deterministic",";","false","otherwise",".","use_next_obs","(","bool",")",":","True","if","next","observation","is","used","in","context","for","distinguishing","tasks",";","false","otherwise","."]
docstr: Compute :math:`q(z \| c)` as a function of input context and sample new z.
  
  Args:
      context (torch.Tensor): Context values, with shape
          :math:`(X, N, C)`. X is the number of tasks. N is batch size. C
          is the combined size of observation, action, reward, and next
          observation if next observation is used in context. Otherwise,
          C is the combined size of observation, action, and reward.
docstr_tok: ["Compute",":","math",":","`","q","(","z","\\|","c",")","`","as","a","function","of","input","context","and","sample","new","z.","Args",":","context","(","torch.Tensor",")",":","Context","values",",","with","shape",":","math",":","`","(","X",",","N",",","C",")","`",".","X","is","the","number","of","tasks",".","N","is","batch","size",".","C","is","the","combined","size","of","observation",",","action",",","reward",",","and","next","observation","if","next","observation","is","used","in","context",".","Otherwise",",","C","is","the","combined","size","of","observation",",","action",",","and","reward","."]
code: def infer_posterior(self, context):
      r"""Compute :math:`q(z \| c)` as a function of input context and sample new z.
  
      Args:
          context (torch.Tensor): Context values, with shape
              :math:`(X, N, C)`. X is the number of tasks. N is batch size. C
              is the combined size of observation, action, reward, and next
              observation if next observation is used in context. Otherwise,
              C is the combined size of observation, action, and reward.
  
      """
      params = self._context_encoder.forward(context)
      params = params.view(
          context.size(0), -1, self._context_encoder.output_dim
      )
      # with probabilistic z, predict mean and variance of q(z | c)
      if self._use_information_bottleneck:
          mu = params[..., : self._latent_dim]
          sigma_squared = F.softplus(params[..., self._latent_dim :])
          z_params = [
              product_of_gaussians(m, s)
              for m, s in zip(
                  torch.unbind(mu), torch.unbind(sigma_squared)
              )
          ]
          self.z_means = torch.stack([p[0] for p in z_params])
          self.z_vars = torch.stack([p[1] for p in z_params])
      else:
          self.z_means = torch.mean(params, dim=1)
      self.sample_from_belief()
  
file_path: garage/src/garage/torch/policies/context_conditioned_policy.py
imports: import numpy
  import torch
  from torch import nn
  import torch.nn.functional
  from garage.torch import global_device, product_of_gaussians
label_desc: Use image node_id 5 for calling the ContextConditionedPolicy obj's underlying member method code with example usage: obj.infer_posterior(context) without return types