o
    Me¾  ã                   @   sH   d dl mZ d dlZd dlZ	 dgZdd„ Zdd„ ZG dd„ deƒZ	dS )	é    )Úprint_functionNÚWeightedAveragec                 C   s*   t | tƒpt | tƒpt | tjƒo| jdkS )N)é   )Ú
isinstanceÚintÚfloatÚnpÚndarrayÚshape©Úvar© r   úDD:\Projects\ConvertPro\env\Lib\site-packages\paddle/fluid/average.pyÚ_is_number_   s
   ÿÿr   c                 C   s   t | ƒp	t| tjƒS ©N)r   r   r   r	   r   r   r   r   Ú_is_number_or_matrix_$   s   r   c                   @   s0   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
S )r   aZ  
    Calculate weighted average.

    The average calculating is accomplished via Python totally. 
    They do not change Paddle's Program, nor do anything to
    modify NN model's configuration. They are completely 
    wrappers of Python functions.

    Examples:
        .. code-block:: python

            import paddle.fluid as fluid
            avg = fluid.average.WeightedAverage()
            avg.add(value=2.0, weight=1)
            avg.add(value=4.0, weight=2)
            avg.eval()

            # The result is 3.333333333.
            # For (2.0 * 1 + 4.0 * 2) / (1 + 2) = 3.333333333
    c                 C   s    t  d| jj t¡ |  ¡  d S )Nz@The %s is deprecated, please use fluid.metrics.Accuracy instead.)ÚwarningsÚwarnÚ	__class__Ú__name__ÚWarningÚreset©Úselfr   r   r   Ú__init__>   s   ÿþzWeightedAverage.__init__c                 C   s   d | _ d | _d S r   )Ú	numeratorÚdenominatorr   r   r   r   r   D   s   
zWeightedAverage.resetc                 C   sl   t |ƒstdƒ‚t|ƒstdƒ‚| jd u s| jd u r$|| | _|| _d S |  j|| 7  _|  j|7  _d S )Nz<The 'value' must be a number(int, float) or a numpy ndarray.z*The 'weight' must be a number(int, float).)r   Ú
ValueErrorr   r   r   )r   ÚvalueÚweightr   r   r   ÚaddH   s   ÿ

zWeightedAverage.addc                 C   s(   | j d u s
| jd u rtdƒ‚| j | j S )Nz3There is no data to be averaged in WeightedAverage.)r   r   r   r   r   r   r   ÚevalV   s
   ÿzWeightedAverage.evalN)r   Ú
__module__Ú__qualname__Ú__doc__r   r   r    r!   r   r   r   r   r   (   s    )
Ú
__future__r   Únumpyr   r   Ú__all__r   r   Úobjectr   r   r   r   r   Ú<module>   s   	