
    ёi+&                     r    S SK rS SKJr  S SKJr  S SKJr  S SKJ	r	  SSK
Jr  / r\         S	S j5       rg)
    N)static_only)LayerHelper)	ParamAttr)Assign   )check_variable_and_dtypec                   ^# [        S%0 [        5       D6m#[        U SSS/S5        [        USS/S5        U R                  S:w  a  [	        SU R                   S	35      eU R
                  S
   nUR
                  S
   nT#R                  T#R                  X,/SU R                  S9n0 nT#R                  (       a-  T#R                  T#R                  US
/SU R                  S9nUUS'   T#R                  U R                  S9nT#R                  U R                  S9nT#R                  UR                  S9nXS'   XS'   XS'   Ub  UO/ US'   US:X  a  SnGO[US:X  a  S
nGOQUS:X  Ga?  U	c   eUnS/U-  nS/U-  n/ n/ n[        U5       HQ  nU	U   U-  nUS-
  S:  a  UR                  UU45        M)  SU-
  S:  a  UR                  UU45        MG  UUU'   SUU'   MS     [        U5      (       a  [        U5      (       a  UR                  S5      nUR                  S5      nUS   nUS
   nUS
   UUS   '   UUUS   '   US
   US
   -   S
-
  nUS-
  S:  a  UR                  UU45        O'SU-
  S:  a  UR                  UU45        O
UUU'   SUU'   [        U5      (       a  [        U5      (       a  M  [        U5      (       a!  UR                  S5      nSUUS   '   SUUS   '   [        U5      (       a!  UR                  S5      nSUUS   '   SUUS   '   U#4S jn U " [        R                   " U	5      R#                  S5      5      US'   U " [        R                   " U5      R#                  S5      5      US'   U " [        R                   " U5      R#                  S5      5      US'   SnO[%        S5      eUc  S nO['        U5      nUn![)        S!5        ['        U5      UU
UUU!S".n"T#R+                  SUUUUS#.U"S$9  UU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WeightSampleWeightuniformr   log_uniformcustom_distg      ?c                    > TR                  [        5       U R                  U R                  [	        U 5      S9nSUl        U$ )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.13/site-packages/paddle/static/nn/loss.py_init_by_numpy_array!nce.<locals>._init_by_numpy_array   sF    ))[!''!''$*;$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   r0   sample_weightr@   rA   r1   namer3   r   r2   r4   dimnum_true_classwr:   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(   r5   r<   r&   s$                                      @r'   r
   r
       su   ~ +&(+FUGi-CUKUGgY>zzQ:5::,aH
 	
 ++a.C[[^N &kk	 	  	A F##!!$a(++	 $ 
 v445;;4GD==EKK=PM==EKK=PM7O7O8.;.G]RF>)	M	!	M	!&&&+s_,&'A%a.?:KS 1$Q,-{"Q&;/0"-Qq	 ( $iiCLL((1+C[[^F!fG1vH&,QiL# 'F6!91vq	)A-H#~!Wh/0x!#23(0W%"$w! $iiCLL$ t99((1+C#&LQ F3q6Nw<<[[^F&)L# "F6!9	 %9HH[!((3%
 ! %9HHV##G,%
 ! *>HH\")))4*
%& 344o.O	a
 !!23**E ))

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