o
    ?e                     @   s   d 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 edgdG dd dejZeeedddZdS )z!The Bernoulli distribution class.    )dtypes)ops)tensor_shape)	array_ops)math_ops)nn)
random_ops)distribution)kullback_leibler)util)deprecation)	tf_exportzdistributions.Bernoulli)v1c                       s   e Zd ZdZejddddddejddd f 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d$ddZdd Zdd Zdd Zd d! Zd"d# Z  ZS )%	BernoullizBernoulli distribution.

  The Bernoulli distribution with `probs` parameter, i.e., the probability of a
  `1` outcome (vs a `0` outcome).
  z
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_onceNFc              	      sz   t t }t|}tj||||d\| _| _W d   n1 s"w   Y  tt	| j
|tj|||| j| jg|d dS )a)  Construct Bernoulli distributions.

    Args:
      logits: An N-D `Tensor` representing the log-odds of a `1` event. Each
        entry in the `Tensor` parametrizes an independent Bernoulli distribution
        where the probability of an event is sigmoid(logits). Only one of
        `logits` or `probs` should be passed in.
      probs: An N-D `Tensor` representing the probability of a `1`
        event. Each entry in the `Tensor` parameterizes an independent
        Bernoulli distribution. Only one of `logits` or `probs` should be passed
        in.
      dtype: The type of the event samples. Default: `int32`.
      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:
      ValueError: If p and logits are passed, or if neither are passed.
    )logitsprobsvalidate_argsnameN)dtypeZreparameterization_typer   allow_nan_stats
parametersZgraph_parentsr   )dictlocalsr   
name_scopedistribution_utilZget_logits_and_probs_logits_probssuperr   __init__r	   ZNOT_REPARAMETERIZED)selfr   r   r   r   r   r   r   	__class__ n/home/www/facesmatcher.com/pyenv/lib/python3.10/site-packages/tensorflow/python/ops/distributions/bernoulli.pyr   '   s$   
(


zBernoulli.__init__c                 C   s   dt j| tjdiS )Nr   r   )r   Zconvert_to_tensorr   int32)Zsample_shaper"   r"   r#   _param_shapes_      zBernoulli._param_shapesc                 C      | j S )z#Log-odds of a `1` outcome (vs `0`).)r   r   r"   r"   r#   r   c      zBernoulli.logitsc                 C   r(   )z&Probability of a `1` outcome (vs `0`).)r   r)   r"   r"   r#   r   h   r*   zBernoulli.probsc                 C      t | jS N)r   shaper   r)   r"   r"   r#   _batch_shape_tensorm      zBernoulli._batch_shape_tensorc                 C   s
   | j  S r,   )r   	get_shaper)   r"   r"   r#   _batch_shapep      
zBernoulli._batch_shapec                 C   s   t jg tjdS )Nr$   )r   Zconstantr   r%   r)   r"   r"   r#   _event_shape_tensors   s   zBernoulli._event_shape_tensorc                 C   s
   t g S r,   )r   ZTensorShaper)   r"   r"   r#   _event_shapev   r2   zBernoulli._event_shapec                 C   sF   t |g|  gd}tj||| jjd}t|| j}t	|| jS )Nr   )seedr   )
r   concatZbatch_shape_tensorr   Zrandom_uniformr   r   r   lesscast)r   nr5   Z	new_shapeuniformsampler"   r"   r#   	_sample_ny   s   
zBernoulli._sample_nc                 C   sz   | j rtj|tjd}t|| jj}| j}dd }|	 
 r.|	 
 r.|	 |	 ks5|||\}}tj||d S )N)Ztarget_dtypec                 S   s   t ||  t | | fS r,   )r   Z	ones_like)r   eventr"   r"   r#   
_broadcast   s   z'Bernoulli._log_prob.<locals>._broadcast)labelsr   )r   r   Z"embed_check_integer_casting_closedr   boolr   r8   r   r   r0   Zis_fully_definedr   Z!sigmoid_cross_entropy_with_logits)r   r=   r   r>   r"   r"   r#   	_log_prob   s   
zBernoulli._log_probc                 C   s&   | j  t| j d  t| j   S )N   )r   r   sigmoidr   softplusr)   r"   r"   r#   _entropy   s   zBernoulli._entropyc                 C   r+   r,   )r   identityr   r)   r"   r"   r#   _mean   r/   zBernoulli._meanc                 C   s   |   d| j  S )Ng      ?)rG   r   r)   r"   r"   r#   	_variance   s   zBernoulli._variancec                 C   s   t | jdk| jS )z.Returns `1` if `prob > 0.5` and `0` otherwise.g      ?)r   r8   r   r   r)   r"   r"   r#   _mode   r'   zBernoulli._moder,   )__name__
__module____qualname____doc__r   
deprecatedr   r%   r   staticmethodr&   propertyr   r   r.   r1   r3   r4   r<   rA   rE   rG   rH   rI   __classcell__r"   r"   r    r#   r      s<    	0



r   Nc                 C   s   t j|d| j|jgd3 t|j t| j  }t|jt| j }t| j| t| j |  W  d   S 1 sBw   Y  dS )aC  Calculate the batched KL divergence KL(a || b) with a and b Bernoulli.

  Args:
    a: instance of a Bernoulli distribution object.
    b: instance of a Bernoulli distribution object.
    name: (optional) Name to use for created operations.
      default is "kl_bernoulli_bernoulli".

  Returns:
    Batchwise KL(a || b)
  Zkl_bernoulli_bernoulli)valuesN)r   r   r   r   rD   r   rC   )abr   Zdelta_probs0Zdelta_probs1r"   r"   r#   _kl_bernoulli_bernoulli   s   
$rU   r,   )rM   Ztensorflow.python.frameworkr   r   r   Ztensorflow.python.opsr   r   r   r   Z#tensorflow.python.ops.distributionsr	   r
   r   r   Ztensorflow.python.utilr   Z tensorflow.python.util.tf_exportr   Distributionr   Z
RegisterKLrU   r"   r"   r"   r#   <module>   s$   
 
