o
    h*                     @   sN   d dl Z d dlmZ G dd dejZG dd dejZG dd dejZdS )    Nc                       s2   e Zd ZdZ fddZd	ddZdd Z  ZS )
WordDropouta0   A word dropout layer that's designed for embedded inputs (e.g., any inputs to an LSTM layer).
    Given a batch of embedded inputs, this layer randomly set some of them to be a replacement state.
    Note that this layer assumes the last dimension of the input to be the hidden dimension of a unit.
    c                    s   t    || _d S N)super__init__dropprob)selfr   	__class__ W/var/www/html/env_mimamsha/lib/python3.10/site-packages/stanza/models/common/dropout.pyr   	   s   

zWordDropout.__init__Nc                 C   sn   | j r| jdkr
|S dd | D }d|d< tj|d|ji| jk }||d}|d ur5|| |  }|S )Nr   c                 S      g | ]}|qS r
   r
   .0yr
   r
   r   
<listcomp>       z'WordDropout.forward.<locals>.<listcomp>   device)trainingr   sizetorchrandr   masked_fillfloat)r   xreplacementmasksizedropmaskresr
   r
   r   forward   s   zWordDropout.forwardc                 C      d | jS Nzp={}formatr   r   r
   r
   r   
extra_repr      zWordDropout.extra_reprr   __name__
__module____qualname____doc__r   r    r&   __classcell__r
   r
   r   r   r      s
    
r   c                       s2   e Zd ZdZd	 fdd	Zdd Zdd Z  ZS )
LockedDropouta  
    A variant of dropout layer that consistently drops out the same parameters over time. Also known as the variational dropout. 
    This implementation was modified from the LockedDropout implementation in the flair library (https://github.com/zalandoresearch/flair).
    Tc                       t    || _|| _d S r   )r   r   r   batch_first)r   r   r0   r   r
   r   r   #      

zLockedDropout.__init__c                 C   s   | j r| jdkr
|S | js#|jd|d|dddd| j }n|j|dd|dddd| j }|d| j |}|| S )Nr   r      F)requires_grad)r   r   r0   	new_emptyr   
bernoulli_div	expand_as)r   r   mmaskr
   r
   r   r    (   s   ,*zLockedDropout.forwardc                 C   r!   r"   r#   r%   r
   r
   r   r&   4   r'   zLockedDropout.extra_repr)Tr(   r
   r
   r   r   r.      s
    r.   c                       s0   e Zd ZdZ fddZdd Zdd Z  ZS )SequenceUnitDropoutz A unit dropout layer that's designed for input of sequence units (e.g., word sequence, char sequence, etc.).
    Given a sequence of unit indices, this layer randomly set some of them to be a replacement id (usually set to be <UNK>).
    c                    r/   r   )r   r   r   replacement_id)r   r   r;   r   r
   r   r   ;   r1   zSequenceUnitDropout.__init__c                 C   sP   | j r| jdkr
|S dd | D }tj|d|ji| jk }||| j}|S )z1 :param: x must be a LongTensor of unit indices. r   c                 S   r   r
   r
   r   r
   r
   r   r   D   r   z/SequenceUnitDropout.forward.<locals>.<listcomp>r   )r   r   r   r   r   r   r   r;   )r   r   r   r   r   r
   r
   r   r    @   s   zSequenceUnitDropout.forwardc                 C   s   d | j| jS )Nzp={}, replacement_id={})r$   r   r;   r%   r
   r
   r   r&   I   s   zSequenceUnitDropout.extra_reprr(   r
   r
   r   r   r:   7   s
    	r:   )r   torch.nnnnModuler   r.   r:   r
   r
   r
   r   <module>   s
    