
    Ph"                     t    d dl mZ d dlZd dlmZ d dlmZ d dlmZ d dl	m
Z
mZ d dlmZ dgZ G d	 de      Zy)
    )DictN)constraints)Distribution)Independent)ComposeTransform	Transform)_sum_rightmostTransformedDistributionc                       e Zd ZU dZi Zeeej                  f   e	d<   d fd	Z
d fd	Z ej                  d      d        Zed        Z ej"                         fd	Z ej"                         fd
Zd Zd Zd Zd Z xZS )r
   a  
    Extension of the Distribution class, which applies a sequence of Transforms
    to a base distribution.  Let f be the composition of transforms applied::

        X ~ BaseDistribution
        Y = f(X) ~ TransformedDistribution(BaseDistribution, f)
        log p(Y) = log p(X) + log |det (dX/dY)|

    Note that the ``.event_shape`` of a :class:`TransformedDistribution` is the
    maximum shape of its base distribution and its transforms, since transforms
    can introduce correlations among events.

    An example for the usage of :class:`TransformedDistribution` would be::

        # Building a Logistic Distribution
        # X ~ Uniform(0, 1)
        # f = a + b * logit(X)
        # Y ~ f(X) ~ Logistic(a, b)
        base_distribution = Uniform(0, 1)
        transforms = [SigmoidTransform().inv, AffineTransform(loc=a, scale=b)]
        logistic = TransformedDistribution(base_distribution, transforms)

    For more examples, please look at the implementations of
    :class:`~torch.distributions.gumbel.Gumbel`,
    :class:`~torch.distributions.half_cauchy.HalfCauchy`,
    :class:`~torch.distributions.half_normal.HalfNormal`,
    :class:`~torch.distributions.log_normal.LogNormal`,
    :class:`~torch.distributions.pareto.Pareto`,
    :class:`~torch.distributions.weibull.Weibull`,
    :class:`~torch.distributions.relaxed_bernoulli.RelaxedBernoulli` and
    :class:`~torch.distributions.relaxed_categorical.RelaxedOneHotCategorical`
    arg_constraintsc                    t        |t              r	|g| _        nCt        |t              r%t	        d |D              st        d      || _        nt        d|       |j                  |j                  z   }t        |j                        }t        | j                        }t        |      |j                  j                  k  r/t        dj                  |j                  j                  |            |j                  |      }|j                  |      }||k7  r"|d t        |      |z
   }	|j                  |	      }|j                  j                  |z
  }
|
dkD  rt!        ||
      }|| _        |j$                  j                  |j                  j                  z
  }t'        |j$                  j                  ||z         }t        |      |k\  sJ t        |      |z
  }|d | }||d  }t(        | U  |||       y )Nc              3   <   K   | ]  }t        |t                y wN)
isinstancer   ).0ts     wC:\Users\daisl\Desktop\realtime-object-detection\venv\Lib\site-packages\torch/distributions/transformed_distribution.py	<genexpr>z3TransformedDistribution.__init__.<locals>.<genexpr>6   s     DAz!Y/s   z6transforms must be a Transform or a list of Transformsz0transforms must be a Transform or list, but was zHbase_distribution needs to have shape with size at least {}, but got {}.r   validate_args)r   r   
transformslistall
ValueErrorbatch_shapeevent_shapelenr   domain	event_dimformatforward_shapeinverse_shapeexpandr   	base_distcodomainmaxsuper__init__)selfbase_distributionr   r   
base_shapebase_event_dim	transformr!   expanded_base_shapebase_batch_shapereinterpreted_batch_ndimstransform_change_in_event_dimr   cutr   r   	__class__s                   r   r(   z TransformedDistribution.__init__0   s   j),DO 
D)DDD L  )DOB:,O 
 '225F5R5RR
.::;$T__5	z?Y--777Zaa$$..
 
 "//
;'55mD,,2;#)*^;  !2 8 89I J$-$4$4$>$>$O!$q( +!#<! + ((9+;+;+E+EE 	& ((::
	 =!Y...- 9,#DS)#CD)kO    c                    | j                  t        |      }t        j                  |      }|| j                  z   }t        | j                        D ]  }|j                  |      } |d t        |      t        | j                  j                        z
   }| j                  j                  |      |_	        | j                  |_        t        t        |3  || j                  d       | j                  |_        |S )NFr   )_get_checked_instancer
   torchSizer   reversedr   r"   r   r$   r#   r'   r(   _validate_args)r)   r   	_instancenewshaper   r/   r3   s          r   r#   zTransformedDistribution.expandf   s    (()@)Ljj-d...$//*AOOE*E + !O3u:DNN4N4N0O#OP--.>?%s4)) 	5 	
 "00
r4   F)is_discretec                 ,   | j                   s| j                  j                  S | j                   d   j                  }t	        | j
                        |j                  kD  r6t        j                  |t	        | j
                        |j                  z
        }|S )N)	r   r$   supportr%   r   r   r   r   independent)r)   rA   s     r   rA   zTransformedDistribution.supportu   sx    >>)))//"%..t 7#4#44!--T--.1B1BBG r4   c                 .    | j                   j                  S r   )r$   has_rsample)r)   s    r   rD   z#TransformedDistribution.has_rsample   s    ~~)))r4   c                     t        j                         5  | j                  j                  |      }| j                  D ]
  } ||      } |cddd       S # 1 sw Y   yxY w)a  
        Generates a sample_shape shaped sample or sample_shape shaped batch of
        samples if the distribution parameters are batched. Samples first from
        base distribution and applies `transform()` for every transform in the
        list.
        N)r7   no_gradr$   sampler   r)   sample_shapexr-   s       r   rG   zTransformedDistribution.sample   sE     ]]_%%l3A!__	aL -	 __s   6AAc                 n    | j                   j                  |      }| j                  D ]
  } ||      } |S )a$  
        Generates a sample_shape shaped reparameterized sample or sample_shape
        shaped batch of reparameterized samples if the distribution parameters
        are batched. Samples first from base distribution and applies
        `transform()` for every transform in the list.
        )r$   rsampler   rH   s       r   rL   zTransformedDistribution.rsample   s4     NN""<0I!A )r4   c                 (   | j                   r| j                  |       t        | j                        }d}|}t	        | j
                        D ]{  }|j                  |      }||j                  j                  |j                  j                  z
  z  }|t        |j                  ||      ||j                  j                  z
        z
  }|}} |t        | j                  j                  |      |t        | j                  j                        z
        z   }|S )z
        Scores the sample by inverting the transform(s) and computing the score
        using the score of the base distribution and the log abs det jacobian.
        g        )r:   _validate_sampler   r   r9   r   invr   r   r%   r	   log_abs_det_jacobianr$   log_prob)r)   valuer   rQ   yr-   rJ   s          r   rQ   z TransformedDistribution.log_prob   s    
 !!%((()	!$//2Ia A))33i6H6H6R6RRRI...q!4I,,666# H A 3 nNN##A&	C8R8R4S(S
 
 r4   c                     d}| j                   D ]  }||j                  z  } t        |t              r|dk(  r|S ||dz
  z  dz   S )zu
        This conditionally flips ``value -> 1-value`` to ensure :meth:`cdf` is
        monotone increasing.
           g      ?)r   signr   int)r)   rR   rV   r-   s       r   _monotonize_cdfz'TransformedDistribution._monotonize_cdf   sN    
 I)..(D )dC TQYLus{#c))r4   c                     | j                   ddd   D ]  }|j                  |      } | j                  r| j                  j	                  |       | j                  j                  |      }| j                  |      }|S )z
        Computes the cumulative distribution function by inverting the
        transform(s) and computing the score of the base distribution.
        Nr@   )r   rO   r:   r$   rN   cdfrX   r)   rR   r-   s      r   rZ   zTransformedDistribution.cdf   sm    
 2.IMM%(E /NN++E2""5)$$U+r4   c                     | j                  |      }| j                  j                  |      }| j                  D ]
  } ||      } |S )z
        Computes the inverse cumulative distribution function using
        transform(s) and computing the score of the base distribution.
        )rX   r$   icdfr   r[   s      r   r]   zTransformedDistribution.icdf   sE    
 $$U+##E*Ie$E )r4   r   )__name__
__module____qualname____doc__r   r   strr   
Constraint__annotations__r(   r#   dependent_propertyrA   propertyrD   r7   r8   rG   rL   rQ   rX   rZ   r]   __classcell__)r3   s   @r   r
   r
      s    @ :<OT#{5556;4Pl $[##6 7 * * #-%**,  $.5::< 
0
*	r4   )typingr   r7   torch.distributionsr    torch.distributions.distributionr   torch.distributions.independentr   torch.distributions.transformsr   r   torch.distributions.utilsr	   __all__r
    r4   r   <module>rp      s0      + 9 7 F 4$
%Jl Jr4   