class_docstr: An implementation of the affine coupling layer of RealNVP (Dinh et al., 2017)
  that conditions on an additional context variable and uses the bijective
  transform,
  
      :math:`\mathbf{y}_{1:d} = \mathbf{x}_{1:d}`
      :math:`\mathbf{y}_{(d+1):D} = \mu + \sigma\odot\mathbf{x}_{(d+1):D}`
  
  where :math:`\mathbf{x}` are the inputs, :math:`\mathbf{y}` are the outputs,
  e.g. :math:`\mathbf{x}_{1:d}` represents the first :math:`d` elements of the
  inputs, and :math:`\mu,\sigma` are shift and translation parameters calculated
  as the output of a function input :math:`\mathbf{x}_{1:d}` and a context
  variable :math:`\mathbf{z}\in\mathbb{R}^M`.
  
  That is, the first :math:`d` components remain unchanged, and the subsequent
  :math:`D-d` are shifted and translated by a function of the previous components.
  
  Together with :class:`~pyro.distributions.ConditionalTransformedDistribution`
  this provides a way to create richer variational approximations.
  
  Example usage:
  
  >>> from pyro.nn import ConditionalDenseNN
  >>> input_dim = 10
  >>> split_dim = 6
  >>> context_dim = 4
  >>> batch_size = 3
  >>> base_dist = dist.Normal(torch.zeros(input_dim), torch.ones(input_dim))
  >>> param_dims = [input_dim-split_dim, input_dim-split_dim]
  >>> hypernet = ConditionalDenseNN(split_dim, context_dim, [10*input_dim],
  ... param_dims)
  >>> transform = ConditionalAffineCoupling(split_dim, hypernet)
  >>> pyro.module("my_transform", transform)  # doctest: +SKIP
  >>> z = torch.rand(batch_size, context_dim)
  >>> flow_dist = dist.ConditionalTransformedDistribution(base_dist,
  ... [transform]).condition(z)
  >>> flow_dist.sample(sample_shape=torch.Size([batch_size]))  # doctest: +SKIP
  
  The inverse of the Bijector is required when, e.g., scoring the log density of a
  sample with :class:`~pyro.distributions.ConditionalTransformedDistribution`.
  This implementation caches the inverse of the Bijector when its forward
  operation is called, e.g., when sampling from
  :class:`~pyro.distributions.ConditionalTransformedDistribution`. However, if the
  cached value isn't available, either because it was overwritten during sampling
  a new value or an arbitary value is being scored, it will calculate it manually.
  
  This is an operation that scales as O(1), i.e. constant in the input dimension.
  So in general, it is cheap to sample *and* score (an arbitrary value) from
  :class:`~pyro.distributions.transforms.ConditionalAffineCoupling`.
  
  :param split_dim: Zero-indexed dimension :math:`d` upon which to perform input/
      output split for transformation.
  :type split_dim: int
  :param hypernet: A neural network whose forward call returns a real-valued mean
      and logit-scale as a tuple. The input should have final dimension split_dim
      and the output final dimension input_dim-split_dim for each member of the
      tuple. The network also inputs a context variable as a keyword argument in
      order to condition the output upon it.
  :type hypernet: callable
  :param log_scale_min_clip: The minimum value for clipping the log(scale) from
      the NN
  :type log_scale_min_clip: float
  :param log_scale_max_clip: The maximum value for clipping the log(scale) from
      the NN
  :type log_scale_max_clip: float
  
  References:
  
  Laurent Dinh, Jascha Sohl-Dickstein, and Samy Bengio. Density estimation using
  Real NVP. ICLR 2017.
class_docstr_tok: ["An","implementation","of","the","affine","coupling","layer","of","RealNVP","(","Dinh","et","al.",",","2017",")","that","conditions","on","an","additional","context","variable","and","uses","the","bijective","transform",",",":","math",":","`","\\mathbf","{","y","}","_","{","1",":","d","}","=","\\mathbf","{","x","}","_","{","1",":","d","}","`",":","math",":","`","\\mathbf","{","y","}","_","{","(","d+1",")",":","D","}","=","\\mu","+","\\sigma\\odot\\mathbf","{","x","}","_","{","(","d+1",")",":","D","}","`","where",":","math",":","`","\\mathbf","{","x","}","`","are","the","inputs",",",":","math",":","`","\\mathbf","{","y","}","`","are","the","outputs",",","e.g",".",":","math",":","`","\\mathbf","{","x","}","_","{","1",":","d","}","`","represents","the","first",":","math",":","`","d","`","elements","of","the","inputs",",","and",":","math",":","`","\\mu",",","\\sigma","`","are","shift","and","translation","parameters","calculated","as","the","output","of","a","function","input",":","math",":","`","\\mathbf","{","x","}","_","{","1",":","d","}","`","and","a","context","variable",":","math",":","`","\\mathbf","{","z","}","\\in\\mathbb","{","R","}","^M","`",".","That","is",",","the","first",":","math",":","`","d","`","components","remain","unchanged",",","and","the","subsequent",":","math",":","`","D-d","`","are","shifted","and","translated","by","a","function","of","the","previous","components",".","Together","with",":","class",":","`","~pyro.distributions.ConditionalTransformedDistribution","`","this","provides","a","way","to","create","richer","variational","approximations",".","Example","usage",":",">",">",">","from","pyro.nn","import","ConditionalDenseNN",">",">",">","input_dim","=","10",">",">",">","split_dim","=","6",">",">",">","context_dim","=","4",">",">",">","batch_size","=","3",">",">",">","base_dist","=","dist.Normal","(","torch.zeros","(","input_dim",")",",","torch.ones","(","input_dim",")",")",">",">",">","param_dims","=","[","input_dim-split_dim",",","input_dim-split_dim","]",">",">",">","hypernet","=","ConditionalDenseNN","(","split_dim",",","context_dim",",","[","10","*","input_dim","]",",","...","param_dims",")",">",">",">","transform","=","ConditionalAffineCoupling","(","split_dim",",","hypernet",")",">",">",">","pyro.module","(","``","my_transform","''",",","transform",")","#","doctest",":","+SKIP",">",">",">","z","=","torch.rand","(","batch_size",",","context_dim",")",">",">",">","flow_dist","=","dist.ConditionalTransformedDistribution","(","base_dist",",","...","[","transform","]",")",".condition","(","z",")",">",">",">","flow_dist.sample","(","sample_shape=torch.Size","(","[","batch_size","]",")",")","#","doctest",":","+SKIP","The","inverse","of","the","Bijector","is","required","when",",","e.g.",",","scoring","the","log","density","of","a","sample","with",":","class",":","`","~pyro.distributions.ConditionalTransformedDistribution","`",".","This","implementation","caches","the","inverse","of","the","Bijector","when","its","forward","operation","is","called",",","e.g.",",","when","sampling","from",":","class",":","`","~pyro.distributions.ConditionalTransformedDistribution","`",".","However",",","if","the","cached","value","is","n't","available",",","either","because","it","was","overwritten","during","sampling","a","new","value","or","an","arbitary","value","is","being","scored",",","it","will","calculate","it","manually",".","This","is","an","operation","that","scales","as","O","(","1",")",",","i.e",".","constant","in","the","input","dimension",".","So","in","general",",","it","is","cheap","to","sample","*","and","*","score","(","an","arbitrary","value",")","from",":","class",":","`","~pyro.distributions.transforms.ConditionalAffineCoupling","`",".",":","param","split_dim",":","Zero-indexed","dimension",":","math",":","`","d","`","upon","which","to","perform","input\/","output","split","for","transformation",".",":","type","split_dim",":","int",":","param","hypernet",":","A","neural","network","whose","forward","call","returns","a","real-valued","mean","and","logit-scale","as","a","tuple",".","The","input","should","have","final","dimension","split_dim","and","the","output","final","dimension","input_dim-split_dim","for","each","member","of","the","tuple",".","The","network","also","inputs","a","context","variable","as","a","keyword","argument","in","order","to","condition","the","output","upon","it",".",":","type","hypernet",":","callable",":","param","log_scale_min_clip",":","The","minimum","value","for","clipping","the","log","(","scale",")","from","the","NN",":","type","log_scale_min_clip",":","float",":","param","log_scale_max_clip",":","The","maximum","value","for","clipping","the","log","(","scale",")","from","the","NN",":","type","log_scale_max_clip",":","float","References",":","Laurent","Dinh",",","Jascha","Sohl-Dickstein",",","and","Samy","Bengio",".","Density","estimation","using","Real","NVP",".","ICLR","2017","."]
code: def __init__(self, split_dim, hypernet, **kwargs):
      super().__init__()
      self.split_dim = split_dim
      self.nn = hypernet
      self.kwargs = kwargs
  
imports: import operator
  from functools import partial, reduce
  import torch
  from torch.distributions.utils import _sum_rightmost
  from pyro.nn import ConditionalDenseNN, DenseNN
  from ..None import constraints
  from ..conditional import ConditionalTransformModule
  from ..torch_transform import TransformModule
  from ..transforms.utils import clamp_preserve_gradients
  from ..util import copy_docs_from
label_desc: Use image node_id 1 to create a new ConditionalAffineCoupling object from inherited base classes: ConditionalTransformModule with example: obj = ConditionalAffineCoupling(split_dim, hypernet)