o
    
i                     @   s
  d dl mZmZ d dlmZ d dlmZmZ d dlm	Z	m
Z
mZmZmZmZ d dlmZ d dlmZ 	dded	ed
ee deee ef fddZ	dd	ededed
ee deee ef f
ddZ	ddededee d	ededededee d
ee defddZdS )    )OptionalList)Floats2d)Modelwith_cpu)IDORTHPREFIXSUFFIXSHAPELOWER)registry)DocNtok2vecexclusive_classesnOreturnc                 C   s  t dd}t dd}t dd}t dd}t dd}t dd}td|i< | | ? | ? }	|rJ||| d	d
}
|	|
? }|d|
 n||| d	d
}|	|? | ? }|d| W d   n1 sjw   Y  |d|  |d	| | |j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].
    layerschain.v1zreduce_mean.v1Logistic.v1
Softmax.v1	Linear.v1list2ragged.v1>>r   r   nIoutput_layerNr   multi_label)r   getr   define_operatorsmaybe_get_dimset_refset_dimattrs)r   r   r   chainreduce_meanLogisticSoftmaxLinearlist2raggedcnnr   modellinear_layer r-   V/home/ubuntu/.local/lib/python3.10/site-packages/spacy_legacy/architectures/textcat.pyTextCatCNN_v1	   s*   	
r/   
ngram_sizeno_output_layerc                 C   s   t dd}t dd}t dd}t dd}t dd}td|i, ||}	||td|	? }
t|
|
j}
|sJ| r?| n| }|
t||j? }
W d    n1 sTw   Y  |
d	|	 |  |
jd
< |
S )Nr   r   r   zSparseLinear.v1zsoftmax_activation.v1zspacy.extract_ngrams.v1r   )attrr   r   )	r   r   r   r   r   r   opsr!   r#   )r   r0   r1   r   r$   r&   SparseLinearsoftmax_activationextract_ngramssparse_linearr+   r   r-   r-   r.   TextCatBOW_v1)   s"   r8   width
embed_sizepretrained_vectorswindow_size
conv_depthdropoutc	           ,      C   s4  t dd}	t dd}
t dd}t dd}t dd}t dd}t dd}t dd	}t dd
}t dd}t dd}t dd}t dd}t dd}t dd}t dd}t dd}t dd}t dd}ttttttg}t	|||d |	| ||
t|dd}|	| d ||
t|dd}|	| d ||
t|dd}|	| d ||
t|dd} tdd |||| fD }!|
|||||B |B | B || |!dd ? |
td!? }"|r|| }#|"|#B }$| d }%n|"}$| }%|$||| |%dd"|||d#|| | |d d$  dd ? | ? |d%? }&|&| ? || ? | ? ||| | d&? ||| d&? |d'? }'||||d(d)}(|rF|d nd })|rR|||)d&}*n|||)d&|d'? | ? }*|(|'B |*? }+|+d*|& W d    n	1 svw   Y  |+d+d(ur|+d+| |+d,|(d, | |+jd-< |+S ).Nr   zHashEmbed.v1zspacy.FeatureExtractor.v1z	Maxout.v1zspacy.StaticVectors.v1r   r   zParametricAttention.v1z
Dropout.v1r   architectureszspacy.TextCatBOW.v1r   r   zconcatenate.v1zclone.v1zreduce_sum.v1zwith_array.v1z
uniqued.v1zresidual.v1zexpand_window.v1)r   |z**
   )r   nVcolumnr>   seed            c                 s   s    | ]}| d V  qdS )r   N)get_dim).0layerr-   r-   r.   	<genexpr>{   s    z%TextCatEnsemble_v1.<locals>.<genexpr>T)r   r   	normalize)rC   )rM   )r<      )padr   g        F)r   r0   r   r1   r   r   r   r   )r   r   r   r   r	   r
   r   r   r   r   indexsumr!   has_dimr"   get_refr#   ),r9   r:   r;   r   r0   r<   r=   r>   r   	HashEmbedFeatureExtractorMaxoutStaticVectorsr'   r(   ParametricAttentionDropoutr&   build_bow_text_classifierr)   r$   concatenateclone
reduce_sum
with_arrayuniquedresidualexpand_windowcolslowerprefixsuffixshapewidth_nItrained_vectorsstatic_vectorsvector_layervectors_widthr   	cnn_modellinear_model	nO_doubler   r+   r-   r-   r.   TextCatEnsemble_v1A   s   



Lro   )N)typingr   r   thinc.typesr   	thinc.apir   r   spacy.attrsr   r   r	   r
   r   r   
spacy.utilr   spacy.tokensr   boolintr/   r8   floatro   r-   r-   r-   r.   <module>   sd     
$
!	
