
    #Zj0A                     p   d dl 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 ddlmZ ddlmZmZ ddlmZmZ  e
d          Z e
d          Z e
d	          Z e
d
          Zeeef         Zeeeee         ee         f         Z G d deeeeef                   Z G d de          Z ej        d          ddddde deee                  deeeef                  defd            Z! ej        d          dddddde deee                  deeeef                  dee         def
d            Z" ej        d          ddddddde deee                  deeeef                  dee         de#defd             Z$ G d! d"e          Z% ej        d#          ddd$de deee                  de%fd%            Z& ej        d&          dddd'de deee                  dee         de%fd(            Z' ej        d)          ddddddde deee                  deeeef                  dee         de#de%fd*            Z( G d+ d,e          Z) ej        d-          dd.de de)fd/            Z* G d0 d1e          Z+ ej        d2          dd3d4de d5e de+fd6            Z,defd7Z-defd8Z.g d9Z/dS ):    )abstractmethod)
AnyDictGenericListOptionalSequenceTupleTypeVarUnioncast   )registry)Floats2dInts1d)get_array_moduleto_categoricalLossTGradTGuessTTruthTc                       e Zd ZdZdeddfdZdededee	e
f         fdZededede	fd	            Zededede
fd
            ZdS )Lossa  Base class for classes computing the loss / gradient. The class can
    be initialized with settings if needed. It provides get_loss and
    get_grad as separate methods to allow calculating them separately. It
    also provides a __call__ method that returns a tuple of both.
    kwargsreturnNc                     d S N )selfr   s     V/Users/jameslopez/projects/MentorCore/.venv/lib/python3.11/site-packages/thinc/loss.py__init__zLoss.__init__"   s          guessestruthsc                 Z    |                      ||          |                     ||          fS r   get_gradget_lossr   r#   r$   s      r    __call__zLoss.__call__$   )    }}Wf--t}}Wf/M/MMMr"   c                     dS )a}  Get the gradient of the loss. Note that the built-in loss functions
        compute the gradient with respect to the pre-activation logits, not the
        post-softmax probabilities. This is because thinc's softmax layer uses
        the identity function as its backward pass, so the softmax derivative
        is absorbed into the loss gradient for numerical stability.
        Nr   r)   s      r    r'   zLoss.get_grad'   s	     	r"   c                     d S r   r   r)   s      r    r(   zLoss.get_loss1   s    BE#r"   )__name__
__module____qualname____doc__r   r!   r   r   r
   r   r   r*   r   r'   r(   r   r"   r    r   r      s          322222N N N5;N N N N N   5    ^ EEE5EEE ^EEEr"   r   c                   X   e Zd ZU eee                  ed<   eeeef                  ed<   e	eef         ed<   ddddddde
deee                  deeeef                  d	ee         d
ef
dZdedeeef         fdZdededeeef         fdZdededefdZdededefdZdedefdZdS )CategoricalCrossentropynamesmissing_value
_name_to_iTN        	normalizer4   r5   
neg_prefixlabel_smoothingr9   r:   r;   c                    || _         || _        || _        || _        || _        | d t          |          D             | _        d S i | _        d S )Nc                     i | ]\  }}||	S r   r   ).0inames      r    
<dictcomp>z4CategoricalCrossentropy.__init__.<locals>.<dictcomp>I   s    GGG71dtQGGGr"   )r9   r4   r5   r:   r;   	enumerater6   r   r9   r4   r5   r:   r;   s         r    r!   z CategoricalCrossentropy.__init__:   s[     #
*$.GGi6F6FGGGDOOO DOOOr"   r#   r   c                     t          |          }g }d } j        r8|                    t          |          t           j                  fd          } j        }t          |t                    r_t          |          }t          |          rt          |d         t                    r1t          |          D ] \  }}||k    r|	                    |           !n̉ j        d}	t          |	          t          |          D ]\  }}||k    r& j        d         ||<   |	                    |           1|rc j        r\|                     j                  rB|t           j                  d          ||<    j        ||                  }
d||<   d||         |
<    fd|D             }|                    |d          }t          ||          }nt!          |||          }|j        |j        k    r6t%          t'          t(          |          |j        d          j                  }n j        rt          d	          |||z  }d||dk    <   d
||dk    <   ||z  }||fS )Nfdtyper   zCannot calculate loss from list of strings without names. You can pass the names as a keyword argument when you create the loss object, e.g. CategoricalCrossentropy(names=['dog', 'cat'])c                 *    g | ]}j         |         S r   )r6   )r>   r@   r   s     r    
<listcomp>z:CategoricalCrossentropy.convert_truths.<locals>.<listcomp>r   s     GGGdod3GGGr"   r?   )	n_classesr;   zLabel smoothing is only applied, when truths have type List[str], List[int] or Ints1d, but it seems like Floats2d was provided.r   )r   r4   oneslenr5   
isinstancelistintrB   append
ValueErrorr:   
startswithr6   asarray
_make_mask_make_mask_by_valuendimr   r   r   shaper;   )r   r$   r#   xpmissingnegatives_maskr5   r?   valuemsg	neg_indexmasks   `           r    convert_truthsz&CategoricalCrossentropy.convert_truthsM   s   g&&: 	PWWc&kk3tz??%C3WOON*fd## !	G&\\F6{{ HfQi-- H$-f$5$5 . .5 M11#NN1---. z)Q  )oo-$-f$5$5 > >5 M11(,
1F1I#NN1----!> $> !& 0 0 A A>
 ).c$/.B.B.D.D(EF1I(,q	(BI01N1-;=N1-i8GGGGGGGFZZcZ22Fgw//DD&vwFFD;',&&#VV$$!-+ $ 4  FF #  $   %n$F#$F6R< 34N>R/0N"Dt|r"   r$   c                 \    |                      ||          }||                     |          fS r   r'   _get_loss_from_gradr   r#   r$   d_truths       r    r*   z CategoricalCrossentropy.__call__   s0     --0011'::;;r"   c                    |                      ||          \  }}t          |          }|j        |j        k    r"d|j         d|j         d}t          |          |                    |dk              s|                    |dk               rd}t          |          |                    |dk              s|                    |dk               rd}t          |          ||z
  }||z  }| j        r||j        d         z  }|S )NzBCannot calculate CategoricalCrossentropy loss: mismatched shapes:  vs .r   r   zVCannot calculate CategoricalCrossentropy loss with guesses outside the [0,1] interval.z[Cannot calculate CategoricalCrossentropy loss with truth values outside the [0,1] interval.)r`   r   rX   rR   anyr9   )r   r#   r$   targetr_   rY   err
differences           r    r'   z CategoricalCrossentropy.get_grad   s   **67;;f%%=FL((yW^WdyyjpjvyyyCS//!66'A+ 	""&&1"5"5 	"kCS//!66&1* 	"
!3!3 	"pCS//!v%
d
> 	7#gmA&66Jr"   c                 X    |                      ||          }|                     |          S r   rb   rd   s       r    r(   z CategoricalCrossentropy.get_loss   s)    --00''000r"   re   c                 0    |dz                                   S )N   )sum)r   re   s     r    rc   z+CategoricalCrossentropy._get_loss_from_grad   s    
!!!r"   )r.   r/   r0   r   r	   str__annotations__r   rP   r   boolfloatr!   r   r
   r`   IntsOrFloatsOrStrsr*   r'   r(   rc   r   r"   r    r3   r3   5   s        HSM""""E#s(O,,,,S#X
 )-37$(!$! ! ! ! &	!
  c3h0! SM! ! ! ! !&?h ?58AS;T ? ? ? ?B<<);<	x	< < < < 2D     $1 12D 1 1 1 1 1"8 " " " " " " "r"   r3   zCategoricalCrossentropy.v1TNr9   r4   r5   r9   r4   r5   r   c                 &    t          | ||          S )Nrv   r3   rv   s      r    $configure_CategoricalCrossentropy_v1ry      s"     #5   r"   zCategoricalCrossentropy.v2r9   r4   r5   r:   r:   c                 (    t          | |||          S )Nrz   rx   rz   s       r    $configure_CategoricalCrossentropy_v2r|      s'     ##	   r"   zCategoricalCrossentropy.v3r7   r8   r;   c                 *    t          | ||||          S Nr8   rx   r8   s        r    $configure_CategoricalCrossentropy_v3r      s*     ##'   r"   c                   @   e Zd Zdddddddedeee                  deeeef                  dee         d	e	f
d
Z
dee         dee         deee         e	f         fdZdee         dee         dee         fdZdee         dee         de	fdZdee         de	fdZdS )SequenceCategoricalCrossentropyTNr7   r8   r9   r4   r5   r:   r;   c                F    t          d||||          | _        || _        d S )NFr8   )r3   ccr9   rC   s         r    r!   z(SequenceCategoricalCrossentropy.__init__   s6     *'!+
 
 
 #r"   r#   r$   r   c                 `    |                      ||          }|                     |          }||fS r   rb   )r   r#   r$   gradslosss        r    r*   z(SequenceCategoricalCrossentropy.__call__   s4     gv..''..d{r"   c                 .   d}t          |          t          |          k    rt          |          t          |          }g }t          ||          D ]A\  }}| j                            ||          }| j        r||z  }|                    |           B|S )Nz]Cannot calculate SequenceCategoricalCrossentropy loss: guesses and truths must be same length)rM   rR   zipr   r'   r9   rQ   )	r   r#   r$   rk   nd_scoresyhyd_yhs	            r    r'   z(SequenceCategoricalCrossentropy.get_grad   s     nw<<3v;;&&S//!LL&)) 	" 	"EB7##B**D~ 	OOD!!!!r"   c                 T    |                      |                     ||                    S r   )rc   r'   r)   s      r    r(   z(SequenceCategoricalCrossentropy.get_loss  s&     ''gv(F(FGGGr"   r   c                 N    d}|D ]}|| j                             |          z  } |S )Nr7   )r   rc   )r   r   r   grads       r    rc   z3SequenceCategoricalCrossentropy._get_loss_from_grad  s7     	6 	6DDG//555DDr"   )r.   r/   r0   rs   r   r	   rq   r   rP   rt   r!   r   ru   r
   r   r*   r'   r(   rc   r   r"   r    r   r      st        )-37$(!$# # # # &	#
  c3h0# SM# # # # #$)3;<N3O	tH~u$	%   )3;<N3O	h   H)H3;<N3OH	H H H H
();       r"   r   z"SequenceCategoricalCrossentropy.v1r9   r4   c                 $    t          | |          S )Nr   r   r   s     r    ,configure_SequenceCategoricalCrossentropy_v1r     s     +YeLLLLr"   z"SequenceCategoricalCrossentropy.v2r9   r4   r:   c                 &    t          | ||          S )Nr   r   r   s      r    ,configure_SequenceCategoricalCrossentropy_v2r     s"     +5Z   r"   z"SequenceCategoricalCrossentropy.v3c                 *    t          | ||||          S r~   r   r8   s        r    ,configure_SequenceCategoricalCrossentropy_v3r   %  s*     +#'   r"   c                   l    e Zd ZdddefdZdededeeef         fdZdededefd	Z	dededefd
Z
dS )
L2DistanceTr9   r9   c                    || _         d S r   r   )r   r9   s     r    r!   zL2Distance.__init__8  s    "r"   r#   r$   r   c                 Z    |                      ||          |                     ||          fS r   r&   r)   s      r    r*   zL2Distance.__call__;  r+   r"   c                     |j         |j         k    r"d|j          d|j          d}t          |          ||z
  }| j        r||j         d         z  }|S )N1Cannot calculate L2 distance: mismatched shapes: rg   rh   r   )rX   rR   r9   )r   r#   r$   rk   rl   s        r    r'   zL2Distance.get_grad>  sf    =FL((hgmhhY_YehhhCS//!v%
> 	7#gmA&66Jr"   c                     |j         |j         k    r"d|j          d|j          d}t          |          |                     ||          }|dz                                  S )Nr   rg   rh   ro   )rX   rR   r'   rp   )r   r#   r$   rk   re   s        r    r(   zL2Distance.get_lossG  sd    =FL((hgmhhY_YehhhCS//!--00
!!!r"   N)r.   r/   r0   rs   r!   r   r
   rt   r*   r'   r(   r   r"   r    r   r   7  s        ,0 # # #T # # # #N N( NuXu_?U N N N N ( x    " "( "u " " " " " "r"   r   zL2Distance.v1r   c                 "    t          |           S )Nr   )r   r   s    r    configure_L2Distancer   P  s    	****r"   c                       e Zd ZddddedefdZdeded	eeef         fd
Zdeded	efdZ	deded	efdZ
deded	efdZdS )CosineDistanceTFr9   ignore_zerosr9   r   c                "    || _         || _        d S r   r   )r   r9   r   s      r    r!   zCosineDistance.__init__V  s    "(r"   r#   r$   r   c                 Z    |                      ||          |                     ||          fS r   r&   r)   s      r    r*   zCosineDistance.__call__Z  r+   r"   c                 T   |j         |j         k    r"d|j          d|j          d}t          |          t          |          }|dz   }|dz   }|j                            |dd          }|j                            |dd          }||z  }	||z                      dd          |	z  }
|
S )N7Cannot calculate cosine similarity: mismatched shapes: rg   rh   :0yE>r   Taxiskeepdims)rX   rR   r   linalgnormrp   )r   r#   r$   rk   rY   r   r   norm_yhnorm_y	mul_normscosines              r    get_similarityzCosineDistance.get_similarity]  s    =FL((nGMnn_e_knnnCS//!g&&t^TM)..!d.;;D99f$	q&1t44y@r"   c                 &   |j         |j         k    r"d|j          d|j          d}t          |          t          |          }| j        r-|                    |                              d          dk    }|dz   }|dz   }|j                            |dd	          }|j                            |dd	          }	||	z  }
||z                      dd	          |
z  }||
z  |||d
z  z  z  z
  }| j        rd||<   | j        r||j         d         z  }| S )Nr   rg   rh   r   r   r   r   Tr   ro   )	rX   rR   r   r   absrp   r   r   r9   )r   r#   r$   rk   rY   zero_indicesr   r   r   r   r   r   r   s                r    r'   zCosineDistance.get_gradl  s8   =FL((nGMnn_e_knnnCS//! g&& 	;66&>>--1-55:Lt^TM)..!d.;;D99f$	q&1t44y@I&B!O"<= 	#!"D> 	+'-**Dur"   c                    |j         |j         k    r"d|j          d|j          d}t          |          t          |          }|                     ||          }|                    |dz
            }| j        r2|                    |                              d          dk    }d||<   | j        r||j         d         z  }|                                }|S )Nr   rg   rh   r   r   r   )rX   rR   r   r   r   r   rp   r9   )	r   r#   r$   rk   rY   r   lossesr   r   s	            r    r(   zCosineDistance.get_loss  s    =FL((nGMnn_e_knnnCS//!g&&$$Wf55
## 	%66&>>--1-55:L#$F< > 	/gmA..Fzz||r"   N)r.   r/   r0   rs   r!   r   r
   rt   r*   r   r'   r(   r   r"   r    r   r   U  s        ,0u ) ) )T ) ) ) ) )N N( NuXu_?U N N N Nh  U     ( x    4 ( u      r"   r   zCosineDistance.v1Fr   r   c                 $    t          | |          S )Nr   )r   r   s     r    configure_CosineDistancer     s     ILIIIIr"   c                 f    t          |           }|                    | j        d          }d||<   |S )NrE   rF   r   )r   rL   rX   )r#   rZ   rY   r_   s       r    rU   rU     s4    	'	"	"B777=7,,DDMKr"   c                     t          |          }|                    |j        d          }|5| j        dk    r
d|| |k    <   n |                    | d          }d|||k    <   |S )NrE   rF   r   r7   rH   r   )r   rL   rX   rW   argmax)r$   r#   r5   rY   r_   labelss         r    rV   rV     sv    	'	"	"B777=7,,D ;!,/D=()) YYvBY//F,/D=()Kr"   )r   r3   r   r   )0abcr   typingr   r   r   r   r   r	   r
   r   r   r   configr   typesr   r   utilr   r   r   r   r   r   IntsOrFloatsrP   rq   ru   r   r3   r   rs   ry   r|   rt   r   r   r   r   r   r   r   r   r   rU   rV   __all__r   r"   r    <module>r      sL                                      # # # # # # # # 2 2 2 2 2 2 2 2						VX%&68Xc]HSMIJ F F F F F7665%/0 F F F4w" w" w" w" w"d w" w" w"t -.. %)/3	   HSM" E#s(O,	
    /. -.. %)/3 $   HSM" E#s(O,	
     /. -.. %)/3 $    HSM" E#s(O,	
      /."2 2 2 2 2d 2 2 2j 566M M MM&.x}&=M$M M M 76M 566 %) $	   HSM" 	
 %   76 566 %)/3 $    HSM" E#s(O,	
   %   76"" " " " " " " "2 !!.2 + + +t +z + + + "!+@ @ @ @ @T @ @ @F $%%EJ J JJ-1JJ J J &%JH    8       r"   