
    {-j+&                     x    d dl Zd dlmZ d dlmZ d dlmZ d dlm	Z	 ddl
mZ g Ze	 	 	 	 	 	 	 	 	 dd
            ZdS )    N)static_only)LayerHelper)	ParamAttr)Assign   )check_variable_and_dtypeuniformFc                   # t          d&i t                      #t          | dddgd           t          |ddgd           | j        dk    rt	          d| j         d	          | j        d
         }|j        d
         }#                    #j        ||gd| j                  }i }#j	        r*#                    #j	        |d
gd| j                  }||d<   #
                    | j                  }#
                    | j                  }#
                    |j                  }| |d<   ||d<   ||d<   ||ng |d<   |dk    rd}n|dk    rd
}n|dk    r|	J |}dg|z  }dg|z  }g }g }t          |          D ]Y}|	|         |z  }|dz
  dk    r|                    ||f           .d|z
  dk    r|                    ||f           O|||<   d||<   Zt          |          rt          |          r|                    d          }|                    d          }|d         }|d
         }|d
         ||d         <   |||d         <   |d
         |d
         z   d
z
  }|dz
  dk    r|                    ||f           n+d|z
  dk    r|                    ||f           n
|||<   d||<   t          |          rt          |          t          |          r+|                    d          }d||d         <   d||d         <   t          |          r+|                    d          }d||d         <   d||d         <   #fd}  | t          j        |	                              d                    |d<    | t          j        |                              d                    |d<    | t          j        |                              d                    |d<   d}nt%          d           |d!}nt'          |          }|}!t)          d"           t'          |          ||
|||!d#}"#                    d||||d$|"%           ||d
z   z  S )'a  
    :api_attr: Static Graph

    Compute and return the noise-contrastive estimation training loss. See `Noise-contrastive estimation: A new estimation principle
    for unnormalized statistical models <http://www.jmlr.org/proceedings/papers/v9/gutmann10a/gutmann10a.pdf>`_.
    By default this operator uses a uniform distribution for sampling.

    Args:
        input (Tensor): Input tensor, 2-D tensor with shape [batch_size, dim],
            and data type is float32 or float64.
        label (Tensor): Input label, 2-D tensor with shape [batch_size, num_true_class],
            and data type is int64.
        num_total_classes (int): Total number of classes in all samples.
        sample_weight (Tensor|None): A Tensor of shape [batch_size, 1]
            storing a weight for each sample. The default weight for each
            sample is 1.0.
        param_attr (ParamAttr|None): To specify the weight parameter attribute.
            Default: None, which means the default weight parameter property is
            used. See usage for details in :ref:`api_paddle_ParamAttr` .
        bias_attr (ParamAttr|None): To specify the bias parameter attribute.
            Default: None, which means the default bias parameter property is
            used. See usage for details in :ref:`api_paddle_ParamAttr` .
        num_neg_samples (int): The number of negative classes. The default value is 10.
        name(str|None): For detailed information, please refer to
            :ref:`api_guide_Name` . Usually name is no need to set and None by default.
        sampler (str, optional): The sampler used to sample class from negative classes.
                       It can be 'uniform', 'log_uniform' or 'custom_dist'.
                       default: 'uniform'.
        custom_dist (nd.array|None): A numpy ndarray with size=num_total_classes.
                       It is used when sampler is set to 'custom_dist'.
                       custom_dist[i] is the probability of i-th class to be sampled.
                       default: None.
        seed (int, optional): The seed used in sampler. Default 0, means no random seed.
        is_sparse(bool, optional): The flag indicating whether to use sparse update,
            the weight@GRAD and bias@GRAD will be changed to SelectedRows. Default False.

    Returns:
        Tensor: The output nce loss.

    Examples:
        .. code-block:: python

            >>> import paddle
            >>> import numpy as np

            >>> paddle.enable_static()

            >>> window_size = 5
            >>> words = []
            >>> for i in range(window_size):
            ...     words.append(paddle.static.data(
            ...         name='word_{0}'.format(i), shape=[-1, 1], dtype='int64'))

            >>> dict_size = 10000
            >>> label_word = int(window_size / 2) + 1

            >>> embs = []
            >>> for i in range(window_size):
            ...     if i == label_word:
            ...         continue
            ...
            ...     emb = paddle.static.nn.embedding(input=words[i], size=[dict_size, 32],
            ...                         param_attr='embed', is_sparse=True)
            ...     embs.append(emb)

            >>> embs = paddle.concat(x=embs, axis=1)                # concat from 4 * [(-1, 1, 32)] to (-1, 4, 32)
            >>> embs = paddle.reshape(x=embs, shape=(-1, 4 * 32))   # reshape to (batch_size = -1, dim = 4*32)
            >>> loss = paddle.static.nn.nce(input=embs, label=words[label_word],
            ...             num_total_classes=dict_size, param_attr='nce.w_0',
            ...             bias_attr='nce.b_0')

            # or use custom distribution
            >>> dist = np.array([0.05,0.5,0.1,0.3,0.05])
            >>> loss = paddle.static.nn.nce(input=embs, label=words[label_word],
            ...         num_total_classes=5, param_attr='nce.w_1',
            ...         bias_attr='nce.b_1',
            ...         num_neg_samples=3,
            ...         sampler="custom_dist",
            ...         custom_dist=dist)
    nceinputfloat32float64labelint64   z,The rank of `input` must be 2, but received .   F)attrshapeis_biasdtypeTBias)r   InputLabelWeightNSampleWeightr	   r   log_uniformcustom_distg      ?c                                          t                      | j        | j        t	          |                     }d|_        |S )N)r   r   r   default_initializerT)create_parameterr   r   r   r   stop_gradient)numpy_arrayrethelpers     U/var/www/html/banglarbhumi/venv/lib/python3.11/site-packages/paddle/static/nn/loss.py_init_by_numpy_arrayz!nce.<locals>._init_by_numpy_array   sJ    ))[[!'!'$*;$7$7	 *  C !%CJ    CustomDistProbsint32CustomDistAliasCustomDistAliasProbszUnsupported sampler type.
   zWWith sparse mode, if your models has only small parameter prefetch may cause speed down)num_total_classesnum_neg_samplesseedsampler	is_sparseremote_prefetch)CostSampleLogitsSampleLabels)typeinputsoutputsattrs)r   )r   localsr   ndim
ValueErrorr   r"   
param_attrr   	bias_attr"create_variable_for_type_inferencerangeappendlenpopnparrayastype	Exceptionintprint	append_op)$r   r   r/   sample_weightr?   r@   r0   namer2   r   r1   r3   dimnum_true_classwr9   bcostsample_logitssample_labelscustom_dist_lenalias_probs_alias_bigslittlesinormal_probbiglittlebig_idxbig_probbig_leftr(   r4   r;   r&   s$                                      @r'   r   r       sg   ~ ++&((++FUGi-CUKKKUGgY>>>zQH5:HHH
 
 	
 +a.C[^N #&k	 	  	 	A F ##!$a(+	 $ 
 
 v445;4GGD==EK=PPM==EK=PPMF7OF7OF8.;.G]]RF>)	M	!	!	M	!	!&&&+s_,&'' 	 	A%a.?:KS 1$$Q,----{"Q&&;/0000"-Qq		$ii 	%CLL 	%((1++C[[^^F!fG1vH&,QiL# 'F6!91vq	)A-H#~!!Wh/0000x!##23333(0W%"$w! $ii 	%CLL 	%$ t99 	 ((1++C#&LQ F3q6Nw<< 	#[[^^F&)L# "F6!9	 	 	 	 	 %9$8H[!!((33%
 %
 ! %9$8HV##G,,%
 %
 ! *>)=H\"")))44*
 *
%& 3444o..O	a  
 !!233** E ))
 

   	 	 	 ?Q&''r)   )	NNNNNr	   Nr   F)numpyrF   paddle.base.frameworkr   paddle.base.layer_helperr   paddle.base.param_attrr   paddle.nn.initializerr   base.data_feederr   __all__r    r)   r'   <module>rj      s        - - - - - - 1 0 0 0 0 0 , , , , , , ( ( ( ( ( ( 8 8 8 8 8 8
 
 		
f( f( f( f( f( f(r)   