o
    i e%                     @   s  d 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 ddlmZ ddlm	Z	 dd	lm
Z
 dd
lmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddgZedgdG dd dejZG dd deZeeedddZdS )z)The Normal (Gaussian) distribution class.    N)constant_op)dtypes)ops)tensor_shape)	array_ops)	check_ops)math_ops)nn)
random_ops)distribution)kullback_leibler)special_math)deprecation)	tf_exportNormalNormalWithSoftplusScalezdistributions.Normal)v1c                       s   e Zd ZdZejdddd			 d6 fdd	Zed	d
 Ze	dd Z
e	dd Zdd Zdd Zdd Zdd Zd7ddZdd Zdd Zdd Zd d! Zd"d# Zd$d% Zd&d' Zd(d) Zd*d+ Zd,d- Zd.d/ Zd0d1 Zd2d3 Zd4d5 Z  ZS )8r   aK  The Normal distribution with location `loc` and `scale` parameters.

  #### Mathematical details

  The probability density function (pdf) is,

  ```none
  pdf(x; mu, sigma) = exp(-0.5 (x - mu)**2 / sigma**2) / Z
  Z = (2 pi sigma**2)**0.5
  ```

  where `loc = mu` is the mean, `scale = sigma` is the std. deviation, and, `Z`
  is the normalization constant.

  The Normal distribution is a member of the [location-scale family](
  https://en.wikipedia.org/wiki/Location-scale_family), i.e., it can be
  constructed as,

  ```none
  X ~ Normal(loc=0, scale=1)
  Y = loc + scale * X
  ```

  #### Examples

  Examples of initialization of one or a batch of distributions.

  ```python
  import tensorflow_probability as tfp
  tfd = tfp.distributions

  # Define a single scalar Normal distribution.
  dist = tfd.Normal(loc=0., scale=3.)

  # Evaluate the cdf at 1, returning a scalar.
  dist.cdf(1.)

  # Define a batch of two scalar valued Normals.
  # The first has mean 1 and standard deviation 11, the second 2 and 22.
  dist = tfd.Normal(loc=[1, 2.], scale=[11, 22.])

  # Evaluate the pdf of the first distribution on 0, and the second on 1.5,
  # returning a length two tensor.
  dist.prob([0, 1.5])

  # Get 3 samples, returning a 3 x 2 tensor.
  dist.sample([3])
  ```

  Arguments are broadcast when possible.

  ```python
  # Define a batch of two scalar valued Normals.
  # Both have mean 1, but different standard deviations.
  dist = tfd.Normal(loc=1., scale=[11, 22.])

  # Evaluate the pdf of both distributions on the same point, 3.0,
  # returning a length 2 tensor.
  dist.prob(3.0)
  ```

  
2019-01-01zThe TensorFlow Distributions library has moved to TensorFlow Probability (https://github.com/tensorflow/probability). You should update all references to use `tfp.distributions` instead of `tf.distributions`.T	warn_onceFc              	      s   t t }tj|||gd>}t|rt|gng ! tj|dd| _	tj|dd| _
t| j	| j
g W d   n1 s@w   Y  W d   n1 sOw   Y  tt| j| j
jtj|||| j	| j
g|d dS )aD  Construct Normal distributions with mean and stddev `loc` and `scale`.

    The parameters `loc` and `scale` must be shaped in a way that supports
    broadcasting (e.g. `loc + scale` is a valid operation).

    Args:
      loc: Floating point tensor; the means of the distribution(s).
      scale: Floating point tensor; the stddevs of the distribution(s).
        Must contain only positive values.
      validate_args: Python `bool`, default `False`. When `True` distribution
        parameters are checked for validity despite possibly degrading runtime
        performance. When `False` invalid inputs may silently render incorrect
        outputs.
      allow_nan_stats: Python `bool`, default `True`. When `True`,
        statistics (e.g., mean, mode, variance) use the value "`NaN`" to
        indicate the result is undefined. When `False`, an exception is raised
        if one or more of the statistic's batch members are undefined.
      name: Python `str` name prefixed to Ops created by this class.

    Raises:
      TypeError: if `loc` and `scale` have different `dtype`.
    valueslocnamescaleN)dtypereparameterization_typevalidate_argsallow_nan_stats
parametersgraph_parentsr   )dictlocalsr   
name_scopecontrol_dependenciesr   assert_positiver   identity_loc_scaleassert_same_float_dtypesuperr   __init__r   r   FULLY_REPARAMETERIZEDselfr   r   r   r   r   r    	__class__ Y/var/www/myenv/lib/python3.10/site-packages/tensorflow/python/ops/distributions/normal.pyr,   j   s,   
$


zNormal.__init__c                 C   s    t tdtj| tjdgd S )N)r   r   r      )r"   zipr   convert_to_tensorr   int32)sample_shaper2   r2   r3   _param_shapes   s   zNormal._param_shapesc                 C      | j S )z$Distribution parameter for the mean.)r(   r/   r2   r2   r3   r         z
Normal.locc                 C   r;   )z.Distribution parameter for standard deviation.)r)   r<   r2   r2   r3   r      r=   zNormal.scalec                 C   s   t t | jt | jS N)r   broadcast_dynamic_shapeshaper   r   r<   r2   r2   r3   _batch_shape_tensor   s   

zNormal._batch_shape_tensorc                 C   s   t | j | j S r>   )r   broadcast_static_shaper   	get_shaper   r<   r2   r2   r3   _batch_shape   s   zNormal._batch_shapec                 C   s   t jg tjdS )Nr4   )r   constantr   r8   r<   r2   r2   r3   _event_shape_tensor      zNormal._event_shape_tensorc                 C   s
   t g S r>   )r   TensorShaper<   r2   r2   r3   _event_shape   s   
zNormal._event_shapeNc                 C   s>   t |g|  gd}tj|dd| jj|d}|| j | j S )Nr   g        g      ?)r@   meanstddevr   seed)r   concatbatch_shape_tensorr
   random_normalr   r   r   )r/   nrL   r@   sampledr2   r2   r3   	_sample_n   s
   zNormal._sample_nc                 C   s   |  ||   S r>   )_log_unnormalized_prob_log_normalizationr/   xr2   r2   r3   	_log_prob      zNormal._log_probc                 C      t | |S r>   r   log_ndtr_zrU   r2   r2   r3   _log_cdf   rG   zNormal._log_cdfc                 C   rY   r>   r   ndtrr\   rU   r2   r2   r3   _cdf   rG   zNormal._cdfc                 C      t | | S r>   rZ   rU   r2   r2   r3   _log_survival_function   rX   zNormal._log_survival_functionc                 C   ra   r>   r^   rU   r2   r2   r3   _survival_function   rX   zNormal._survival_functionc                 C   s   dt | | S )Ng      )r   squarer\   rU   r2   r2   r3   rS      s   zNormal._log_unnormalized_probc                 C   s    dt dt j  t| j S N      ?g       @)mathlogpir   r   r<   r2   r2   r3   rT      s    zNormal._log_normalizationc                 C   s6   | j t| j }dtdtj tj  t| S re   )	r   r   	ones_liker   rg   rh   ri   er   )r/   r   r2   r2   r3   _entropy   s   $zNormal._entropyc                 C      | j t| j S r>   )r   r   rj   r   r<   r2   r2   r3   _mean   rX   zNormal._meanc                 C   s   |  t|S r>   )_inv_zr   ndtri)r/   pr2   r2   r3   	_quantile   rG   zNormal._quantilec                 C   rm   r>   )r   r   rj   r   r<   r2   r2   r3   _stddev   rX   zNormal._stddevc                 C   s   |   S r>   )rn   r<   r2   r2   r3   _mode   s   zNormal._modec                 C   sD   t jd|gd || j | j W  d   S 1 sw   Y  dS )z'Standardize input `x` to a unit normal.standardizer   N)r   r$   r   r   rU   r2   r2   r3   r\         $z	Normal._zc                 C   sD   t jd|gd || j | j W  d   S 1 sw   Y  dS )z4Reconstruct input `x` from a its normalized version.reconstructr   N)r   r$   r   r   )r/   zr2   r2   r3   ro      rv   zNormal._inv_z)FTr   r>   ) __name__
__module____qualname____doc__r   
deprecatedr,   staticmethodr:   propertyr   r   rA   rD   rF   rI   rR   rW   r]   r`   rb   rc   rS   rT   rl   rn   rr   rs   rt   r\   ro   __classcell__r2   r2   r0   r3   r   )   sH    ?,



c                       s8   e Zd ZdZejdddd			 d	 fdd	Z  ZS )
r   z(Normal with softplus applied to `scale`.r   z4Use `tfd.Normal(loc, tf.nn.softplus(scale)) instead.Tr   Fc                    sh   t t }tj||gd}tt| j|tj|dd|||d W d    n1 s*w   Y  || _	d S )Nr   softplus_scaler   )r   r   r   r   r   )
r"   r#   r   r$   r+   r   r,   r	   softplus_parametersr.   r0   r2   r3   r,      s   


z NormalWithSoftplusScale.__init__)FTr   )ry   rz   r{   r|   r   r}   r,   r   r2   r2   r0   r3   r      s    c           	      C   s   t |d| j|jgG tjd| jd}tjd| jd}tjd| jd}t| j}t|j}|| }t	| j|j||  ||| t
|   W  d   S 1 sUw   Y  dS )aD  Calculate the batched KL divergence KL(n_a || n_b) with n_a and n_b Normal.

  Args:
    n_a: instance of a Normal distribution object.
    n_b: instance of a Normal distribution object.
    name: (optional) Name to use for created operations.
      default is "kl_normal_normal".

  Returns:
    Batchwise KL(n_a || n_b)
  kl_normal_normal   r4   r5   rf   N)r   r$   r   r   rE   r   r   rd   r   squared_differencerh   )	n_an_br   onetwohalfs_a_squareds_b_squaredratior2   r2   r3   _kl_normal_normal  s   $r   r>   )r|   rg   tensorflow.python.frameworkr   r   r   r   tensorflow.python.opsr   r   r   r	   r
   #tensorflow.python.ops.distributionsr   r   r   tensorflow.python.utilr    tensorflow.python.util.tf_exportr   __all__Distributionr   r   
RegisterKLr   r2   r2   r2   r3   <module>   s2   
 L
