ó ùµÈ[c@@s dZddlmZddlZddlmZddlZddlmZm Z ddl m Z ddl m Z e e d „Zd efd „ƒYZe jed ƒZe jed ƒZe jed ƒZd „Zeedƒdefd„ƒYƒƒZeedƒdefd„ƒYƒƒZeeddƒdefd„ƒYƒƒZdefd„ƒYZedefd„ƒYƒZedefd„ƒYƒZedefd„ƒYƒZed efd!„ƒYƒZed"efd#„ƒYƒZ ed$efd%„ƒYƒZ!eed&ƒd'efd(„ƒYƒƒZ"eed)ƒd*efd+„ƒYƒƒZ#eed,ƒd-efd.„ƒYƒƒZ$ed/efd0„ƒYƒZ%ed1e%fd2„ƒYƒZ&ed3e%fd4„ƒYƒZ'ed5efd6„ƒYƒZ(de d7„Z*dS(8s Online evaluation metric module.i(tabsolute_importN(t OrderedDicti(t numeric_typest string_types(tndarray(tregistrycC@s±|s"t|ƒt|ƒ}}n|j|j}}||kr\tdj||ƒƒ‚n|r§t|tjjƒrƒ|g}nt|tjjƒr§|g}q§n||fS(s¨Helper function for checking shape of label and prediction Parameters ---------- labels : list of `NDArray` The labels of the data. preds : list of `NDArray` Predicted values. wrap : boolean If True, wrap labels/preds in a list if they are single NDArray shape : boolean If True, check the shape of labels and preds; Otherwise only check their length. s9Shape of labels {} does not match shape of predictions {}(tlentshapet ValueErrortformatt isinstanceRtNDArray(tlabelstpredstwrapRt label_shapet pred_shape((sL/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/metric.pytcheck_label_shapes!s   t EvalMetriccB@s\eZdZd d d„Zd„Zd„Zd„Zd„Zd„Z d„Z d„Z RS( s‹Base class for all evaluation metrics. .. note:: This is a base class that provides common metric interfaces. One should not use this class directly, but instead create new metric classes that extend it. Parameters ---------- name : str Name of this metric instance for display. output_names : list of str, or None Name of predictions that should be used when updating with update_dict. By default include all predictions. label_names : list of str, or None Name of labels that should be used when updating with update_dict. By default include all labels. cK@s8t|ƒ|_||_||_||_|jƒdS(N(tstrtnamet output_namest label_namest_kwargstreset(tselfRRRtkwargs((sL/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/metric.pyt__init__Xs    cC@sdjt|jƒƒƒS(NsEvalMetric: {}(R tdicttget_name_value(R((sL/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/metric.pyt__str__`scC@sK|jjƒ}|ji|jjd6|jd6|jd6|jd6ƒ|S(sjSave configurations of metric. Can be recreated from configs with metric.create(**config) tmetricRRR(Rtcopytupdatet __class__t__name__RRR(Rtconfig((sL/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/metric.pyt get_configcs    cC@sœ|jdk r2g|jD]}||^q}nt|jƒƒ}|jdk rvg|jD]}||^q]}nt|jƒƒ}|j||ƒdS(s,Update the internal evaluation with named label and pred Parameters ---------- labels : OrderedDict of str -> NDArray name to array mapping for labels. preds : OrderedDict of str -> NDArray name to array mapping of predicted outputs. N(RtNonetlisttvaluesRR!(RtlabeltpredR((sL/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/metric.pyt update_dictos ##cC@s tƒ‚dS(sßUpdates the internal evaluation result. Parameters ---------- labels : list of `NDArray` The labels of the data. preds : list of `NDArray` Predicted values. N(tNotImplementedError(RR R ((sL/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/metric.pyR!†s cC@sd|_d|_dS(s7Resets the internal evaluation result to initial state.igN(tnum_instt sum_metric(R((sL/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/metric.pyR“s cC@s=|jdkr"|jtdƒfS|j|j|jfSdS(sÍGets the current evaluation result. Returns ------- names : list of str Name of the metrics. values : list of float Value of the evaluations. itnanN(R-RtfloatR.(R((sL/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/metric.pytget˜s cC@s[|jƒ\}}t|tƒs-|g}nt|tƒsH|g}ntt||ƒƒS(sReturns zipped name and value pairs. Returns ------- list of tuples A (name, value) tuple list. (R1R R'tzip(RRtvalue((sL/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/metric.pyR§s   N( R#t __module__t__doc__R&RRR%R+R!RR1R(((sL/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/metric.pyRDs     RcO@srt|ƒrt|||ŽSt|tƒrbtƒ}x'|D]}|jt|||Žƒq;W|St|||ŽS(síCreates evaluation metric from metric names or instances of EvalMetric or a custom metric function. Parameters ---------- metric : str or callable Specifies the metric to create. This argument must be one of the below: - Name of a metric. - An instance of `EvalMetric`. - A list, each element of which is a metric or a metric name. - An evaluation function that computes custom metric for a given batch of labels and predictions. *args : list Additional arguments to metric constructor. Only used when metric is str. **kwargs : dict Additional arguments to metric constructor. Only used when metric is str Examples -------- >>> def custom_metric(label, pred): ... return np.mean(np.abs(label - pred)) ... >>> metric1 = mx.metric.create('acc') >>> metric2 = mx.metric.create(custom_metric) >>> metric3 = mx.metric.create([metric1, metric2, 'rmse']) (tcallablet CustomMetricR R'tCompositeEvalMetrictaddtcreatet_create(RtargsRtcomposite_metrict child_metric((sL/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/metric.pyR:½s   t compositeR8cB@sbeZdZd dd d d„Zd„Zd„Zd„Zd„Zd„Z d„Z d „Z RS( sManages multiple evaluation metrics. Parameters ---------- metrics : list of EvalMetric List of child metrics. name : str Name of this metric instance for display. output_names : list of str, or None Name of predictions that should be used when updating with update_dict. By default include all predictions. label_names : list of str, or None Name of labels that should be used when updating with update_dict. By default include all labels. Examples -------- >>> predicts = [mx.nd.array([[0.3, 0.7], [0, 1.], [0.4, 0.6]])] >>> labels = [mx.nd.array([0, 1, 1])] >>> eval_metrics_1 = mx.metric.Accuracy() >>> eval_metrics_2 = mx.metric.F1() >>> eval_metrics = mx.metric.CompositeEvalMetric() >>> for child_metric in [eval_metrics_1, eval_metrics_2]: >>> eval_metrics.add(child_metric) >>> eval_metrics.update(labels = labels, preds = predicts) >>> print eval_metrics.get() (['accuracy', 'f1'], [0.6666666666666666, 0.8]) R?cC@s]tt|ƒj|d|d|ƒ|dkr7g}ng|D]}t|ƒ^q>|_dS(NRR(tsuperR8RR&R:tmetrics(RRARRRti((sL/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/metric.pyRs   cC@s|jjt|ƒƒdS(srAdds a child metric. Parameters ---------- metric A metric instance. N(RAtappendR:(RR((sL/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/metric.pyR9scC@sCy|j|SWn-tk r>tdj|t|jƒƒƒSXdS(s•Returns a child metric. Parameters ---------- index : int Index of child metric in the list of metrics. s(Metric index {} is out of range 0 and {}N(RAt IndexErrorRR R(Rtindex((sL/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/metric.pyt get_metrics   cC@s¼|jdk rJtg|jƒD]}|d|jkr|^qƒ}n|jdk r”tg|jƒD]}|d|jkri|^qiƒ}nx!|jD]}|j||ƒqžWdS(Ni(RR&RtitemsRRAR+(RR R RBR((sL/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/metric.pyR+'s%%cC@s(x!|jD]}|j||ƒq WdS(sßUpdates the internal evaluation result. Parameters ---------- labels : list of `NDArray` The labels of the data. preds : list of `NDArray` Predicted values. N(RAR!(RR R R((sL/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/metric.pyR!2s cC@s:y"x|jD]}|jƒq WWntk r5nXdS(s7Resets the internal evaluation result to initial state.N(RARtAttributeError(RR((sL/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/metric.pyR@s  cC@sŒg}g}xs|jD]h}|jƒ\}}t|tƒrI|g}nt|tƒrd|g}n|j|ƒ|j|ƒqW||fS(sÐReturns the current evaluation result. Returns ------- names : list of str Name of the metrics. values : list of float Value of the evaluations. (RAR1R RRtextend(RtnamesR(RRR3((sL/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/metric.pyR1Hs    cC@sItt|ƒjƒ}|jig|jD]}|jƒ^q(d6ƒ|S(NRA(R@R8R%R!RA(RR$RB((sL/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/metric.pyR%^s0N( R#R4R5R&RR9RFR+R!RR1R%(((sL/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/metric.pyR8çs    tacctAccuracycB@s,eZdZddddd„Zd„ZRS(sÈComputes accuracy classification score. The accuracy score is defined as .. math:: \text{accuracy}(y, \hat{y}) = \frac{1}{n} \sum_{i=0}^{n-1} \text{1}(\hat{y_i} == y_i) Parameters ---------- axis : int, default=1 The axis that represents classes name : str Name of this metric instance for display. output_names : list of str, or None Name of predictions that should be used when updating with update_dict. By default include all predictions. label_names : list of str, or None Name of labels that should be used when updating with update_dict. By default include all labels. Examples -------- >>> predicts = [mx.nd.array([[0.3, 0.7], [0, 1.], [0.4, 0.6]])] >>> labels = [mx.nd.array([0, 1, 1])] >>> acc = mx.metric.Accuracy() >>> acc.update(preds = predicts, labels = labels) >>> print acc.get() ('accuracy', 0.6666666666666666) itaccuracycC@s5tt|ƒj|d|d|d|ƒ||_dS(NtaxisRR(R@RLRRN(RRNRRR((sL/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/metric.pyRŠs  cC@sât||tƒ\}}xÃt||ƒD]²\}}|j|jkratj|d|jƒ}n|jƒjdƒ}|jƒjdƒ}|j }|j }t||ƒ|j ||kj ƒ7_ |j t |ƒ7_ q(WdS(s†Updates the internal evaluation result. Parameters ---------- labels : list of `NDArray` The labels of the data with class indices as values, one per sample. preds : list of `NDArray` Prediction values for samples. Each prediction value can either be the class index, or a vector of likelihoods for all classes. RNtint32N(RtTrueR2RRtargmaxRNtasnumpytastypetflatR.tsumR-R(RR R R)t pred_label((sL/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/metric.pyR!‘s    N(R#R4R5R&RR!(((sL/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/metric.pyRLis ttop_k_accuracyt top_k_acct TopKAccuracycB@s,eZdZddddd„Zd„ZRS(sGComputes top k predictions accuracy. `TopKAccuracy` differs from Accuracy in that it considers the prediction to be ``True`` as long as the ground truth label is in the top K predicated labels. If `top_k` = ``1``, then `TopKAccuracy` is identical to `Accuracy`. Parameters ---------- top_k : int Whether targets are in top k predictions. name : str Name of this metric instance for display. output_names : list of str, or None Name of predictions that should be used when updating with update_dict. By default include all predictions. label_names : list of str, or None Name of labels that should be used when updating with update_dict. By default include all labels. Examples -------- >>> np.random.seed(999) >>> top_k = 3 >>> labels = [mx.nd.array([2, 6, 9, 2, 3, 4, 7, 8, 9, 6])] >>> predicts = [mx.nd.array(np.random.rand(10, 10))] >>> acc = mx.metric.TopKAccuracy(top_k=top_k) >>> acc.update(labels, predicts) >>> print acc.get() ('top_k_accuracy', 0.3) iRWcC@sftt|ƒj|d|d|d|ƒ||_|jdksLtdƒ‚|jd|j7_dS(Nttop_kRRis.Please use Accuracy if top_k is no more than 1s_%d(R@RYRRZtAssertionErrorR(RRZRRR((sL/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/metric.pyRÒs    c C@s|t||tƒ\}}x]t||ƒD]L\}}t|jƒdksUtdƒ‚tj|jƒj dƒddƒ}|jƒj dƒ}t||ƒ|jd}t|jƒ}|dkrç|j |j |j kj ƒ7_ n~|dkre|jd}t ||jƒ}xPt|ƒD]?} |j |dd…|d| fj |j kj ƒ7_ qWn|j|7_q(WdS( sßUpdates the internal evaluation result. Parameters ---------- labels : list of `NDArray` The labels of the data. preds : list of `NDArray` Predicted values. is)Predictions should be no more than 2 dimstfloat32RNiROiN(RRPR2RRR[tnumpytargsortRRRSR.RTRUtminRZtrangeR-( RR R R)RVt num_samplestnum_dimst num_classesRZtj((sL/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/metric.pyR!Ûs !$   $  @N(R#R4R5R&RR!(((sL/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/metric.pyRY®s"t_BinaryClassificationMetricscB@steZdZd„Zd„Zed„ƒZed„ƒZed„ƒZed„ƒZ ed„ƒZ d„Z RS( s3 Private container class for classification metric statistics. True/false positive and true/false negative counts are sufficient statistics for various classification metrics. This class provides the machinery to track those statistics across mini-batches of (label, prediction) pairs. cC@s(d|_d|_d|_d|_dS(Ni(ttrue_positivestfalse_negativestfalse_positivesttrue_negatives(R((sL/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/metric.pyRs   cC@s |jƒ}|jƒjdƒ}tj|ddƒ}t||ƒttj|ƒƒdkrwtd|jj ƒ‚n|dk}d|}|dk}d|}|j ||j ƒ7_ |j ||j ƒ7_ |j ||j ƒ7_ |j||j ƒ7_dS(s Update various binary classification counts for a single (label, pred) pair. Parameters ---------- label : `NDArray` The labels of the data. pred : `NDArray` Predicted values. RORNiis1%s currently only supports binary classification.N(RRRSR]RQRRtuniqueRR"R#RfRURhRgRi(RR)R*RVt pred_truet pred_falset label_truet label_false((sL/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/metric.pytupdate_binary_statss      cC@s9|j|jdkr1t|jƒ|j|jSdSdS(Nig(RfRhR0(R((sL/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/metric.pyt precision&scC@s9|j|jdkr1t|jƒ|j|jSdSdS(Nig(RfRgR0(R((sL/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/metric.pytrecall-scC@s>|j|jdkr6d|j|j|j|jSdSdS(Niig(RpRq(R((sL/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/metric.pytfscore4s cC@sµ|js dSt|jƒ}t|jƒ}t|jƒ}t|jƒ}||||||||g}d}x$td„|ƒD]}||9}q„W||||tj|ƒS(s@ Calculate the Matthew's Correlation Coefficent ggð?cS@s |dkS(Ng((tt((sL/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/metric.pytLs( ttotal_examplesR0RfRhRgRitfiltertmathtsqrt(Rttrue_post false_post false_negttrue_negttermstdenomRs((sL/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/metric.pyt matthewscc;s  cC@s|j|j|j|jS(N(RgRhRiRf(R((sL/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/metric.pyRuPscC@s(d|_d|_d|_d|_dS(Ni(RhRgRfRi(R((sL/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/metric.pyt reset_statsUs   ( R#R4R5RRotpropertyRpRqRrRRuR€(((sL/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/metric.pyReùs  tF1cB@s5eZdZddddd„Zd„Zd„ZRS(sµComputes the F1 score of a binary classification problem. The F1 score is equivalent to harmonic mean of the precision and recall, where the best value is 1.0 and the worst value is 0.0. The formula for F1 score is:: F1 = 2 * (precision * recall) / (precision + recall) The formula for precision and recall is:: precision = true_positives / (true_positives + false_positives) recall = true_positives / (true_positives + false_negatives) .. note:: This F1 score only supports binary classification. Parameters ---------- name : str Name of this metric instance for display. output_names : list of str, or None Name of predictions that should be used when updating with update_dict. By default include all predictions. label_names : list of str, or None Name of labels that should be used when updating with update_dict. By default include all labels. average : str, default 'macro' Strategy to be used for aggregating across mini-batches. "macro": average the F1 scores for each batch. "micro": compute a single F1 score across all batches. Examples -------- >>> predicts = [mx.nd.array([[0.3, 0.7], [0., 1.], [0.4, 0.6]])] >>> labels = [mx.nd.array([0., 1., 1.])] >>> f1 = mx.metric.F1() >>> f1.update(preds = predicts, labels = labels) >>> print f1.get() ('f1', 0.8) tf1tmacrocC@s8||_tƒ|_tj|d|d|d|ƒdS(NRRR(taverageReRARR(RRRRR…((sL/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/metric.pyR‡s  cC@sºt||tƒ\}}x0t||ƒD]\}}|jj||ƒq(W|jdkrŽ|j|jj7_|jd7_|jj ƒn(|jj|jj |_|jj |_dS(sßUpdates the internal evaluation result. Parameters ---------- labels : list of `NDArray` The labels of the data. preds : list of `NDArray` Predicted values. R„iN( RRPR2RARoR…R.RrR-R€Ru(RR R R)R*((sL/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/metric.pyR!Žs cC@s#d|_d|_|jjƒdS(s7Resets the internal evaluation result to initial state.gN(R.R-RAR€(R((sL/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/metric.pyR¦s  N(R#R4R5R&RR!R(((sL/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/metric.pyR‚\s ) tMCCcB@s5eZdZddddd„Zd„Zd„ZRS(sïComputes the Matthews Correlation Coefficient of a binary classification problem. While slower to compute than F1 the MCC can give insight that F1 or Accuracy cannot. For instance, if the network always predicts the same result then the MCC will immeadiately show this. The MCC is also symetric with respect to positive and negative categorization, however, there needs to be both positive and negative examples in the labels or it will always return 0. MCC of 0 is uncorrelated, 1 is completely correlated, and -1 is negatively correlated. .. math:: \text{MCC} = \frac{ TP \times TN - FP \times FN } {\sqrt{ (TP + FP) ( TP + FN ) ( TN + FP ) ( TN + FN ) } } where 0 terms in the denominator are replaced by 1. .. note:: This version of MCC only supports binary classification. Parameters ---------- name : str Name of this metric instance for display. output_names : list of str, or None Name of predictions that should be used when updating with update_dict. By default include all predictions. label_names : list of str, or None Name of labels that should be used when updating with update_dict. By default include all labels. average : str, default 'macro' Strategy to be used for aggregating across mini-batches. "macro": average the MCC for each batch. "micro": compute a single MCC across all batches. Examples -------- >>> # In this example the network almost always predicts positive >>> false_positives = 1000 >>> false_negatives = 1 >>> true_positives = 10000 >>> true_negatives = 1 >>> predicts = [mx.nd.array( [[.3, .7]]*false_positives + [[.7, .3]]*true_negatives + [[.7, .3]]*false_negatives + [[.3, .7]]*true_positives )] >>> labels = [mx.nd.array( [0.]*(false_positives + true_negatives) + [1.]*(false_negatives + true_positives) )] >>> f1 = mx.metric.F1() >>> f1.update(preds = predicts, labels = labels) >>> mcc = mx.metric.MCC() >>> mcc.update(preds = predicts, labels = labels) >>> print f1.get() ('f1', 0.95233560306652054) >>> print mcc.get() ('mcc', 0.01917751877733392) tmccR„cC@s8||_tƒ|_tj|d|d|d|ƒdS(NRRR(t_averageRet_metricsRR(RRRRR…((sL/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/metric.pyRìs  cC@sºt||tƒ\}}x0t||ƒD]\}}|jj||ƒq(W|jdkrŽ|j|jj7_|jd7_|jj ƒn(|jj|jj |_|jj |_dS(sßUpdates the internal evaluation result. Parameters ---------- labels : list of `NDArray` The labels of the data. preds : list of `NDArray` Predicted values. R„iN( RRPR2R‰RoRˆR.RR-R€Ru(RR R R)R*((sL/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/metric.pyR!ós cC@s#d|_d|_|jjƒdS(s7Resets the internal evaluation result to initial state.gN(R.R-R‰R€(R((sL/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/metric.pyR s  N(R#R4R5R&RR!R(((sL/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/metric.pyR†­s = t PerplexitycB@s5eZdZddddd„Zd„Zd„ZRS(sYComputes perplexity. Perplexity is a measurement of how well a probability distribution or model predicts a sample. A low perplexity indicates the model is good at predicting the sample. The perplexity of a model q is defined as .. math:: b^{\big(-\frac{1}{N} \sum_{i=1}^N \log_b q(x_i) \big)} = \exp \big(-\frac{1}{N} \sum_{i=1}^N \log q(x_i)\big) where we let `b = e`. :math:`q(x_i)` is the predicted value of its ground truth label on sample :math:`x_i`. For example, we have three samples :math:`x_1, x_2, x_3` and their labels are :math:`[0, 1, 1]`. Suppose our model predicts :math:`q(x_1) = p(y_1 = 0 | x_1) = 0.3` and :math:`q(x_2) = 1.0`, :math:`q(x_3) = 0.6`. The perplexity of model q is :math:`exp\big(-(\log 0.3 + \log 1.0 + \log 0.6) / 3\big) = 1.77109762852`. Parameters ---------- ignore_label : int or None Index of invalid label to ignore when counting. By default, sets to -1. If set to `None`, it will include all entries. axis : int (default -1) The axis from prediction that was used to compute softmax. By default use the last axis. name : str Name of this metric instance for display. output_names : list of str, or None Name of predictions that should be used when updating with update_dict. By default include all predictions. label_names : list of str, or None Name of labels that should be used when updating with update_dict. By default include all labels. Examples -------- >>> predicts = [mx.nd.array([[0.3, 0.7], [0, 1.], [0.4, 0.6]])] >>> labels = [mx.nd.array([0, 1, 1])] >>> perp = mx.metric.Perplexity(ignore_label=None) >>> perp.update(labels, predicts) >>> print perp.get() ('Perplexity', 1.7710976285155853) iÿÿÿÿt perplexitycC@s>tt|ƒj|d|d|d|ƒ||_||_dS(Nt ignore_labelRR(R@RŠRRŒRN(RRŒRNRRR((sL/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/metric.pyRHs    cC@s€t|ƒt|ƒkst‚d}d}x1t||ƒD] \}}|j|j|jdkstd|j|jfƒ‚|j|jƒj|jfƒ}tj ||j ddƒd|j ƒ}|j d k r||j kj |jƒ}|tj|ƒjƒ8}|d||}n|tjtjtjd |ƒƒƒjƒ8}||j7}q:W|j|7_|j|7_d S( sßUpdates the internal evaluation result. Parameters ---------- labels : list of `NDArray` The labels of the data. preds : list of `NDArray` Predicted values. giiÿÿÿÿsshape mismatch: %s vs. %stdtypeRORNig»½×Ùß|Û=N(RR[R2tsizeRt as_in_contexttcontexttreshapeRtpickRSRNRŒR&RRUtasscalartlogtmaximumR.R-(RR R tlosstnumR)R*tignore((sL/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/metric.pyR!Ps  !'.cC@s |jtj|j|jƒfS(s±Returns the current evaluation result. Returns ------- Tuple of (str, float) Representing name of the metric and evaluation result. (RRwtexpR.R-(R((sL/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/metric.pyR1lsN(R#R4R5R&RR!R1(((sL/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/metric.pyRŠs 5 tMAEcB@s)eZdZdddd„Zd„ZRS(sˆComputes Mean Absolute Error (MAE) loss. The mean absolute error is given by .. math:: \frac{\sum_i^n |y_i - \hat{y}_i|}{n} Parameters ---------- name : str Name of this metric instance for display. output_names : list of str, or None Name of predictions that should be used when updating with update_dict. By default include all predictions. label_names : list of str, or None Name of labels that should be used when updating with update_dict. By default include all labels. Examples -------- >>> predicts = [mx.nd.array(np.array([3, -0.5, 2, 7]).reshape(4,1))] >>> labels = [mx.nd.array(np.array([2.5, 0.0, 2, 8]).reshape(4,1))] >>> mean_absolute_error = mx.metric.MAE() >>> mean_absolute_error.update(labels = labels, preds = predicts) >>> print mean_absolute_error.get() ('mae', 0.5) tmaecC@s&tt|ƒj|d|d|ƒdS(NRR(R@RšR(RRRR((sL/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/metric.pyR™scC@sçt||tƒ\}}xÈt||ƒD]·\}}|jƒ}|jƒ}t|jƒdkr}|j|jddƒ}nt|jƒdkr®|j|jddƒ}n|jtj ||ƒj ƒ7_|j d7_ q(WdS(sßUpdates the internal evaluation result. Parameters ---------- labels : list of `NDArray` The labels of the data. preds : list of `NDArray` Predicted values. iiN( RRPR2RRRRR‘R.R]tabstmeanR-(RR R R)R*((sL/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/metric.pyR!žs   "N(R#R4R5R&RR!(((sL/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/metric.pyRš{stMSEcB@s)eZdZdddd„Zd„ZRS(s‡Computes Mean Squared Error (MSE) loss. The mean squared error is given by .. math:: \frac{\sum_i^n (y_i - \hat{y}_i)^2}{n} Parameters ---------- name : str Name of this metric instance for display. output_names : list of str, or None Name of predictions that should be used when updating with update_dict. By default include all predictions. label_names : list of str, or None Name of labels that should be used when updating with update_dict. By default include all labels. Examples -------- >>> predicts = [mx.nd.array(np.array([3, -0.5, 2, 7]).reshape(4,1))] >>> labels = [mx.nd.array(np.array([2.5, 0.0, 2, 8]).reshape(4,1))] >>> mean_squared_error = mx.metric.MSE() >>> mean_squared_error.update(labels = labels, preds = predicts) >>> print mean_squared_error.get() ('mse', 0.375) tmsecC@s&tt|ƒj|d|d|ƒdS(NRR(R@RžR(RRRR((sL/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/metric.pyRÕscC@sât||tƒ\}}xÃt||ƒD]²\}}|jƒ}|jƒ}t|jƒdkr}|j|jddƒ}nt|jƒdkr®|j|jddƒ}n|j||djƒ7_|j d7_ q(WdS(sßUpdates the internal evaluation result. Parameters ---------- labels : list of `NDArray` The labels of the data. preds : list of `NDArray` Predicted values. iig@N( RRPR2RRRRR‘R.RR-(RR R R)R*((sL/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/metric.pyR!Ús   N(R#R4R5R&RR!(((sL/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/metric.pyRž¸stRMSEcB@s)eZdZdddd„Zd„ZRS(s²Computes Root Mean Squred Error (RMSE) loss. The root mean squared error is given by .. math:: \sqrt{\frac{\sum_i^n (y_i - \hat{y}_i)^2}{n}} Parameters ---------- name : str Name of this metric instance for display. output_names : list of str, or None Name of predictions that should be used when updating with update_dict. By default include all predictions. label_names : list of str, or None Name of labels that should be used when updating with update_dict. By default include all labels. Examples -------- >>> predicts = [mx.nd.array(np.array([3, -0.5, 2, 7]).reshape(4,1))] >>> labels = [mx.nd.array(np.array([2.5, 0.0, 2, 8]).reshape(4,1))] >>> root_mean_squared_error = mx.metric.RMSE() >>> root_mean_squared_error.update(labels = labels, preds = predicts) >>> print root_mean_squared_error.get() ('rmse', 0.612372457981) trmsecC@s&tt|ƒj|d|d|ƒdS(NRR(R@R R(RRRR((sL/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/metric.pyRscC@sët||tƒ\}}xÌt||ƒD]»\}}|jƒ}|jƒ}t|jƒdkr}|j|jddƒ}nt|jƒdkr®|j|jddƒ}n|jtj ||dj ƒƒ7_|j d7_ q(WdS(sßUpdates the internal evaluation result. Parameters ---------- labels : list of `NDArray` The labels of the data. preds : list of `NDArray` Predicted values. iig@N( RRPR2RRRRR‘R.R]RxRR-(RR R R)R*((sL/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/metric.pyR!s   &N(R#R4R5R&RR!(((sL/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/metric.pyR ôstcet CrossEntropycB@s,eZdZddddd„Zd„ZRS(sµComputes Cross Entropy loss. The cross entropy over a batch of sample size :math:`N` is given by .. math:: -\sum_{n=1}^{N}\sum_{k=1}^{K}t_{nk}\log (y_{nk}), where :math:`t_{nk}=1` if and only if sample :math:`n` belongs to class :math:`k`. :math:`y_{nk}` denotes the probability of sample :math:`n` belonging to class :math:`k`. Parameters ---------- eps : float Cross Entropy loss is undefined for predicted value is 0 or 1, so predicted values are added with the small constant. name : str Name of this metric instance for display. output_names : list of str, or None Name of predictions that should be used when updating with update_dict. By default include all predictions. label_names : list of str, or None Name of labels that should be used when updating with update_dict. By default include all labels. Examples -------- >>> predicts = [mx.nd.array([[0.3, 0.7], [0, 1.], [0.4, 0.6]])] >>> labels = [mx.nd.array([0, 1, 1])] >>> ce = mx.metric.CrossEntropy() >>> ce.update(labels, predicts) >>> print ce.get() ('cross-entropy', 0.57159948348999023) gê-™—q=s cross-entropycC@s5tt|ƒj|d|d|d|ƒ||_dS(NtepsRR(R@R£RR¤(RR¤RRR((sL/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/metric.pyRUs  cC@såt||tƒ\}}xÆt||ƒD]µ\}}|jƒ}|jƒ}|jƒ}|jd|jdksxt‚|tj|jdƒtj |ƒf}|j tj ||j ƒ j ƒ7_ |j|jd7_q(WdS(sßUpdates the internal evaluation result. Parameters ---------- labels : list of `NDArray` The labels of the data. preds : list of `NDArray` Predicted values. iN(RRPR2RRtravelRR[R]tarangetint64R.R”R¤RUR-(RR R R)R*tprob((sL/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/metric.pyR!\s     )&N(R#R4R5R&RR!(((sL/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/metric.pyR£0s$tnll_losstNegativeLogLikelihoodcB@s,eZdZddddd„Zd„ZRS(sComputes the negative log-likelihood loss. The negative log-likelihoodd loss over a batch of sample size :math:`N` is given by .. math:: -\sum_{n=1}^{N}\sum_{k=1}^{K}t_{nk}\log (y_{nk}), where :math:`K` is the number of classes, :math:`y_{nk}` is the prediceted probability for :math:`k`-th class for :math:`n`-th sample. :math:`t_{nk}=1` if and only if sample :math:`n` belongs to class :math:`k`. Parameters ---------- eps : float Negative log-likelihood loss is undefined for predicted value is 0, so predicted values are added with the small constant. name : str Name of this metric instance for display. output_names : list of str, or None Name of predictions that should be used when updating with update_dict. By default include all predictions. label_names : list of str, or None Name of labels that should be used when updating with update_dict. By default include all labels. Examples -------- >>> predicts = [mx.nd.array([[0.3, 0.7], [0, 1.], [0.4, 0.6]])] >>> labels = [mx.nd.array([0, 1, 1])] >>> nll_loss = mx.metric.NegativeLogLikelihood() >>> nll_loss.update(labels, predicts) >>> print nll_loss.get() ('nll-loss', 0.57159948348999023) gê-™—q=snll-losscC@s5tt|ƒj|d|d|d|ƒ||_dS(NR¤RR(R@RªRR¤(RR¤RRR((sL/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/metric.pyR™s  cC@sùt||tƒ\}}xÚt||ƒD]É\}}|jƒ}|jƒ}|jƒ}|jd}|jd|ks‘t|jd|fƒ‚|tj|dtj ƒtj |ƒf}|j tj ||j ƒ j ƒ7_ |j|7_q(WdS(sßUpdates the internal evaluation result. Parameters ---------- labels : list of `NDArray` The labels of the data. preds : list of `NDArray` Predicted values. iRN(RRPR2RRR¥RR[R]R¦R§R.R”R¤RUR-(RR R R)R*t num_examplesR¨((sL/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/metric.pyR! s     ,+&N(R#R4R5R&RR!(((sL/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/metric.pyRªts$tpearsonrtPearsonCorrelationcB@s)eZdZdddd„Zd„ZRS(sbComputes Pearson correlation. The pearson correlation is given by .. math:: \frac{cov(y, \hat{y})}{\sigma{y}\sigma{\hat{y}}} Parameters ---------- name : str Name of this metric instance for display. output_names : list of str, or None Name of predictions that should be used when updating with update_dict. By default include all predictions. label_names : list of str, or None Name of labels that should be used when updating with update_dict. By default include all labels. Examples -------- >>> predicts = [mx.nd.array([[0.3, 0.7], [0, 1.], [0.4, 0.6]])] >>> labels = [mx.nd.array([[1, 0], [0, 1], [0, 1]])] >>> pr = mx.metric.PearsonCorrelation() >>> pr.update(labels, predicts) >>> print pr.get() ('pearson-correlation', 0.42163704544016178) R¬cC@s&tt|ƒj|d|d|ƒdS(NRR(R@R­R(RRRR((sL/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/metric.pyRÖscC@s¡t||tƒ\}}x‚t||ƒD]q\}}t||ttƒ|jƒ}|jƒ}|jtj|jƒ|jƒƒd7_|j d7_ q(WdS(sÞUpdates the internal evaluation result. Parameters ---------- labels : list of `NDArray` The labels of the data. preds : list of `NDArray` Predicted values. iiN(ii( RRPR2tFalseRRR.R]tcorrcoefR¥R-(RR R R)R*((sL/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/metric.pyR!Ûs   +N(R#R4R5R&RR!(((sL/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/metric.pyR­¸stLosscB@s)eZdZdddd„Zd„ZRS(sÊDummy metric for directly printing loss. Parameters ---------- name : str Name of this metric instance for display. output_names : list of str, or None Name of predictions that should be used when updating with update_dict. By default include all predictions. label_names : list of str, or None Name of labels that should be used when updating with update_dict. By default include all labels. R–cC@s&tt|ƒj|d|d|ƒdS(NRR(R@R°R(RRRR((sL/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/metric.pyRþscC@sft|tjjƒr!|g}nx>|D]6}|jtj|ƒjƒ7_|j|j7_q(WdS(N(R RR R.RUR“R-RŽ(Rt_R R*((sL/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/metric.pyR!s   N(R#R4R5R&RR!(((sL/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/metric.pyR°ïstTorchcB@s eZdZdddd„ZRS(s"Dummy metric for torch criterions.ttorchcC@s&tt|ƒj|d|d|ƒdS(NRR(R@R²R(RRRR((sL/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/metric.pyRsN(R#R4R5R&R(((sL/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/metric.pyR² stCaffecB@s eZdZdddd„ZRS(s"Dummy metric for caffe criterions.tcaffecC@s&tt|ƒj|d|d|ƒdS(NRR(R@R´R(RRRR((sL/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/metric.pyRsN(R#R4R5R&R(((sL/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/metric.pyR´sR7cB@s5eZdZdeddd„Zd„Zd„ZRS(s7Computes a customized evaluation metric. The `feval` function can return a `tuple` of (sum_metric, num_inst) or return an `int` sum_metric. Parameters ---------- feval : callable(label, pred) Customized evaluation function. name : str, optional The name of the metric. (the default is None). allow_extra_outputs : bool, optional If true, the prediction outputs can have extra outputs. This is useful in RNN, where the states are also produced in outputs for forwarding. (the default is False). name : str Name of this metric instance for display. output_names : list of str, or None Name of predictions that should be used when updating with update_dict. By default include all predictions. label_names : list of str, or None Name of labels that should be used when updating with update_dict. By default include all labels. Examples -------- >>> predicts = [mx.nd.array(np.array([3, -0.5, 2, 7]).reshape(4,1))] >>> labels = [mx.nd.array(np.array([2.5, 0.0, 2, 8]).reshape(4,1))] >>> feval = lambda x, y : (x + y).mean() >>> eval_metrics = mx.metric.CustomMetric(feval=feval) >>> eval_metrics.update(labels, predicts) >>> print eval_metrics.get() ('custom()', 6.0) c C@s~|dkr:|j}|jdƒdkr:d|}q:ntt|ƒj|d|d|d|d|ƒ||_||_dS(Nt>> def custom_metric(label, pred): ... return np.mean(np.abs(label-pred)) ... >>> metric = mx.metric.np(custom_metric) c@s ˆ||ƒS(sInternal eval function.((R)R*(t numpy_feval(sL/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/metric.pyR·‹s(R#R7(R¾RR¸R·((R¾sL/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/metric.pytnpps (+R5t __future__RRwt collectionsRR]tbaseRRtRRR®RtobjectRtget_register_functregistertget_alias_functaliastget_create_funcR;R:R8RLRYReR‚R†RŠRšRžR R£RªR­R°R²R´R7R&R¿(((sL/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/metric.pytsj  #s * € CIcPdh<;; B B 5P