
    j                     r    d dl mZ d dlmZ  G d de          Z G d de          Z G d de          ZdS )	    N)Modulec                   Z     e Zd ZdZd	def fdZdefdZdej	        dej	        fdZ
 xZS )
DropoutaR  Randomly zero a portion of the elements during training.

    The remaining elements are multiplied with :math:`\frac{1}{1-p}` where
    :math:`p` is the probability of zeroing an element. This is done so the
    expected value of a given element will remain the same.

    Args:
        p (float): The probability to zero an element
          ?pc                     t                                                       |dk     s|dk    rt          d| d          d|z
  | _        d S Nr      zThe dropout probability z is not in [0, 1)super__init__
ValueError_p_1selfr   	__class__s     _/lsinfo/ai/hellotax_ai/base_platform/venv/lib/python3.11/site-packages/mlx/nn/layers/dropout.pyr   zDropout.__init__   S    q55AFFLLLLMMME			    returnc                     dd| j         z
   S Nzp=r
   r   r   s    r   _extra_reprzDropout._extra_repr       !AdiK!!!r   xc                     | j         dk    s| j        s|S t          j                            | j         |j                  }||z  d| j         z  z  S )Nr
   )r   trainingmxrandom	bernoullishape)r   r   masks      r   __call__zDropout.__call__   sH    9>>>Hy""49ag66qQ]++r   r   __name__
__module____qualname____doc__floatr   strr   r    arrayr%   __classcell__r   s   @r   r   r      s          %      "S " " " ","( ,rx , , , , , , , ,r   r   c                   Z     e Zd ZdZd	def fdZdefdZdej	        dej	        fdZ
 xZS )
	Dropout2da  Apply 2D channel-wise dropout during training.

    Randomly zero out entire channels independently with probability :math:`p`.
    This layer expects the channels to be last, i.e. the input shape should be
    ``NWHC`` or ``WHC`` where:``N`` is the batch dimension,``H`` is the input
    image height,``W`` is the input image width, and``C`` is the number of
    input channels

    The remaining channels are scaled by :math:`\frac{1}{1-p}` to
    maintain the expected value of each element. Unlike traditional dropout,
    which zeros individual entries, this layer zeros entire channels. This is
    beneficial for early convolution layers where adjacent pixels are
    correlated. In such case, traditional dropout may not effectively
    regularize activations. For more details, see [1].

    [1]: Thompson, J., Goroshin, R., Jain, A., LeCun, Y. and Bregler C., 2015.
    Efficient Object Localization Using Convolutional Networks. CVPR 2015.

    Args:
        p (float): Probability of zeroing a channel during training.
    r   r   c                     t                                                       |dk     s|dk    rt          d| d          d|z
  | _        d S r	   r   r   s     r   r   zDropout2d.__init__=   r   r   r   c                     dd| j         z
   S r   r   r   s    r   r   zDropout2d._extra_reprE   r   r   r   c                    |j         dvrt          d|j          d          | j        dk    s| j        s|S t	          |j                  }dx|d<   |d<   t          j                            | j        |          }||z  d| j        z  z  S )N)      Received input with z( dimensions. Expected 3 or 4 dimensions.r
   r   r#   	ndimr   r   r   listr#   r    r!   r"   r   r   
mask_shaper$   s       r   r%   zDropout2d.__call__H   s    6WqvWWW   9>>>H
 !']]
*++
2By""TYj"AAqQ]++r   r&   r'   r0   s   @r   r2   r2   &   s         , %      "S " " " ","( ,rx , , , , , , , ,r   r2   c                   Z     e Zd ZdZd	def fdZdefdZdej	        dej	        fdZ
 xZS )
	Dropout3da.  Apply 3D channel-wise dropout during training.

    Randomly zero out entire channels independently with probability :math:`p`.
    This layer expects the channels to be last, i.e., the input shape should be
    `NDHWC` or `DHWC` where: `N` is the batch dimension, `D` is the depth,
    `H` is the input image height, `W` is the input image width, and `C` is
    the number of input channels.

    The remaining channels are scaled by :math:`\frac{1}{1-p}` to
    maintain the expected value of each element. Unlike traditional dropout,
    which zeros individual entries, this layer zeros entire channels. This is
    often beneficial for convolutional layers processing 3D data, like in
    medical imaging or video processing.

    Args:
        p (float): Probability of zeroing a channel during training.
    r   r   c                     t                                                       |dk     s|dk    rt          d| d          d|z
  | _        d S r	   r   r   s     r   r   zDropout3d.__init__n   r   r   r   c                     dd| j         z
   S r   r   r   s    r   r   zDropout3d._extra_reprv   r   r   r   c                    |j         dvrt          d|j          d          | j        dk    s| j        s|S t	          |j                  }dx|d<   x|d<   |d<   t          j                            | j        |          }||z  d| j        z  z  S )	N)r7      r8   z( dimensions. Expected 4 or 5 dimensions.r
   r9   r:   r;   r<   r?   s       r   r%   zDropout3d.__call__y   s    6WqvWWW   9>>>H
 !']]
;<<
2<B*R.y""TYj"AAqQ]++r   r&   r'   r0   s   @r   rB   rB   [   s         $ %      "S " " " ","( ,rx , , , , , , , ,r   rB   )mlx.corecorer    mlx.nn.layers.baser   r   r2   rB    r   r   <module>rL      s          % % % % % %, , , , ,f , , ,>2, 2, 2, 2, 2, 2, 2, 2,j., ., ., ., ., ., ., ., ., .,r   