o
    2h                     @   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)	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)dtypereparameterization_typer   allow_nan_stats
parametersgraph_parentsr   )dictlocalsr   
name_scopedistribution_utilget_logits_and_probs_logits_probssuperr   __init__r	   NOT_REPARAMETERIZED)selfr   r   r   r   r   r   r   	__class__ h/var/www/html/chatgem/venv/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   convert_to_tensorr   int32)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    r0   r'   r'   r(   r   h   r1   zBernoulli.probsc                 C      t | jS N)r   shaper   r0   r'   r'   r(   _batch_shape_tensorm      zBernoulli._batch_shape_tensorc                 C   s
   | j  S r3   )r   	get_shaper0   r'   r'   r(   _batch_shapep      
zBernoulli._batch_shapec                 C   s   t jg tjdS )Nr)   )r   constantr   r+   r0   r'   r'   r(   _event_shape_tensors   s   zBernoulli._event_shape_tensorc                 C   s
   t g S r3   )r   TensorShaper0   r'   r'   r(   _event_shapev   r9   zBernoulli._event_shapec                 C   sF   t |g|  gd}tj||| jjd}t|| j}t	|| jS )Nr   )seedr   )
r   concatbatch_shape_tensorr   random_uniformr   r   r   lesscast)r$   nr>   	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)target_dtypec                 S   s   t ||  t | | fS r3   )r   	ones_like)r   eventr'   r'   r(   
_broadcast   s   z'Bernoulli._log_prob.<locals>._broadcast)labelsr   )r   r   "embed_check_integer_casting_closedr   boolr   rC   r   r   r7   is_fully_definedr   !sigmoid_cross_entropy_with_logits)r$   rK   r   rL   r'   r'   r(   	_log_prob   s   
zBernoulli._log_probc                 C   s&   | j  t| j d  t| j   S )N   )r   r   sigmoidr   softplusr0   r'   r'   r(   _entropy   s   zBernoulli._entropyc                 C   r2   r3   )r   identityr   r0   r'   r'   r(   _mean   r6   zBernoulli._meanc                 C   s   |   d| j  S )Ng      ?)rX   r   r0   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   rC   r   r   r0   r'   r'   r(   _mode   r.   zBernoulli._moder3   )__name__
__module____qualname____doc__r   
deprecatedr   r+   r"   staticmethodr-   propertyr   r   r5   r8   r;   r=   rH   rR   rV   rX   rY   rZ   __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)
  kl_bernoulli_bernoulli)valuesN)r   r   r   r   rU   r   rT   )abr   delta_probs0delta_probs1r'   r'   r(   _kl_bernoulli_bernoulli   s   
$ri   r3   )r^   tensorflow.python.frameworkr   r   r   tensorflow.python.opsr   r   r   r   #tensorflow.python.ops.distributionsr	   r
   r   r   tensorflow.python.utilr    tensorflow.python.util.tf_exportr   Distributionr   
RegisterKLri   r'   r'   r'   r(   <module>   s$   
 
