o
    ?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Z	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)dtypeZreparameterization_typevalidate_argsallow_nan_stats
parametersZgraph_parentsr   )dictlocalsr   
name_scopeZcontrol_dependenciesr   Zassert_positiver   identity_loc_scaleZassert_same_float_dtypesuperr   __init__r   r   ZFULLY_REPARAMETERIZEDselfr   r   r   r   r   r   	__class__ k/home/www/facesmatcher.com/pyenv/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   Zconvert_to_tensorr   int32)Zsample_shaper+   r+   r,   _param_shapes   s   zNormal._param_shapesc                 C      | j S )z$Distribution parameter for the mean.)r#   r(   r+   r+   r,   r         z
Normal.locc                 C   r2   )z.Distribution parameter for standard deviation.)r$   r3   r+   r+   r,   r      r4   zNormal.scalec                 C   s   t t | jt | jS N)r   Zbroadcast_dynamic_shapeshaper   r   r3   r+   r+   r,   _batch_shape_tensor   s   

zNormal._batch_shape_tensorc                 C   s   t | j | j S r5   )r   Zbroadcast_static_shaper   	get_shaper   r3   r+   r+   r,   _batch_shape   s   zNormal._batch_shapec                 C   s   t jg tjdS )Nr-   )r   constantr   r0   r3   r+   r+   r,   _event_shape_tensor      zNormal._event_shape_tensorc                 C   s
   t g S r5   )r   ZTensorShaper3   r+   r+   r,   _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      ?)r6   meanZstddevr   seed)r   concatZbatch_shape_tensorr
   Zrandom_normalr   r   r   )r(   nr?   r6   Zsampledr+   r+   r,   	_sample_n   s
   zNormal._sample_nc                 C   s   |  ||   S r5   )_log_unnormalized_prob_log_normalizationr(   xr+   r+   r,   	_log_prob      zNormal._log_probc                 C      t | |S r5   r   Zlog_ndtr_zrE   r+   r+   r,   _log_cdf   r<   zNormal._log_cdfc                 C   rI   r5   r   ZndtrrK   rE   r+   r+   r,   _cdf   r<   zNormal._cdfc                 C      t | | S r5   rJ   rE   r+   r+   r,   _log_survival_function   rH   zNormal._log_survival_functionc                 C   rO   r5   rM   rE   r+   r+   r,   _survival_function   rH   zNormal._survival_functionc                 C   s   dt | | S )Ng      )r   squarerK   rE   r+   r+   r,   rC      s   zNormal._log_unnormalized_probc                 C   s    dt dt j  t| j S N      ?g       @)mathlogpir   r   r3   r+   r+   r,   rD      s    zNormal._log_normalizationc                 C   s6   | j t| j }dtdtj tj  t| S rS   )	r   r   	ones_liker   rU   rV   rW   er   )r(   r   r+   r+   r,   _entropy   s   $zNormal._entropyc                 C      | j t| j S r5   )r   r   rX   r   r3   r+   r+   r,   _mean   rH   zNormal._meanc                 C   s   |  t|S r5   )_inv_zr   Zndtri)r(   pr+   r+   r,   	_quantile   r<   zNormal._quantilec                 C   r[   r5   )r   r   rX   r   r3   r+   r+   r,   _stddev   rH   zNormal._stddevc                 C   s   |   S r5   )r\   r3   r+   r+   r,   _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.Zstandardizer   N)r   r!   r   r   rE   r+   r+   r,   rK         $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.Zreconstructr   N)r   r!   r   r   )r(   zr+   r+   r,   r]      rb   zNormal._inv_z)FTr   r5   ) __name__
__module____qualname____doc__r   
deprecatedr&   staticmethodr1   propertyr   r   r7   r9   r;   r=   rB   rG   rL   rN   rP   rQ   rC   rD   rZ   r\   r_   r`   ra   rK   r]   __classcell__r+   r+   r)   r,   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   Zsoftplus_scaler   )r   r   r   r   r   )
r   r    r   r!   r%   r   r&   r	   Zsoftplus_parametersr'   r)   r+   r,   r&      s   


z NormalWithSoftplusScale.__init__)FTr   )rd   re   rf   rg   r   rh   r&   rk   r+   r+   r)   r,   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)
  Zkl_normal_normal   r-   r.   rT   N)r   r!   r   r   r:   r   r   rR   r   Zsquared_differencerV   )	Zn_aZn_br   onetwoZhalfZs_a_squaredZs_b_squaredratior+   r+   r,   _kl_normal_normal  s   $rq   r5   )rg   rU   Ztensorflow.python.frameworkr   r   r   r   Ztensorflow.python.opsr   r   r   r	   r
   Z#tensorflow.python.ops.distributionsr   r   r   Ztensorflow.python.utilr   Z tensorflow.python.util.tf_exportr   __all__Distributionr   r   Z
RegisterKLrq   r+   r+   r+   r,   <module>   s2   
 L
