ó —Àv]c@@sädZddlmZddddddd d d d d dddddgZddlZddlmZddlm Z ddl m Z ddd„Z d„Zde fd„ƒYZdefd„ƒYZdefd„ƒYZdefd„ƒYZeZdefd„ƒYZeZd efd „ƒYZd efd!„ƒYZd efd"„ƒYZd efd#„ƒYZdefd$„ƒYZdefd%„ƒYZdefd&„ƒYZdefd'„ƒYZdefd(„ƒYZdS()s% losses for training neural networks i(tabsolute_importtLosstL2LosstL1LosstSigmoidBinaryCrossEntropyLosstSigmoidBCELosstSoftmaxCrossEntropyLosst SoftmaxCELosst KLDivLosstCTCLosst HuberLosst HingeLosstSquaredHingeLosst LogisticLosst TripletLosstPoissonNLLLosstCosineEmbeddingLossNi(tndarray(t numeric_typesi(t HybridBlockcC@sY|dk r!|j||ƒ}n|dk rUt|tƒsHtdƒ‚||}n|S(sApply weighting to loss. Parameters ---------- loss : Symbol The loss to be weighted. weight : float or None Global scalar weight for loss. sample_weight : Symbol or None Per sample weighting. Must be broadcastable to the same shape as loss. For example, if loss has shape (64, 10) and you want to weight each sample in the batch separately, `sample_weight` should have shape (64, 1). Returns ------- loss : Symbol Weighted loss sweight must be a numberN(tNonet broadcast_mult isinstanceRtAssertionError(tFtlosstweightt sample_weight((s1/tmp/pip-install-Qvdv_2/mxnet/mxnet/gluon/loss.pyt_apply_weighting"s    cC@s,|tkr|j|jƒS|j||ƒS(s"Reshapes x to the same shape as y.(Rtreshapetshapet reshape_like(Rtxty((s1/tmp/pip-install-Qvdv_2/mxnet/mxnet/gluon/loss.pyt _reshape_likeAscB@s)eZdZd„Zd„Zd„ZRS(sÇBase class for loss. Parameters ---------- weight : float or None Global scalar weight for loss. batch_axis : int, default 0 The axis that represents mini-batch. cK@s,tt|ƒj|||_||_dS(N(tsuperRt__init__t_weightt _batch_axis(tselfRt batch_axistkwargs((s1/tmp/pip-install-Qvdv_2/mxnet/mxnet/gluon/loss.pyR$Qs cC@s"d}|jd|jj|jS(Ns-{name}(batch_axis={_batch_axis}, w={_weight})tname(tformatt __class__t__name__t__dict__(R'ts((s1/tmp/pip-install-Qvdv_2/mxnet/mxnet/gluon/loss.pyt__repr__VscO@s t‚dS(sOverrides to construct symbolic graph for this `Block`. Parameters ---------- x : Symbol or NDArray The first input tensor. *args : list of Symbol or list of NDArray Additional input tensors. N(tNotImplementedError(R'RR targsR)((s1/tmp/pip-install-Qvdv_2/mxnet/mxnet/gluon/loss.pythybrid_forwardZs (R-t __module__t__doc__R$R0R3(((s1/tmp/pip-install-Qvdv_2/mxnet/mxnet/gluon/loss.pyRFs   cB@s)eZdZddd„Zdd„ZRS(sÄCalculates the mean squared error between `label` and `pred`. .. math:: L = \frac{1}{2} \sum_i \vert {label}_i - {pred}_i \vert^2. `label` and `pred` can have arbitrary shape as long as they have the same number of elements. Parameters ---------- weight : float or None Global scalar weight for loss. batch_axis : int, default 0 The axis that represents mini-batch. Inputs: - **pred**: prediction tensor with arbitrary shape - **label**: target tensor with the same size as pred. - **sample_weight**: element-wise weighting tensor. Must be broadcastable to the same shape as pred. For example, if pred has shape (64, 10) and you want to weigh each sample in the batch separately, sample_weight should have shape (64, 1). Outputs: - **loss**: loss tensor with shape (batch_size,). Dimenions other than batch_axis are averaged out. gð?icK@s tt|ƒj|||dS(N(R#RR$(R'RR(R)((s1/tmp/pip-install-Qvdv_2/mxnet/mxnet/gluon/loss.pyR$†scC@s]t|||ƒ}|j||ƒ}t|||jd|ƒ}|j|d|jdtƒS(Nitaxistexclude(R"tsquareRR%tmeanR&tTrue(R'RtpredtlabelRR((s1/tmp/pip-install-Qvdv_2/mxnet/mxnet/gluon/loss.pyR3‰sN(R-R4R5R$RR3(((s1/tmp/pip-install-Qvdv_2/mxnet/mxnet/gluon/loss.pyRiscB@s)eZdZddd„Zdd„ZRS(s·Calculates the mean absolute error between `label` and `pred`. .. math:: L = \sum_i \vert {label}_i - {pred}_i \vert. `label` and `pred` can have arbitrary shape as long as they have the same number of elements. Parameters ---------- weight : float or None Global scalar weight for loss. batch_axis : int, default 0 The axis that represents mini-batch. Inputs: - **pred**: prediction tensor with arbitrary shape - **label**: target tensor with the same size as pred. - **sample_weight**: element-wise weighting tensor. Must be broadcastable to the same shape as pred. For example, if pred has shape (64, 10) and you want to weigh each sample in the batch separately, sample_weight should have shape (64, 1). Outputs: - **loss**: loss tensor with shape (batch_size,). Dimenions other than batch_axis are averaged out. icK@s tt|ƒj|||dS(N(R#RR$(R'RR(R)((s1/tmp/pip-install-Qvdv_2/mxnet/mxnet/gluon/loss.pyR$­scC@sYt|||ƒ}|j||ƒ}t|||j|ƒ}|j|d|jdtƒS(NR6R7(R"tabsRR%R9R&R:(R'RR;R<RR((s1/tmp/pip-install-Qvdv_2/mxnet/mxnet/gluon/loss.pyR3°sN(R-R4R5RR$R3(((s1/tmp/pip-install-Qvdv_2/mxnet/mxnet/gluon/loss.pyRscB@s/eZdZeddd„Zddd„ZRS(s=The cross-entropy loss for binary classification. (alias: SigmoidBCELoss) BCE loss is useful when training logistic regression. If `from_sigmoid` is False (default), this loss computes: .. math:: prob = \frac{1}{1 + \exp(-{pred})} L = - \sum_i {label}_i * \log({prob}_i) * pos\_weight + (1 - {label}_i) * \log(1 - {prob}_i) If `from_sigmoid` is True, this loss computes: .. math:: L = - \sum_i {label}_i * \log({pred}_i) * pos\_weight + (1 - {label}_i) * \log(1 - {pred}_i) A tensor `pos_weight > 1` decreases the false negative count, hence increasing the recall. Conversely setting `pos_weight < 1` decreases the false positive count and increases the precision. `pred` and `label` can have arbitrary shape as long as they have the same number of elements. Parameters ---------- from_sigmoid : bool, default is `False` Whether the input is from the output of sigmoid. Set this to false will make the loss calculate sigmoid and BCE together, which is more numerically stable through log-sum-exp trick. weight : float or None Global scalar weight for loss. batch_axis : int, default 0 The axis that represents mini-batch. Inputs: - **pred**: prediction tensor with arbitrary shape - **label**: target tensor with values in range `[0, 1]`. Must have the same size as `pred`. - **sample_weight**: element-wise weighting tensor. Must be broadcastable to the same shape as pred. For example, if pred has shape (64, 10) and you want to weigh each sample in the batch separately, sample_weight should have shape (64, 1). - **pos_weight**: a weighting tensor of positive examples. Must be a vector with length equal to the number of classes.For example, if pred has shape (64, 10), pos_weight should have shape (1, 10). Outputs: - **loss**: loss tensor with shape (batch_size,). Dimenions other than batch_axis are averaged out. icK@s)tt|ƒj|||||_dS(N(R#RR$t _from_sigmoid(R't from_sigmoidRR(R)((s1/tmp/pip-install-Qvdv_2/mxnet/mxnet/gluon/loss.pyR$ðs c C@swt|||ƒ}|js¸|dkr^|j|ƒ|||j|j|ƒ ddƒ}qCd|j|d|ƒ}|||||j|j|ƒ ddƒ|j| ƒ}n‹d}|dkr|j||ƒ||jd||ƒd| }nA|j|j||ƒ||ƒ|jd||ƒd| }t|||j |ƒ}|j |d|j dt ƒS(Ntact_typetsoftreluigê-™—q=gð?R6R7( R"R>Rtrelut ActivationR=RtlogRR%R9R&R:( R'RR;R<Rt pos_weightRt log_weightteps((s1/tmp/pip-install-Qvdv_2/mxnet/mxnet/gluon/loss.pyR3õs   #2 $ !N(R-R4R5tFalseRR$R3(((s1/tmp/pip-install-Qvdv_2/mxnet/mxnet/gluon/loss.pyR·s7cB@s2eZdZdeeddd„Zdd„ZRS(søComputes the softmax cross entropy loss. (alias: SoftmaxCELoss) If `sparse_label` is `True` (default), label should contain integer category indicators: .. math:: \DeclareMathOperator{softmax}{softmax} p = \softmax({pred}) L = -\sum_i \log p_{i,{label}_i} `label`'s shape should be `pred`'s shape with the `axis` dimension removed. i.e. for `pred` with shape (1,2,3,4) and `axis = 2`, `label`'s shape should be (1,2,4). If `sparse_label` is `False`, `label` should contain probability distribution and `label`'s shape should be the same with `pred`: .. math:: p = \softmax({pred}) L = -\sum_i \sum_j {label}_j \log p_{ij} Parameters ---------- axis : int, default -1 The axis to sum over when computing softmax and entropy. sparse_label : bool, default True Whether label is an integer array instead of probability distribution. from_logits : bool, default False Whether input is a log probability (usually from log_softmax) instead of unnormalized numbers. weight : float or None Global scalar weight for loss. batch_axis : int, default 0 The axis that represents mini-batch. Inputs: - **pred**: the prediction tensor, where the `batch_axis` dimension ranges over batch size and `axis` dimension ranges over the number of classes. - **label**: the truth tensor. When `sparse_label` is True, `label`'s shape should be `pred`'s shape with the `axis` dimension removed. i.e. for `pred` with shape (1,2,3,4) and `axis = 2`, `label`'s shape should be (1,2,4) and values should be integers between 0 and 2. If `sparse_label` is False, `label`'s shape must be the same as `pred` and values should be floats in the range `[0, 1]`. - **sample_weight**: element-wise weighting tensor. Must be broadcastable to the same shape as label. For example, if label has shape (64, 10) and you want to weigh each sample in the batch separately, sample_weight should have shape (64, 1). Outputs: - **loss**: loss tensor with shape (batch_size,). Dimenions other than batch_axis are averaged out. iÿÿÿÿicK@s;tt|ƒj|||||_||_||_dS(N(R#RR$t_axist _sparse_labelt _from_logits(R'R6t sparse_labelt from_logitsRR(R)((s1/tmp/pip-install-Qvdv_2/mxnet/mxnet/gluon/loss.pyR$Os    cC@s¸|js!|j||jƒ}n|jrO|j||d|jdtƒ }n5t|||ƒ}|j||d|jdtƒ }t|||j |ƒ}|j |d|j dtƒS(NR6tkeepdimsR7( RKt log_softmaxRIRJtpickR:R"tsumRR%R9R&(R'RR;R<RR((s1/tmp/pip-install-Qvdv_2/mxnet/mxnet/gluon/loss.pyR3Ws  %#N(R-R4R5R:RHRR$R3(((s1/tmp/pip-install-Qvdv_2/mxnet/mxnet/gluon/loss.pyRs<  cB@s/eZdZedddd„Zdd„ZRS(sáThe Kullback-Leibler divergence loss. KL divergence measures the distance between contiguous distributions. It can be used to minimize information loss when approximating a distribution. If `from_logits` is True (default), loss is defined as: .. math:: L = \sum_i {label}_i * \big[\log({label}_i) - {pred}_i\big] If `from_logits` is False, loss is defined as: .. math:: \DeclareMathOperator{softmax}{softmax} prob = \softmax({pred}) L = \sum_i {label}_i * \big[\log({label}_i) - log({pred}_i)\big] `label` and `pred` can have arbitrary shape as long as they have the same number of elements. Parameters ---------- from_logits : bool, default is `True` Whether the input is log probability (usually from log_softmax) instead of unnormalized numbers. axis : int, default -1 The dimension along with to compute softmax. Only used when `from_logits` is False. weight : float or None Global scalar weight for loss. batch_axis : int, default 0 The axis that represents mini-batch. Inputs: - **pred**: prediction tensor with arbitrary shape. If `from_logits` is True, `pred` should be log probabilities. Otherwise, it should be unnormalized predictions, i.e. from a dense layer. - **label**: truth tensor with values in range `(0, 1)`. Must have the same size as `pred`. - **sample_weight**: element-wise weighting tensor. Must be broadcastable to the same shape as pred. For example, if pred has shape (64, 10) and you want to weigh each sample in the batch separately, sample_weight should have shape (64, 1). Outputs: - **loss**: loss tensor with shape (batch_size,). Dimenions other than batch_axis are averaged out. References ---------- `Kullback-Leibler divergence `_ iÿÿÿÿicK@s2tt|ƒj|||||_||_dS(N(R#RR$RKRI(R'RMR6RR(R)((s1/tmp/pip-install-Qvdv_2/mxnet/mxnet/gluon/loss.pyR$£s cC@sp|js!|j||jƒ}n||j|dƒ|}t|||j|ƒ}|j|d|jdtƒS(Ngê-™—q=R6R7( RKRORIRDRR%R9R&R:(R'RR;R<RR((s1/tmp/pip-install-Qvdv_2/mxnet/mxnet/gluon/loss.pyR3©s  N(R-R4R5R:RR$R3(((s1/tmp/pip-install-Qvdv_2/mxnet/mxnet/gluon/loss.pyRfs;cB@s2eZdZdddd„Zdddd„ZRS(sòConnectionist Temporal Classification Loss. Parameters ---------- layout : str, default 'NTC' Layout of prediction tensor. 'N', 'T', 'C' stands for batch size, sequence length, and alphabet_size respectively. label_layout : str, default 'NT' Layout of the labels. 'N', 'T' stands for batch size, and sequence length respectively. weight : float or None Global scalar weight for loss. Inputs: - **pred**: unnormalized prediction tensor (before softmax). Its shape depends on `layout`. If `layout` is 'TNC', pred should have shape `(sequence_length, batch_size, alphabet_size)`. Note that in the last dimension, index `alphabet_size-1` is reserved for internal use as blank label. So `alphabet_size` is one plus the actual alphabet size. - **label**: zero-based label tensor. Its shape depends on `label_layout`. If `label_layout` is 'TN', `label` should have shape `(label_sequence_length, batch_size)`. - **pred_lengths**: optional (default None), used for specifying the length of each entry when different `pred` entries in the same batch have different lengths. `pred_lengths` should have shape `(batch_size,)`. - **label_lengths**: optional (default None), used for specifying the length of each entry when different `label` entries in the same batch have different lengths. `label_lengths` should have shape `(batch_size,)`. Outputs: - **loss**: output loss has shape `(batch_size,)`. **Example**: suppose the vocabulary is `[a, b, c]`, and in one batch we have three sequences 'ba', 'cbb', and 'abac'. We can index the labels as `{'a': 0, 'b': 1, 'c': 2, blank: 3}`. Then `alphabet_size` should be 4, where label 3 is reserved for internal use by `CTCLoss`. We then need to pad each sequence with `-1` to make a rectangular `label` tensor:: [[1, 0, -1, -1], [2, 1, 1, -1], [0, 1, 0, 2]] References ---------- `Connectionist Temporal Classification: Labelling Unsegmented Sequence Data with Recurrent Neural Networks `_ tNTCtNTcK@sy|dkstd|ƒ‚|d ks8td|ƒ‚||_||_|jdƒ}tt|ƒj|||dS( NRRtTNCs<Only 'NTC' and 'TNC' layouts for pred are supported. Got: %sRStTNs;Only 'NT' and 'TN' layouts for label are supported. Got: %stN(RRRT(RSRU(Rt_layoutt _label_layouttfindR#R R$(R'tlayoutt label_layoutRR)R(((s1/tmp/pip-install-Qvdv_2/mxnet/mxnet/gluon/loss.pyR$ës    c C@sš|jdkr'|j|ddƒ}n|jdkrN|j|ddƒ}n|j||||d|dk d|dk ddƒ}t|||j|ƒS(NRRiituse_data_lengthstuse_label_lengthst blank_labeltlast(RWtswapaxesR&R RRR%(R'RR;R<t pred_lengthst label_lengthsRR((s1/tmp/pip-install-Qvdv_2/mxnet/mxnet/gluon/loss.pyR3õs   N(R-R4R5RR$R3(((s1/tmp/pip-install-Qvdv_2/mxnet/mxnet/gluon/loss.pyR ±s8 cB@s,eZdZdddd„Zdd„ZRS(sNCalculates smoothed L1 loss that is equal to L1 loss if absolute error exceeds rho but is equal to L2 loss otherwise. Also called SmoothedL1 loss. .. math:: L = \sum_i \begin{cases} \frac{1}{2 {rho}} ({label}_i - {pred}_i)^2 & \text{ if } |{label}_i - {pred}_i| < {rho} \\ |{label}_i - {pred}_i| - \frac{{rho}}{2} & \text{ otherwise } \end{cases} `label` and `pred` can have arbitrary shape as long as they have the same number of elements. Parameters ---------- rho : float, default 1 Threshold for trimmed mean estimator. weight : float or None Global scalar weight for loss. batch_axis : int, default 0 The axis that represents mini-batch. Inputs: - **pred**: prediction tensor with arbitrary shape - **label**: target tensor with the same size as pred. - **sample_weight**: element-wise weighting tensor. Must be broadcastable to the same shape as pred. For example, if pred has shape (64, 10) and you want to weigh each sample in the batch separately, sample_weight should have shape (64, 1). Outputs: - **loss**: loss tensor with shape (batch_size,). Dimenions other than batch_axis are averaged out. iicK@s)tt|ƒj|||||_dS(N(R#R R$t_rho(R'trhoRR(R)((s1/tmp/pip-install-Qvdv_2/mxnet/mxnet/gluon/loss.pyR$'scC@s–t|||ƒ}|j||ƒ}|j||jk|d|jd|j|j|ƒƒ}t|||j|ƒ}|j|d|jdt ƒS(Ngà?R6R7( R"R=twhereRcR8RR%R9R&R:(R'RR;R<RR((s1/tmp/pip-install-Qvdv_2/mxnet/mxnet/gluon/loss.pyR3+s  N(R-R4R5RR$R3(((s1/tmp/pip-install-Qvdv_2/mxnet/mxnet/gluon/loss.pyR s#cB@s,eZdZdddd„Zdd„ZRS(spCalculates the hinge loss function often used in SVMs: .. math:: L = \sum_i max(0, {margin} - {pred}_i \cdot {label}_i) where `pred` is the classifier prediction and `label` is the target tensor containing values -1 or 1. `label` and `pred` must have the same number of elements. Parameters ---------- margin : float The margin in hinge loss. Defaults to 1.0 weight : float or None Global scalar weight for loss. batch_axis : int, default 0 The axis that represents mini-batch. Inputs: - **pred**: prediction tensor with arbitrary shape. - **label**: truth tensor with values -1 or 1. Must have the same size as pred. - **sample_weight**: element-wise weighting tensor. Must be broadcastable to the same shape as pred. For example, if pred has shape (64, 10) and you want to weigh each sample in the batch separately, sample_weight should have shape (64, 1). Outputs: - **loss**: loss tensor with shape (batch_size,). Dimenions other than batch_axis are averaged out. iicK@s)tt|ƒj|||||_dS(N(R#R R$t_margin(R'tmarginRR(R)((s1/tmp/pip-install-Qvdv_2/mxnet/mxnet/gluon/loss.pyR$VscC@s`t|||ƒ}|j|j||ƒ}t|||j|ƒ}|j|d|jdtƒS(NR6R7(R"RBRfRR%R9R&R:(R'RR;R<RR((s1/tmp/pip-install-Qvdv_2/mxnet/mxnet/gluon/loss.pyR3ZsN(R-R4R5RR$R3(((s1/tmp/pip-install-Qvdv_2/mxnet/mxnet/gluon/loss.pyR 4s cB@s,eZdZdddd„Zdd„ZRS(s•Calculates the soft-margin loss function used in SVMs: .. math:: L = \sum_i max(0, {margin} - {pred}_i \cdot {label}_i)^2 where `pred` is the classifier prediction and `label` is the target tensor containing values -1 or 1. `label` and `pred` can have arbitrary shape as long as they have the same number of elements. Parameters ---------- margin : float The margin in hinge loss. Defaults to 1.0 weight : float or None Global scalar weight for loss. batch_axis : int, default 0 The axis that represents mini-batch. Inputs: - **pred**: prediction tensor with arbitrary shape - **label**: truth tensor with values -1 or 1. Must have the same size as pred. - **sample_weight**: element-wise weighting tensor. Must be broadcastable to the same shape as pred. For example, if pred has shape (64, 10) and you want to weigh each sample in the batch separately, sample_weight should have shape (64, 1). Outputs: - **loss**: loss tensor with shape (batch_size,). Dimenions other than batch_axis are averaged out. iicK@s)tt|ƒj|||||_dS(N(R#R R$Rf(R'RgRR(R)((s1/tmp/pip-install-Qvdv_2/mxnet/mxnet/gluon/loss.pyR$ƒscC@sit|||ƒ}|j|j|j||ƒƒ}t|||j|ƒ}|j|d|jdtƒS(NR6R7( R"R8RBRfRR%R9R&R:(R'RR;R<RR((s1/tmp/pip-install-Qvdv_2/mxnet/mxnet/gluon/loss.pyR3‡s#N(R-R4R5RR$R3(((s1/tmp/pip-install-Qvdv_2/mxnet/mxnet/gluon/loss.pyR as cB@s,eZdZdddd„Zdd„ZRS(s¨Calculates the logistic loss (for binary losses only): .. math:: L = \sum_i \log(1 + \exp(- {pred}_i \cdot {label}_i)) where `pred` is the classifier prediction and `label` is the target tensor containing values -1 or 1 (0 or 1 if `label_format` is binary). `label` and `pred` can have arbitrary shape as long as they have the same number of elements. Parameters ---------- weight : float or None Global scalar weight for loss. batch_axis : int, default 0 The axis that represents mini-batch. label_format : str, default 'signed' Can be either 'signed' or 'binary'. If the label_format is 'signed', all label values should be either -1 or 1. If the label_format is 'binary', all label values should be either 0 or 1. Inputs: - **pred**: prediction tensor with arbitrary shape. - **label**: truth tensor with values -1/1 (label_format is 'signed') or 0/1 (label_format is 'binary'). Must have the same size as pred. - **sample_weight**: element-wise weighting tensor. Must be broadcastable to the same shape as pred. For example, if pred has shape (64, 10) and you want to weigh each sample in the batch separately, sample_weight should have shape (64, 1). Outputs: - **loss**: loss tensor with shape (batch_size,). Dimenions other than batch_axis are averaged out. itsignedcK@sKtt|ƒj|||||_|jdkrGtd|ƒ‚ndS(NRhtbinarys7label_format can only be signed or binary, recieved %s.(RhRi(R#R R$t _label_formatt ValueError(R'RR(t label_formatR)((s1/tmp/pip-install-Qvdv_2/mxnet/mxnet/gluon/loss.pyR$±s  cC@sšt|||ƒ}|jdkr2|dd}n|j|ƒ|||j|j|ƒ ddƒ}t|||j|ƒ}|j|d|jdt ƒS(NRhgð?g@R@RAR6R7( R"RjRBRCR=RR%R9R&R:(R'RR;R<RR((s1/tmp/pip-install-Qvdv_2/mxnet/mxnet/gluon/loss.pyR3¸s N(R-R4R5RR$R3(((s1/tmp/pip-install-Qvdv_2/mxnet/mxnet/gluon/loss.pyR Žs!cB@s)eZdZdddd„Zd„ZRS(sDCalculates triplet loss given three input tensors and a positive margin. Triplet loss measures the relative similarity between a positive example, a negative example, and prediction: .. math:: L = \sum_i \max(\Vert {pos_i}_i - {pred} \Vert_2^2 - \Vert {neg_i}_i - {pred} \Vert_2^2 + {margin}, 0) `positive`, `negative`, and 'pred' can have arbitrary shape as long as they have the same number of elements. Parameters ---------- margin : float Margin of separation between correct and incorrect pair. weight : float or None Global scalar weight for loss. batch_axis : int, default 0 The axis that represents mini-batch. Inputs: - **pred**: prediction tensor with arbitrary shape - **positive**: positive example tensor with arbitrary shape. Must have the same size as pred. - **negative**: negative example tensor with arbitrary shape Must have the same size as pred. Outputs: - **loss**: loss tensor with shape (batch_size,). iicK@s)tt|ƒj|||||_dS(N(R#RR$Rf(R'RgRR(R)((s1/tmp/pip-install-Qvdv_2/mxnet/mxnet/gluon/loss.pyR$äscC@sŒt|||ƒ}t|||ƒ}|j|j||ƒ|j||ƒd|jdtƒ}|j||jƒ}t|||jdƒS(NR6R7( R"RQR8R&R:RBRfRR%R(R'RR;tpositivetnegativeR((s1/tmp/pip-install-Qvdv_2/mxnet/mxnet/gluon/loss.pyR3ès *N(R-R4R5RR$R3(((s1/tmp/pip-install-Qvdv_2/mxnet/mxnet/gluon/loss.pyRÃscB@s2eZdZdeded„Zddd„ZRS(sQFor a target (Random Variable) in a Poisson distribution, the function calculates the Negative Log likelihood loss. PoissonNLLLoss measures the loss accrued from a poisson regression prediction made by the model. .. math:: L = \text{pred} - \text{target} * \log(\text{pred}) +\log(\text{target!}) `target`, 'pred' can have arbitrary shape as long as they have the same number of elements. Parameters ---------- from_logits : boolean, default True indicating whether log(predicted) value has already been computed. If True, the loss is computed as :math:`\exp(\text{pred}) - \text{target} * \text{pred}`, and if False, then loss is computed as :math:`\text{pred} - \text{target} * \log(\text{pred}+\text{epsilon})`.The default value weight : float or None Global scalar weight for loss. batch_axis : int, default 0 The axis that represents mini-batch. compute_full: boolean, default False Indicates whether to add an approximation(Stirling factor) for the Factorial term in the formula for the loss. The Stirling factor is: :math:`\text{target} * \log(\text{target}) - \text{target} + 0.5 * \log(2 * \pi * \text{target})` epsilon: float, default 1e-08 This is to avoid calculating log(0) which is not defined. Inputs: - **pred**: Predicted value - **target**: Random variable(count or number) which belongs to a Poisson distribution. - **sample_weight**: element-wise weighting tensor. Must be broadcastable to the same shape as pred. For example, if pred has shape (64, 10) and you want to weigh each sample in the batch separately, sample_weight should have shape (64, 1). Outputs: - **loss**: Average loss (shape=(1,1)) of the loss tensor with shape (batch_size,). icK@s2tt|ƒj|||||_||_dS(N(R#RR$RKt _compute_full(R'RRMR(t compute_fullR)((s1/tmp/pip-install-Qvdv_2/mxnet/mxnet/gluon/loss.pyR$s g:Œ0âŽyE>c C@sÔt|||ƒ}|jr5|j|ƒ||}n|||j||ƒ}|jr¯||j|ƒ|d|jd|tjƒ}|dk}||9}||7}nt|||j|ƒ}|j |ƒS(Ngà?ii( R"RKtexpRDRotnptpiRR%R9( R'RR;ttargetRtepsilonRtstirling_factort target_gt_1((s1/tmp/pip-install-Qvdv_2/mxnet/mxnet/gluon/loss.pyR3s  3   N(R-R4R5RR:RHR$R3(((s1/tmp/pip-install-Qvdv_2/mxnet/mxnet/gluon/loss.pyRñs&cB@s8eZdZdddd„Zdd„Zdd„ZRS(sþFor a target label 1 or -1, vectors input1 and input2, the function computes the cosine distance between the vectors. This can be interpreted as how similar/dissimilar two input vectors are. .. math:: L = \sum_i \begin{cases} 1 - {cos\_sim({input1}_i, {input2}_i)} & \text{ if } {label}_i = 1\\ {cos\_sim({input1}_i, {input2}_i)} & \text{ if } {label}_i = -1 \end{cases}\\ cos\_sim(input1, input2) = \frac{{input1}_i.{input2}_i}{||{input1}_i||.||{input2}_i||} `input1`, `input2` can have arbitrary shape as long as they have the same number of elements. Parameters ---------- weight : float or None Global scalar weight for loss. batch_axis : int, default 0 The axis that represents mini-batch. margin : float Margin of separation between correct and incorrect pair. Inputs: - **input1**: a tensor with arbitrary shape - **input2**: another tensor with same shape as pred to which input1 is compared for similarity and loss calculation - **label**: A 1-D tensor indicating for each pair input1 and input2, target label is 1 or -1 - **sample_weight**: element-wise weighting tensor. Must be broadcastable to the same shape as input1. For example, if input1 has shape (64, 10) and you want to weigh each sample in the batch separately, sample_weight should have shape (64, 1). Outputs: - **loss**: The loss tensor with shape (batch_size,). icK@s)tt|ƒj|||||_dS(N(R#RR$Rf(R'RR(RgR)((s1/tmp/pip-install-Qvdv_2/mxnet/mxnet/gluon/loss.pyR$Ssc C@sÕt|||ƒ}|jdƒ}|j|||ƒ}|dk}|dk}d||} |tkr}|jdgƒ} n|jdƒ} |j| |||jddƒ} | | } t|| |j |ƒ} | S(NiÿÿÿÿiiR6(iÿÿÿÿi(ii( R"Rt_cosine_similarityRtarraytzerostbroadcast_maximumRfRR%( R'Rtinput1tinput2R<Rtcos_simty_1t y_minus_1t cos_sim_atz_arrayt cos_sim_bR((s1/tmp/pip-install-Qvdv_2/mxnet/mxnet/gluon/loss.pyR3Ws    iÿÿÿÿc C@s²|j|d|ƒjddƒ}|j|d|ƒjddƒ}|j||d|ƒjddƒ}|tkrˆ|jdgƒ}n|jddƒ}||j|||ƒS(NR6iÿÿÿÿigê-™—q=(ii(tnormRRQRRytfullR{( R'RR R!R6tx_normty_normtx_dot_yteps_arr((s1/tmp/pip-install-Qvdv_2/mxnet/mxnet/gluon/loss.pyRxis!!% N(R-R4R5RR$R3Rx(((s1/tmp/pip-install-Qvdv_2/mxnet/mxnet/gluon/loss.pyR/s" (R5t __future__Rt__all__tnumpyRrtRtbaseRtblockRRRR"RRRRRRRRR R R R R RRR(((s1/tmp/pip-install-Qvdv_2/mxnet/mxnet/gluon/loss.pyts8    #''WRKQ2--5.>