a
    hE                     @   s   d Z ddl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 deZG dd	 d	ejZG d
d dejZG dd dZG dd deZG dd deZG dd deZdS )z:
Time series distributional output classes and utilities.
    )CallableOptionalN)nn)AffineTransformDistributionIndependentNegativeBinomialNormalStudentTTransformedDistributionc                       sH   e Zd Zded fddZedd Zedd	 Zed
d Z  Z	S )AffineTransformedNr   )base_distributionc                    sF   |d u rdn|| _ |d u rdn|| _t |t| j| j |dg d S )Ng      ?        locscale	event_dim)r   r   super__init__r   )selfr   r   r   r   	__class__ Z/var/www/html/assistant/venv/lib/python3.9/site-packages/transformers/time_series_utils.pyr   #   s    zAffineTransformed.__init__c                 C   s   | j j| j | j S )z7
        Returns the mean of the distribution.
        )	base_distmeanr   r   r   r   r   r   r   )   s    zAffineTransformed.meanc                 C   s   | j j| jd  S )z;
        Returns the variance of the distribution.
           )r   variancer   r   r   r   r   r   0   s    zAffineTransformed.variancec                 C   s
   | j  S )zE
        Returns the standard deviation of the distribution.
        )r   sqrtr   r   r   r   stddev7   s    zAffineTransformed.stddev)NNr   )
__name__
__module____qualname__r   r   propertyr   r   r    __classcell__r   r   r   r   r   "   s   

r   c                       sV   e Zd Zeeeef edeej	 f dd fddZ
ej	eej	 dddZ  ZS )	ParameterProjection.N)in_featuresargs_dim
domain_mapreturnc                    s@   t  jf i | || _t fdd| D | _|| _d S )Nc                    s   g | ]}t  |qS r   )r   ZLinear).0dimr'   r   r   
<listcomp>E       z0ParameterProjection.__init__.<locals>.<listcomp>)r   r   r(   r   Z
ModuleListvaluesprojr)   )r   r'   r(   r)   kwargsr   r-   r   r   @   s    zParameterProjection.__init__xr*   c                    s    fdd| j D }| j| S )Nc                    s   g | ]}| qS r   r   )r+   r1   r4   r   r   r.   I   r/   z/ParameterProjection.forward.<locals>.<listcomp>)r1   r)   )r   r4   Zparams_unboundedr   r5   r   forwardH   s    zParameterProjection.forward)r!   r"   r#   intdictstrr   tupletorchTensorr   r6   r%   r   r   r   r   r&   ?   s   r&   c                       s$   e Zd Z fddZdd Z  ZS )LambdaLayerc                    s   t    || _d S N)r   r   function)r   r?   r   r   r   r   O   s    
zLambdaLayer.__init__c                 G   s   | j |g|R  S r>   )r?   )r   r4   argsr   r   r   r6   S   s    zLambdaLayer.forward)r!   r"   r#   r   r6   r%   r   r   r   r   r=   N   s   r=   c                   @   s   e Zd ZU eed< eed< eeef ed< deddddZd	d
 Z	de
ej e
ej edddZeedddZeedddZeedddZeejdddZejdddZeejejdddZdS ) DistributionOutputdistribution_classr'   r(      N)r,   r*   c                    s"    _  fddjD _d S )Nc                    s   i | ]}| j |  qS r   )r(   )r+   kr,   r   r   r   
<dictcomp>^   r/   z/DistributionOutput.__init__.<locals>.<dictcomp>)r,   r(   )r   r,   r   rE   r   r   \   s    zDistributionOutput.__init__c                 C   s(   | j dkr| j| S t| j| dS d S )NrC   r,   rB   r   )r   
distr_argsr   r   r   _base_distribution`   s    

z%DistributionOutput._base_distributionr   r   r*   c                 C   s4   |  |}|d u r|d u r|S t|||| jdS d S )Nr   )rI   r   r   )r   rH   r   r   Zdistrr   r   r   distributionf   s    
zDistributionOutput.distributionr*   c                 C   s   | j dkrdS | j fS )zo
        Shape of each individual event contemplated by the distributions that this object constructs.
        rC   r   )r,   r   r   r   r   event_shaper   s    zDistributionOutput.event_shapec                 C   s
   t | jS )z
        Number of event dimensions, i.e., length of the `event_shape` tuple, of the distributions that this object
        constructs.
        )lenrM   r   r   r   r   r   y   s    zDistributionOutput.event_dimc                 C   s   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   r   r   r   r   r   value_in_support   s    z#DistributionOutput.value_in_support)r'   r*   c                 C   s   t || jt| jdS )z~
        Return the parameter projection layer that maps the input to the appropriate parameters of the distribution.
        )r'   r(   r)   )r&   r(   r=   r)   )r   r'   r   r   r   get_parameter_projection   s
    z+DistributionOutput.get_parameter_projection)r@   c                 G   s
   t  dS )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.
        N)NotImplementedError)r   r@   r   r   r   r)      s    zDistributionOutput.domain_mapr3   c                 C   s   | t t | d  d 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      @       @)r;   r   Zsquarer5   r   r   r   
squareplus   s    zDistributionOutput.squareplus)rC   )NN)r!   r"   r#   type__annotations__r7   r8   r9   r   rI   r   r;   r<   r   rK   r$   r:   rM   r   floatrO   r   ModulerP   r)   staticmethodrS   r   r   r   r   rA   W   s,   
	  
rA   c                   @   sV   e Zd ZU dZddddZeeef ed< e	Z
eed< eejejejdddZdS )	StudentTOutputz.
    Student-T distribution output class.
    rC   )dfr   r   r(   rB   c                 C   sD   |  |t|jj}d|  | }|d|d|dfS )NrR   rS   Z	clamp_minr;   ZfinfoZdtypeepssqueeze)clsrZ   r   r   r   r   r   r)      s    zStudentTOutput.domain_mapN)r!   r"   r#   __doc__r(   r8   r9   r7   rU   r
   rB   rT   classmethodr;   r<   r)   r   r   r   r   rY      s
   
rY   c                   @   sP   e Zd ZU dZdddZeeef ed< e	Z
eed< eejejdddZdS )	NormalOutputz+
    Normal distribution output class.
    rC   )r   r   r(   rB   c                 C   s.   |  |t|jj}|d|dfS Nr[   r\   )r_   r   r   r   r   r   r)      s    zNormalOutput.domain_mapN)r!   r"   r#   r`   r(   r8   r9   r7   rU   r	   rB   rT   ra   r;   r<   r)   r   r   r   r   rb      s
   
rb   c                   @   s~   e Zd ZU dZdddZeeef ed< e	Z
eed< eejejdddZedd	d
Zdeej eej edddZdS )NegativeBinomialOutputz6
    Negative Binomial distribution output class.
    rC   total_countlogitsr(   rB   c                 C   s   |  |}|d|dfS rc   )rS   r^   )r_   rf   rg   r   r   r   r)      s    
z!NegativeBinomialOutput.domain_maprL   c                 C   s8   |\}}| j dkr | j||dS t| j||ddS d S )NrC   re   rG   )r   rH   rf   rg   r   r   r   rI      s    
z)NegativeBinomialOutput._base_distributionNrJ   c                 C   s*   |\}}|d ur||  7 }| ||fS r>   )logrI   )r   rH   r   r   rf   rg   r   r   r   rK      s    z#NegativeBinomialOutput.distribution)NN)r!   r"   r#   r`   r(   r8   r9   r7   rU   r   rB   rT   ra   r;   r<   r)   r   rI   r   rK   r   r   r   r   rd      s   
 rd   )r`   typingr   r   r;   r   Ztorch.distributionsr   r   r   r   r	   r
   r   r   rW   r&   r=   rA   rY   rb   rd   r   r   r   r   <module>   s   $	M