o
    "j                     @   s8   d dl Z d dl mZ d dlmZ G dd de jjZdS )    N)_legacy_C_ops)in_dynamic_modec                       s2   e Zd ZdZd fdd	Zdd Zd	d
 Z  ZS )FusedDropouta
	  
    Dropout is a regularization technique for reducing overfitting by preventing
    neuron co-adaption during training as described in the paper:
    `Improving neural networks by preventing co-adaptation of feature detectors <https://arxiv.org/abs/1207.0580>`_
    The dropout operator randomly sets the outputs of some units to zero, while upscale others
    according to the given dropout probability.

    It is an optimized implementation for ``paddle.nn.Dropout``.

    In dygraph mode, please use ``eval()`` to switch to evaluation mode, where dropout is disabled.

    Parameters:
        p (float|int, optional): Probability of setting units to zero. Default: 0.5
        axis (int|list|tuple, optional): The axis along which the dropout is performed. Default: None.
        mode(str, optional): ['upscale_in_train'(default) | 'downscale_in_infer']

                               1. upscale_in_train (default), upscale the output at training time

                                  - train: :math:`out = input \times \frac{mask}{(1.0 - p)}`
                                  - inference: :math:`out = input`

                               2. downscale_in_infer, downscale the output at inference

                                  - train: :math:`out = input \times mask`
                                  - inference: :math:`out = input \times (1.0 - p)`
        name (str, optional): Name for the operation, Default: None. For more information, please refer to :ref:`api_guide_Name`.

    Shape:
        - input: N-D tensor.
        - output: N-D tensor, the same shape as input.


    Examples:
        .. code-block:: python

            >>> import paddle
            >>> paddle.seed(2023)

            >>> x = paddle.to_tensor([[1, 2, 3], [4, 5, 6]], dtype="float32")
            >>> m = paddle.incubate.nn.FusedDropout(p=0.5)

            >>> y_train = m(x)
            >>> print(y_train)
            Tensor(shape=[2, 3], dtype=float32, place=Place(cpu), stop_gradient=True,
            [[0., 0., 6.],
             [0., 0., 0.]])

            >>> m.eval()  # switch the model to test phase
            >>> y_test = m(x)
            >>> print(y_test)
            Tensor(shape=[2, 3], dtype=float32, place=Place(cpu), stop_gradient=True,
            [[1., 2., 3.],
             [4., 5., 6.]])
          ?Nupscale_in_trainc                    s   t    t|ttfstd|dk s|dkrtd|dkr"dn|}|dvr,td|r:t|tttfs:td	|| _	|| _
|| _d | _|d urYt|trR|gnt|| _d S d S )
Nzp argument should be a numberr      z!p argument should between 0 and 1downscale_in_inferZdowngrade_in_infer)r   r   zBmode argument should be 'downscale_in_infer' or 'upscale_in_train'z/datatype of axis argument should be int or list)super__init__
isinstancefloatint	TypeError
ValueErrorlisttuplepmodenameaxis)selfr   r   r   r   	__class__ j/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/paddle/incubate/nn/layer/fused_dropout_nd.pyr
   L   s(   
zFusedDropout.__init__c                 C   s   | j dkr|S | jd urAt rAd }tj jdkrtj j}t|d| j d| j	 d|d ud|d ur4|ndd| j
d| j\}}|S tjjj|| j | j| j	| j
| jd}|S )	Nr   Zdropout_probZis_testZfix_seedseedZdropout_implementationr   )r   r   trainingr   r   )r   r   r   paddleZstaticZdefault_main_programZrandom_seedr   Z
dropout_ndr   r   nnZ
functionalZdropoutr   )r   inputr   outmaskr   r   r   forwardg   s>   
zFusedDropout.forwardc                 C   s6   | j r	d| j  nd}d| j d| j d| j | S )Nz, name= zp=z, axis=z, mode=)r   r   r   r   )r   Zname_strr   r   r   
extra_repr   s    zFusedDropout.extra_repr)r   Nr   N)__name__
__module____qualname____doc__r
   r"   r$   __classcell__r   r   r   r   r      s
    7$r   )r   r   Zpaddle.frameworkr   r   ZLayerr   r   r   r   r   <module>   s   