o
    i1                     @   sh  d dl mZ d dlmZmZmZmZ d dlmZm	Z	m
Z
mZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZm Z m!Z! d dl"m#Z$ d dl%m&Z&m'Z' d dl(m)Z)m*Z* ddl+m,Z, dd	l-m.Z. dd
l/m0Z0 ddl1m2Z2 ddl3m3Z3 ddl4m5Z5 ddl6m7Z7 dZ8	d>dede9dee: deee0 e*f fddZ;dd Z<	d>de9de:de9dee: deee0 e*f f
ddZ=		d?de9de:de9d e:dee: deee0 e*f fd!d"Z>	d>de9de:de9d#eee)e)e)f e)f dee: deee0 e*f fd$d%Z?	d>deee0 ee* f d&eee0 e*f dee: deee0 e*f fd'd(Z@defd)d*ZA	d>d+e:d,eeB dee: deee0 e*f fd-d.ZC	d>deee0 ee* f de9dee: deee0 e*f fd/d0ZDdd1deee0 ee* f d2ee*e*f d3eee*e*f  deee0 e*f fd4d5ZEdefd6d7ZF	d>dede9d8e9d9e9d:e9d;e9dee: deee0 e*f fd<d=ZGdS )@    )partial)ListOptionalTuplecast)DropoutGelu	LayerNormLinearLogisticMaxoutModelParametricAttentionParametricAttention_v2ReluSoftmaxSparseLinearSparseLinear_v2chaincloneconcatenatelist2raggedreduce_firstreduce_last
reduce_maxreduce_mean
reduce_sumresidual	resizablesoftmax_activationwith_cpu)init)resize_linear_weightedresize_model)ArrayXdFloats2d   )ORTH)Errors)Doc)registry   )extract_ngrams)StaticVectors   )get_tok2vec_widthixNtok2vecexclusive_classesnOreturnc              	   C   s   t | |dddd|dS )a1  
    Build a simple CNN text classifier, given a token-to-vector model as inputs.
    If exclusive_classes=True, a softmax non-linearity is applied, so that the
    outputs sum to 1. If exclusive_classes=False, a logistic non-linearity
    is applied instead, so that outputs are in the range [0, 1].
    FT)r0   r1   use_reduce_firstuse_reduce_lastuse_reduce_maxuse_reduce_meanr2   )build_reduce_text_classifier)r0   r1   r2    r9   K/home/ubuntu/.local/lib/python3.10/site-packages/spacy/ml/models/textcat.py build_simple_cnn_text_classifier/   s   	r;   c                 C   s0   t ||}| d|jd  | jd|dd | S )Noutput_layerr   r2   T)force)r#   set_reflayersset_dim)modelnew_nOresizable_layerr9   r9   r:   resize_and_set_refC   s   
rD   
ngram_sizeno_output_layerc                 C   s   t | |||t|ddS )Nr2   r1   rE   rF   r2   sparse_linear)_build_bow_text_classifierr   )r1   rE   rF   r2   r9   r9   r:   build_bow_text_classifierJ   s   rK      lengthc              	   C   sD   |dk rt tjj|dd|d   }t| |||t||ddS )Nr.   )rM   r+   )r2   rM   rH   )
ValueErrorr(   E1056format
bit_lengthrJ   r   )r1   rE   rF   rM   r2   r9   r9   r:   build_bow_text_classifier_v3Y   s   
rR   rI   c           	      C   s   ddd}t dti: d }|st|d< | rt nt }t|tt|dd}t	|t
d|? }t||j}|r?|t||j? }W d    n1 sIw   Y  |d ur[|dtt| |d	| |  |jd
< tt|d|jd< |S )Nr   bW>>rT   fill_defaultsresize_layer)attrr2   r<   multi_labelrC   resize_output)r   define_operatorsr   	NEG_VALUEr   r   r   r   r"   r,   r'   r    opsr@   r   intr>   attrsrD   )	r1   rE   rF   rI   r2   rX   r<   rC   rA   r9   r9   r:   rJ   o   s0   

rJ   linear_modelc                 C   sL  |j d  }tttdV | d}t|}t||d}t|d}| t	 ? |? t
 ? t||? td? ? }|r=|d nd }	|rHt||	d}
n	t||	dt ? }
||B |
? }|d|  W d    n1 sgw   Y  |dd	ur|d ur|dtt| |d
|d
 |d| |d| |d| | |j d< t|_|S )Nr\   rV   |r2   r2   nIrh           r+   r0   Fr<   attention_layermaxout_layer
norm_layer)rc   r   r_   r   r   maybe_get_dimr   r   r	   r   r   r   r   r   r
   r   r>   has_dimr@   r   rb   get_refinit_ensemble_textcatr!   )r0   rd   r2   r1   widthrk   rl   rm   	cnn_model	nO_doubler<   rA   r9   r9   r:   build_text_classifier_v2   s@   

ru   c                 C   s   |  d}|| t| }|  dd| |  dd| |  dd| |  dd| |  dd| t| || | S )Nr0   rk   r2   rl   rh   rm   rp   
initializer/   r@   
init_chainrA   XYr0   tok2vec_widthr9   r9   r:   rq      s   

rq   rr   dropoutc                 C   s   t ttd2 t| t ? t| ? t ? tt	| | d ? t
|| ? }|r-|t|? }|t ? }W d    |S 1 s=w   Y  |S )N)rV   z**r+   )r   r_   r   r   r-   r   r   r   r   r   r
   r   r   )rr   r}   r2   rA   r9   r9   r:   build_text_classifier_lowdata   s(   
r~   c                 C   s   |  d}t| t||dt||dd}tdti |r$t|d}nt|dt	 ? }||? }W d    n1 s:w   Y  |
ddurS|d urS|dtt| |d| | |jd< |S )	Nr2   )rh   r2   )r0   nonlinear_layerkey_transformrV   rG   Fr<   r\   )rn   3_build_parametric_attention_with_residual_nonlinearr   r   r   r_   r   r   r
   r   ro   r@   r   rb   r>   rc   )r0   r1   r2   rr   parametric_attentionr<   rA   r9   r9   r:   %build_textcat_parametric_attention_v1   s"   



r   )r   r   r   c                 C   s   t ttdO | d}t||d}t|d}| t ? |? t ? t	||? t
d? ? }t|_|d|  |d| |d| |d	| |d
| |W  d    S 1 sZw   Y  d S )Nre   r2   )r2   r   ri   rj   r0   rk   r   r   rm   )r   r_   r   r   rn   r   r	   r   r   r   r   2_init_parametric_attention_with_residual_nonlinearr!   r>   )r0   r   r   rr   rk   rm   r   r9   r9   r:   r      s,   

$r   c                 C   s   |  d}|| t| }|  dd| |  dd| |  dd| |  dd| |  dd| |  dd| |  dd| t| || | S )Nr0   rk   r2   r   rh   r   rm   rv   ry   r9   r9   r:   r     s   

r   r4   r5   r6   r7   c                 C   sz  ddd}g }|r| t  |r| t  |r| t  |r'| t  t|s0ttjt	
dtic | t ? t| ? }	| d}
|
durP|
t| nd}|rmt||d}t|d< t|tt|dd	}|	|? }nt||d}t|tt|dd	}|	|? t ? }|d
| tt|d|jd< W d   n1 sw   Y  |d|  |dur|dtt| | |jd< |S )a  Build a model that classifies pooled `Doc` representations.

    Pooling is performed using reductions. Reductions are concatenated when
    multiple reductions are used.

    tok2vec (Model): the tok2vec layer to pool over.
    exclusive_classes (bool): Whether or not classes are mutually exclusive.
    use_reduce_first (bool): Pool by using the hidden representation of the
        first token of a `Doc`.
    use_reduce_last (bool): Pool by using the hidden representation of the
        last token of a `Doc`.
    use_reduce_max (bool): Pool by taking the maximum values of the hidden
        representations of a `Doc`.
    use_reduce_mean (bool): Pool by taking the mean of all hidden
        representations of a `Doc`.
    nO (Optional[int]): Number of classes.
    r   rS   rV   r2   Nrg   rT   rW   rY   r<   r]   r^   r0   r\   )appendr   r   r   r   lenrN   r(   E1057r   r_   r   r   r   rn   r   r`   r   r   r"   r
   r   r>   rD   rc   r@   r   rb   )r0   r1   r4   r5   r6   r7   r2   rX   
reductionscnn
nO_tok2vecrh   r<   rC   rA   r9   r9   r:   r8   #  sZ   



r8   )N)rL   N)H	functoolsr   typingr   r   r   r   	thinc.apir   r   r	   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    thinc.layers.chainr!   rx   thinc.layers.resizabler"   r#   thinc.typesr$   r%   rc   r'   errorsr(   tokensr)   utilr*   r,   staticvectorsr-   r0   r/   r`   boolrb   r;   rD   rK   rR   rJ   ru   rq   floatr~   r   r   r   r8   r9   r9   r9   r:   <module>   s    p



"
)



