U
    ,-e`                     @   s   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 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DictOptionalTupleN)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 krdn|| _ |d k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__ _/var/www/html/Darija-Ai-Train/env/lib/python3.8/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 | || _t fdd| D | _|| _d S )Nc                    s   g | ]}t  |qS r   )r   ZLinear).0dimr)   r   r   
<listcomp>E   s     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-   r2   r5   r   r   r0   I   s     z/ParameterProjection.forward.<locals>.<listcomp>)r2   r+   )r   r5   Zparams_unboundedr   r6   r   forwardH   s    zParameterProjection.forward)r#   r$   r%   intr   strr   r   torchTensorr   r7   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 |f| S r=   )r>   )r   r5   argsr   r   r   r7   S   s    zLambdaLayer.forward)r#   r$   r%   r   r7   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>^   s      z/DistributionOutput.__init__.<locals>.<dictcomp>)r.   r*   )r   r.   r   rD   r   r   \   s    zDistributionOutput.__init__c                 C   s(   | j dkr| j| S t| j| dS d S )NrB   r.   rA   r	   )r   
distr_argsr   r   r   _base_distribution`   s    

z%DistributionOutput._base_distributionr   r   r,   c                 C   s4   |  |}|d kr|d kr|S t|||| jdS d S )Nr   )rH   r   r   )r   rG   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.
        rB   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.
        )lenrL   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_mapr4   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squarer6   r   r   r   
squareplus   s    zDistributionOutput.squareplus)rB   )NN)r#   r$   r%   type__annotations__r8   r   r9   r   rH   r   r:   r;   r   rJ   r&   r   rL   r   floatrN   r   ModulerO   r+   staticmethodrR   r   r   r   r   r@   W   s,   
	  
r@   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.
    rB   )dfr   r   r*   rA   c                 C   sD   |  |t|jj}d|  | }|d|d|dfS )NrQ   rR   Z	clamp_minr:   ZfinfoZdtypeZepssqueeze)clsrY   r   r   r   r   r   r+      s    zStudentTOutput.domain_mapN)r#   r$   r%   __doc__r*   r   r9   r8   rT   r   rA   rS   classmethodr:   r;   r+   r   r   r   r   rX      s
   
rX   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.
    rB   )r   r   r*   rA   c                 C   s.   |  |t|jj}|d|dfS NrZ   r[   )r]   r   r   r   r   r   r+      s    zNormalOutput.domain_mapN)r#   r$   r%   r^   r*   r   r9   r8   rT   r   rA   rS   r_   r:   r;   r+   r   r   r   r   r`      s
   
r`   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.
    rB   total_countlogitsr*   rA   c                 C   s   |  |}|d|dfS ra   )rR   r\   )r]   rd   re   r   r   r   r+      s    
z!NegativeBinomialOutput.domain_maprK   c                 C   s8   |\}}| j dkr | j||dS t| j||ddS d S )NrB   rc   rF   )r   rG   rd   re   r   r   r   rH      s    
z)NegativeBinomialOutput._base_distributionNrI   c                 C   s*   |\}}|d k	r||  7 }| ||fS r=   )logrH   )r   rG   r   r   rd   re   r   r   r   rJ      s    z#NegativeBinomialOutput.distribution)NN)r#   r$   r%   r^   r*   r   r9   r8   rT   r
   rA   rS   r_   r:   r;   r+   r   rH   r   rJ   r   r   r   r   rb      s   
    rb   )r^   typingr   r   r   r   r:   r   Ztorch.distributionsr   r   r	   r
   r   r   r   r   rV   r(   r<   r@   rX   r`   rb   r   r   r   r   <module>   s   $	M