
    ga                     0   d Z ddlmZmZmZ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.
    )CallableDictOptionalTupleN)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        Z/var/www/html/ai-engine/env/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.variance1   s    
 ~&Q66r   c                 4    | j                                         S )zE
        Returns the standard deviation of the distribution.
        )r$   sqrtr!   s    r   stddevzAffineTransformed.stddev8   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dimr0   s     r   
<listcomp>z0ParameterProjection.__init__.<locals>.<listcomp>F   s%    "\"\"\329[##>#>"\"\"\r   r6   )r   r   r1   r   
ModuleListvaluesprojr2   )r   r0   r1   r2   kwargsr   s    `   r   r   zParameterProjection.__init__A   se     	""6""" M"\"\"\"\(//J[J["\"\"\]]	$r   xc                 >    fd| j         D             } | j        | S )Nc                 &    g | ]} |          S r6   r6   )r8   r=   r?   s     r   r:   z/ParameterProjection.forward.<locals>.<listcomp>J   s!    :::DDGG:::r   )r=   r2   )r   r?   params_unboundeds    ` r   forwardzParameterProjection.forwardI   s/    ::::	:::t 011r   )r(   r)   r*   intr   strr   r   torchTensorr   rC   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   rL   r   s     r   r   zLambdaLayer.__init__P   s$     r   c                      | j         |g|R  S rK   )rL   )r   r?   argss      r   rC   zLambdaLayer.forwardT   s    t}Q&&&&&r   )r(   r)   r*   r   rC   r,   r-   s   @r   rI   rI   O   sG        ! ! ! ! !' ' ' ' ' ' 'r   rI   c                   T   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
ej                 de
ej                 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_classr0   r1      r9   r3   Nc                 J      _          fd j        D              _        d S )Nc                 2    i | ]}|j         |         z  S r6   )r1   )r8   kr9   r   s     r   
<dictcomp>z/DistributionOutput.__init__.<locals>.<dictcomp>_   s'    JJJqC$-"22JJJr   )r9   r1   )r   r9   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 )NrR   r9   rQ   r
   )r   
distr_argss     r   _base_distributionz%DistributionOutput._base_distributiona   s8    8q==*4*J776t6
CQGGGr   r   r   c                 h    |                      |          }|||S t          |||| j                  S )Nr   )rZ   r   r   )r   rY   r   r   distrs        r   distributionzDistributionOutput.distributiong   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.
        rR   r6   )r9   r!   s    r   event_shapezDistributionOutput.event_shapes   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_dimz   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   r6   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.
        )r0   r1   r2   )r/   r1   rI   r2   )r   r0   s     r   get_parameter_projectionz+DistributionOutput.get_parameter_projection   s0     ##]"4?33
 
 
 	
r   rN   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   rN   s     r   r2   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      @       @)rF   r&   square)r?   s    r   
squarepluszDistributionOutput.squareplus   s+     EJu|A4555<<r   )rR   NN)r(   r)   r*   type__annotations__rD   r   rE   r   rZ   r   rF   rG   r	   r]   r+   r   r_   r   floatrc   r   Modulere   r2   staticmethodrk   r6   r   r   rP   rP   X   s        38nK KC K K K K KH H H '+(,	
\ 
\ el#
\ %	
\
 

\ 
\ 
\ 
\ 4U 4 4 4 X4 %3 % % % X% %    X
C 
BI 
 
 
 
$ $ $ $ $ =el =u| = = = \= = =r   rP   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.
    rR   )dfr   r   r1   rQ   rt   r   r   c                 6   |                      |                              t          j        |j                  j                  }d|                      |          z   }|                    d          |                    d          |                    d          fS )Nri   rk   	clamp_minrF   finfodtypeepssqueeze)clsrt   r   r   s       r   r2   zStudentTOutput.domain_map   ss    u%%//EK0H0H0LMM3>>"%%%zz"~~s{{2b0A0AAAr   N)r(   r)   r*   __doc__r1   r   rE   rD   rn   r   rQ   rm   classmethodrF   rG   r2   r6   r   r   rs   rs      s           '(A>>Hd38n>>>''''BEL Bu| BEL B B B [B B Br   rs   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.
    rR   )r   r   r1   rQ   r   r   c                     |                      |                              t          j        |j                  j                  }|                    d          |                    d          fS Nrv   rw   )r}   r   r   s      r   r2   zNormalOutput.domain_map   sQ    u%%//EK0H0H0LMM{{2b 1 111r   N)r(   r)   r*   r~   r1   r   rE   rD   rn   r   rQ   rm   r   rF   rG   r2   r6   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ej                 deej                 d	efdZdS )NegativeBinomialOutputz6
    Negative Binomial distribution output class.
    rR   total_countlogitsr1   rQ   r   r   c                     |                      |          }|                    d          |                    d          fS r   )rk   r|   )r}   r   r   s      r   r2   z!NegativeBinomialOutput.domain_map   s8    nn[11""2&&r(:(:::r   r3   c                     |\  }}| j         dk    r|                     ||          S t          |                     ||          d          S )NrR   r   rX   )r   rY   r   r   s       r   rZ   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 rK   )logrZ   )r   rY   r   r   r   r   s         r   r]   z#NegativeBinomialOutput.distribution   s?     )Veiikk!F&&V'<===r   rl   )r(   r)   r*   r~   r1   r   rE   rD   rn   r   rQ   rm   r   rF   rG   r2   r	   rZ   r   r]   r6   r   r   r   r      s           01A>>Hd38n>>>////;U\ ;5< ; ; ; [;c c c c c _c	> 	>'5	>EMelE[	>		> 	> 	> 	> 	> 	>r   r   )r~   typingr   r   r   r   rF   r   torch.distributionsr   r	   r
   r   r   r   r   r   rp   r/   rI   rP   rs   r   r   r6   r   r   <module>r      s     3 2 2 2 2 2 2 2 2 2 2 2                        $ $ $ $ $/ $ $ $: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   