
    bi8                     $   d Z ddlmZ ddlZddlmZ ddlmZmZmZm	Z	m
Z
mZmZ  G d de          Z G d d	ej                  Z G d
 dej                  Z G d d          Z G d de          Z G d de          Z G d de          ZdS )z:
Time series distributional output classes and utilities.
    )CallableN)nn)AffineTransformDistributionIndependentNegativeBinomialNormalStudentTTransformedDistributionc                   h     e Zd Zddef fdZed             Zed             Zed             Z xZ	S )	AffineTransformedNr   base_distributionc                     |dn|| _         |dn|| _        t                                          |t	          | j        | j         |          g           d S )Ng      ?        locscale	event_dim)r   r   super__init__r   )selfr   r   r   r   	__class__s        Y/root/projects/butler/venv/lib/python3.11/site-packages/transformers/time_series_utils.pyr   zAffineTransformed.__init__#   s[    !MSSu
+333*_QUQ[gp-q-q-q,rsssss    c                 :    | j         j        | j        z  | j        z   S )z7
        Returns the mean of the distribution.
        )	base_distmeanr   r   r   s    r   r   zAffineTransformed.mean)   s    
 ~"TZ/$(::r   c                 0    | j         j        | j        dz  z  S )z;
        Returns the variance of the distribution.
           )r   variancer   r   s    r   r!   zAffineTransformed.variance0   s    
 ~&Q66r   c                 4    | j                                         S )zE
        Returns the standard deviation of the distribution.
        )r!   sqrtr   s    r   stddevzAffineTransformed.stddev7   s    
 }!!###r   )NNr   )
__name__
__module____qualname__r   r   propertyr   r!   r$   __classcell__r   s   @r   r   r   "   s        t t, t t t t t t ; ; X; 7 7 X7 $ $ X$ $ $ $ $r   r   c            	            e Zd Zdedeeef         dedeej	                 f         ddf fdZ
dej	        deej	                 fd	Z xZS )
ParameterProjectionin_featuresargs_dim
domain_map.returnNc                      t                      j        di | || _        t          j        fd|                                D                       | _        || _        d S )Nc                 :    g | ]}t          j        |          S  )r   Linear).0dimr-   s     r   
<listcomp>z0ParameterProjection.__init__.<locals>.<listcomp>E   s%    "\"\"\329[##>#>"\"\"\r   r3   )r   r   r.   r   
ModuleListvaluesprojr/   )r   r-   r.   r/   kwargsr   s    `   r   r   zParameterProjection.__init__@   se     	""6""" M"\"\"\"\(//J[J["\"\"\]]	$r   xc                 >    fd| j         D             } | j        | S )Nc                 &    g | ]} |          S r3   r3   )r5   r:   r<   s     r   r7   z/ParameterProjection.forward.<locals>.<listcomp>I   s!    :::DDGG:::r   )r:   r/   )r   r<   params_unboundeds    ` r   forwardzParameterProjection.forwardH   s/    ::::	:::t 011r   )r%   r&   r'   intdictstrr   tupletorchTensorr   r@   r)   r*   s   @r   r,   r,   ?   s        %%*.sCx.%FNsTYZ_ZfTgOgFh%	% % % % % %2 2%*= 2 2 2 2 2 2 2 2r   r,   c                   $     e Zd Z fdZd Z xZS )LambdaLayerc                 V    t                                                       || _        d S N)r   r   function)r   rK   r   s     r   r   zLambdaLayer.__init__O   s$     r   c                      | j         |g|R  S rJ   )rK   )r   r<   argss      r   r@   zLambdaLayer.forwardS   s    t}Q&&&&&r   )r%   r&   r'   r   r@   r)   r*   s   @r   rH   rH   N   sG        ! ! ! ! !' ' ' ' ' ' 'r   rH   c                   H   e Zd ZU eed<   eed<   eeef         ed<   ddeddfdZd	 Z		 	 dd
e
j        dz  de
j        dz  defdZedefd            Zedefd            Zedefd            Zdedej        fdZde
j        fdZede
j        de
j        fd            ZdS )DistributionOutputdistribution_classr-   r.      r6   r0   Nc                 J      _          fd j        D              _        d S )Nc                 2    i | ]}|j         |         z  S r3   )r.   )r5   kr6   r   s     r   
<dictcomp>z/DistributionOutput.__init__.<locals>.<dictcomp>^   s'    JJJqC$-"22JJJr   )r6   r.   )r   r6   s   ``r   r   zDistributionOutput.__init__\   s/    JJJJJDMJJJr   c                 \    | j         dk    r
 | j        | S t           | j        | d          S )NrQ   r6   rP   r   )r   
distr_argss     r   _base_distributionz%DistributionOutput._base_distribution`   s8    8q==*4*J776t6
CQGGGr   r   r   c                 h    |                      |          }|||S t          |||| j                  S )Nr   )rY   r   r   )r   rX   r   r   distrs        r   distributionzDistributionOutput.distributionf   s>     ''
33;5=L$U5DN[[[[r   c                 ,    | j         dk    rdn| j         fS )zo
        Shape of each individual event contemplated by the distributions that this object constructs.
        rQ   r3   )r6   r   s    r   event_shapezDistributionOutput.event_shaper   s    
 X]]rr3r   c                 *    t          | j                  S )z
        Number of event dimensions, i.e., length of the `event_shape` tuple, of the distributions that this object
        constructs.
        )lenr^   r   s    r   r   zDistributionOutput.event_dimy   s     4#$$$r   c                     dS )z
        A float that will have a valid numeric value when computing the log-loss of the corresponding distribution. By
        default 0.0. This value will be used when padding data series.
        r   r3   r   s    r   value_in_supportz#DistributionOutput.value_in_support   s	     sr   c                 T    t          || j        t          | j                            S )z~
        Return the parameter projection layer that maps the input to the appropriate parameters of the distribution.
        )r-   r.   r/   )r,   r.   rH   r/   )r   r-   s     r   get_parameter_projectionz+DistributionOutput.get_parameter_projection   s0     ##]"4?33
 
 
 	
r   rM   c                     t                      )a  
        Converts arguments to the right shape and domain. The domain depends on the type of distribution, while the
        correct shape is obtained by reshaping the trailing axis in such a way that the returned tensors define a
        distribution of the right event_shape.
        )NotImplementedError)r   rM   s     r   r/   zDistributionOutput.domain_map   s     "###r   r<   c                 `    | t          j        t          j        |           dz             z   dz  S )z
        Helper to map inputs to the positive orthant by applying the square-plus operation. Reference:
        https://twitter.com/jon_barron/status/1387167648669048833
        g      @       @)rE   r#   square)r<   s    r   
squarepluszDistributionOutput.squareplus   s+     EJu|A4555<<r   )rQ   NN)r%   r&   r'   type__annotations__rA   rB   rC   r   rY   rE   rF   r   r\   r(   rD   r^   r   floatrb   r   Modulerd   r/   staticmethodrj   r3   r   r   rO   rO   W   s        38nK KC K K K K KH H H $(%)	
\ 
\ \D 
\ |d"	
\
 

\ 
\ 
\ 
\ 4U 4 4 4 X4 %3 % % % X% %    X
C 
BI 
 
 
 
$ $ $ $ $ =el =u| = = = \= = =r   rO   c                       e Zd ZU dZddddZeeef         ed<   e	Z
eed<   edej        dej        dej        fd	            Zd
S )StudentTOutputz.
    Student-T distribution output class.
    rQ   )dfr   r   r.   rP   rs   r   r   c                 6   |                      |                              t          j        |j                  j                  }d|                      |          z   }|                    d          |                    d          |                    d          fS )Nrh   rj   	clamp_minrE   finfodtypeepssqueeze)clsrs   r   r   s       r   r/   zStudentTOutput.domain_map   ss    u%%//EK0H0H0LMM3>>"%%%zz"~~s{{2b0A0AAAr   N)r%   r&   r'   __doc__r.   rB   rC   rA   rm   r
   rP   rl   classmethodrE   rF   r/   r3   r   r   rr   rr      s           '(A>>Hd38n>>>''''BEL Bu| BEL B B B [B B Br   rr   c                   z    e Zd ZU dZdddZeeef         ed<   e	Z
eed<   edej        dej        fd            Zd	S )
NormalOutputz+
    Normal distribution output class.
    rQ   )r   r   r.   rP   r   r   c                     |                      |                              t          j        |j                  j                  }|                    d          |                    d          fS Nru   rv   )r|   r   r   s      r   r/   zNormalOutput.domain_map   sQ    u%%//EK0H0H0LMM{{2b 1 111r   N)r%   r&   r'   r}   r.   rB   rC   rA   rm   r	   rP   rl   r~   rE   rF   r/   r3   r   r   r   r      s           ()155Hd38n555%%%%2U\ 2%, 2 2 2 [2 2 2r   r   c                       e Zd ZU dZdddZeeef         ed<   e	Z
eed<   edej        dej        fd            Zd	efd
Z	 ddej        dz  dej        dz  d	efdZdS )NegativeBinomialOutputz6
    Negative Binomial distribution output class.
    rQ   total_countlogitsr.   rP   r   r   c                     |                      |          }|                    d          |                    d          fS r   )rj   r{   )r|   r   r   s      r   r/   z!NegativeBinomialOutput.domain_map   s8    nn[11""2&&r(:(:::r   r0   c                     |\  }}| j         dk    r|                     ||          S t          |                     ||          d          S )NrQ   r   rW   )r   rX   r   r   s       r   rY   z)NegativeBinomialOutput._base_distribution   sU    (V8q==**{6*RRRt66;W]6^^`abbbr   Nr   r   c                 l    |\  }}|||                                 z  }|                     ||f          S rJ   )logrY   )r   rX   r   r   r   r   s         r   r\   z#NegativeBinomialOutput.distribution   s?     )Veiikk!F&&V'<===r   rk   )r%   r&   r'   r}   r.   rB   rC   rA   rm   r   rP   rl   r~   rE   rF   r/   r   rY   r\   r3   r   r   r   r      s           01A>>Hd38n>>>////;U\ ;5< ; ; ; [;c c c c c Y]	> 	>$|d2	>BG,QUBU	>		> 	> 	> 	> 	> 	>r   r   )r}   collections.abcr   rE   r   torch.distributionsr   r   r   r   r	   r
   r   r   ro   r,   rH   rO   rr   r   r   r3   r   r   <module>r      s    % $ $ $ $ $                        $ $ $ $ $/ $ $ $:2 2 2 2 2") 2 2 2' ' ' ' '") ' ' 'J= J= J= J= J= J= J= J=ZB B B B B' B B B2 2 2 2 2% 2 2 2 >  >  >  >  >/  >  >  >  >  >r   