
    AVh                        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       G d dej&                               Z ej*                  ee      dd       Zy)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                        e Zd ZdZ ej
                  ddd      ddej                  ddd f fd	       Ze	d	        Z
ed
        Zed        Zd Zd Zd Zd ZddZd Zd Zd Zd Zd Z x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           	      H   t        t                     }t        j                  |      5 }t	        j
                  ||||      \  | _        | _        ddd       t        t        | +  |t        j                  |||| j                  | j                  g|       y# 1 sw Y   HxY w)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__s	           ]/home/dcms/DCMS/lib/python3.12/site-packages/tensorflow/python/ops/distributions/bernoulli.pyr%   zBernoulli.__init__'   s    P fhJ		 "3"H"H%	#dlDK 
)T# , @ @#'||T[[1 $  s   'BB!c                 P    dt        j                  | t        j                        iS )Nr   r   )r   convert_to_tensorr   int32)sample_shapes    r)   _param_shapeszBernoulli._param_shapes_   s    c++LMNN    c                     | j                   S )z#Log-odds of a `1` outcome (vs `0`).)r"   r'   s    r)   r   zBernoulli.logitsc   s     <<r0   c                     | j                   S )z&Probability of a `1` outcome (vs `0`).)r#   r2   s    r)   r   zBernoulli.probsh   s     ;;r0   c                 @    t        j                  | j                        S N)r   shaper"   r2   s    r)   _batch_shape_tensorzBernoulli._batch_shape_tensorm   s    ??4<<((r0   c                 6    | j                   j                         S r5   )r"   	get_shaper2   s    r)   _batch_shapezBernoulli._batch_shapep   s    <<!!##r0   c                 L    t        j                  g t        j                        S )Nr+   )r   constantr   r-   r2   s    r)   _event_shape_tensorzBernoulli._event_shape_tensors   s    b55r0   c                 ,    t        j                  g       S r5   )r   TensorShaper2   s    r)   _event_shapezBernoulli._event_shapev   s    ##B''r0   c                 (   t        j                  |g| j                         gd      }t        j                  ||| j
                  j                        }t        j                  || j
                        }t        j                  || j                        S )Nr   )seedr   )
r   concatbatch_shape_tensorr	   random_uniformr   r   r   lesscast)r'   nrB   	new_shapeuniformsamples         r)   	_sample_nzBernoulli._sample_ny   sn      1#t'>'>'@!A1EI''DJJ$4$46G]]7DJJ/F==,,r0   c                    | j                   r%t        j                  |t        j                        }t        j                  || j                  j                        }| j                  }d }|j                         j                         r?|j                         j                         r!|j                         |j                         k(  s |||      \  }}t        j                  ||       S )N)target_dtypec                 b    t        j                  |      | z  t        j                  |       |z  fS r5   )r   	ones_like)r   events     r)   
_broadcastz'Bernoulli._log_prob.<locals>._broadcast   s2    !!%(61!!&)E13 3r0   )labelsr   )r   r    "embed_check_integer_casting_closedr   boolr   rG   r   r   r9   is_fully_definedr   !sigmoid_cross_entropy_with_logits)r'   rQ   r   rR   s       r)   	_log_probzBernoulli._log_prob   s    BB
fkk+e
 MM%!2!23E[[F3 OO..0//1OO!1!1!33 /mfe00fMMMr0   c                     | j                    t        j                  | j                         dz
  z  t        j                  | j                          z   S )N   )r   r   sigmoidr   softplusr2   s    r)   _entropyzBernoulli._entropy   s@    [[LH,,T[[9A=>KK%& 'r0   c                 @    t        j                  | j                        S r5   )r   identityr   r2   s    r)   _meanzBernoulli._mean   s    djj))r0   c                 B    | j                         d| j                  z
  z  S )Ng      ?)r`   r   r2   s    r)   	_variancezBernoulli._variance   s    ::<2

?++r0   c                 \    t        j                  | j                  dkD  | j                        S )z.Returns `1` if `prob > 0.5` and `0` otherwise.g      ?)r   rG   r   r   r2   s    r)   _modezBernoulli._mode   s     ==c)4::66r0   r5   )__name__
__module____qualname____doc__r   
deprecatedr   r-   r%   staticmethodr/   propertyr   r   r7   r:   r=   r@   rL   rX   r]   r`   rb   rd   __classcell__)r(   s   @r)   r   r      s     ;'
  \\"#..` O O    )$6(-N,'*,7r0   r   Nc                    t        j                  |d| j                  |j                  g      5  t        j                  |j                         t        j                  | j                         z
  }t        j                  |j                        t        j                  | j                        z
  }t        j                  | j                        |z  t        j                  | j                         |z  z   cddd       S # 1 sw Y   yxY w)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   r\   r   r[   )abr   delta_probs0delta_probs1s        r)   _kl_bernoulli_bernoullirt      s     ~~d4hh13 :;;y)BKK	,BBL;;qxx(2;;qxx+@@LQXX&5y)L89	: : :s   CC??Dr5   )rh   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   
RegisterKLrt    r0   r)   <module>r}      s    ( . + 4 + * $ , < @ I . 6 ()*B7)) B7 +B7J Y	2: 3:r0   