ó šÄïYc@@s¯dZddlmZddlZddlmZddlZddlmZm Z ddl m Z ddl m Z dd „Z d efd „ƒYZe jed ƒZe jed ƒZe jed ƒZd „Zeedƒdefd„ƒYƒƒZeedƒdefd„ƒYƒƒZeeddƒdefd„ƒYƒƒZedefd„ƒYƒZedefd„ƒYƒZedefd„ƒYƒZedefd„ƒYƒZed efd!„ƒYƒZeed"ƒd#efd$„ƒYƒƒZeed%ƒd&efd'„ƒYƒƒZ ed(efd)„ƒYƒZ!ed*e!fd+„ƒYƒZ"ed,e!fd-„ƒYƒZ#ed.efd/„ƒYƒZ$de&d0„Z'dS(1s Online evaluation metric module.i(tabsolute_importN(t OrderedDicti(t numeric_typest string_types(tndarray(tregistrycC@sf|dkr(t|ƒt|ƒ}}n|j|j}}||krbtdj||ƒƒ‚ndS(Nis9Shape of labels {} does not match shape of predictions {}(tlentshapet ValueErrortformat(tlabelstpredsRt label_shapet pred_shape((s,build/bdist.linux-armv7l/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((s,build/bdist.linux-armv7l/egg/mxnet/metric.pyt__init__@s    cC@sdjt|jƒƒƒS(NsEvalMetric: {}(R tdicttget_name_value(R((s,build/bdist.linux-armv7l/egg/mxnet/metric.pyt__str__HscC@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((s,build/bdist.linux-armv7l/egg/mxnet/metric.pyt get_configKs    cC@sœ|jdk r2g|jD]}||^q}nt|jƒƒ}|jdk rvg|jD]}||^q]}nt|jƒƒ}|j||ƒdS(sUpdate the internal evaluation with named label and pred Parameters ---------- labels : OrderedDict of str -> NDArray name to array mapping for labels. preds : list of NDArray name to array mapping of predicted outputs. N(RtNonetlisttvaluesRR(RtlabeltpredR((s,build/bdist.linux-armv7l/egg/mxnet/metric.pyt update_dictWs ##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 ((s,build/bdist.linux-armv7l/egg/mxnet/metric.pyRns cC@sd|_d|_dS(s7Resets the internal evaluation result to initial state.igN(tnum_instt sum_metric(R((s,build/bdist.linux-armv7l/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((s,build/bdist.linux-armv7l/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. (R.t isinstanceR$tzip(RRtvalue((s,build/bdist.linux-armv7l/egg/mxnet/metric.pyRs   N( R t __module__t__doc__R#RRR"R(RRR.R(((s,build/bdist.linux-armv7l/egg/mxnet/metric.pyR,s     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((s,build/bdist.linux-armv7l/egg/mxnet/metric.pyR8¥s   t compositeR6cB@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|ƒjdd|d|ƒ|dkr7g}ng|D]}t|ƒ^q>|_dS(NR=RR(tsuperR6RR#R8tmetrics(RR?RRRti((s,build/bdist.linux-armv7l/egg/mxnet/metric.pyRïs   cC@s|jjt|ƒƒdS(srAdds a child metric. Parameters ---------- metric A metric instance. N(R?tappendR8(RR((s,build/bdist.linux-armv7l/egg/mxnet/metric.pyR7÷scC@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(R?t IndexErrorRR R(Rtindex((s,build/bdist.linux-armv7l/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#RtitemsRR?R((RR R R@R((s,build/bdist.linux-armv7l/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(R?R(RR R R((s,build/bdist.linux-armv7l/egg/mxnet/metric.pyRs cC@s:y"x|jD]}|jƒq WWntk r5nXdS(s7Resets the internal evaluation result to initial state.N(R?RtAttributeError(RR((s,build/bdist.linux-armv7l/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. (R?R.R/RRtextend(RtnamesR%RRR1((s,build/bdist.linux-armv7l/egg/mxnet/metric.pyR.0s    cC@sItt|ƒjƒ}|jig|jD]}|jƒ^q(d6ƒ|S(NR?(R>R6R"RR?(RR!R@((s,build/bdist.linux-armv7l/egg/mxnet/metric.pyR"Fs0N( R R2R3R#RR7RDR(RRR.R"(((s,build/bdist.linux-armv7l/egg/mxnet/metric.pyR6Ï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>RJRRL(RRLRRR((s,build/bdist.linux-armv7l/egg/mxnet/metric.pyRrs  cC@sÎt||ƒxºt||ƒD]©\}}|j|jkrVtj|d|jƒ}n|jƒjdƒ}|jƒjdƒ}t||ƒ|j|j |j kj ƒ7_|j t |j ƒ7_ qWdS(sßUpdates the internal evaluation result. Parameters ---------- labels : list of `NDArray` The labels of the data. preds : list of `NDArray` Predicted values. RLtint32N( RR0RRtargmaxRLtasnumpytastypeR+tflattsumR*R(RR R R&t pred_label((s,build/bdist.linux-armv7l/egg/mxnet/metric.pyRys  !N(R R2R3R#RR(((s,build/bdist.linux-armv7l/egg/mxnet/metric.pyRJQs 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) iRTcC@sftt|ƒj|d|d|d|ƒ||_|jdksLtdƒ‚|jd|j7_dS(Nttop_kRRis.Please use Accuracy if top_k is no more than 1s_%d(R>RVRRWtAssertionErrorR(RRWRRR((s,build/bdist.linux-armv7l/egg/mxnet/metric.pyR¶s    c C@sqt||ƒx]t||ƒD]L\}}t|jƒdksJtdƒ‚tj|jƒjdƒddƒ}|jƒjdƒ}t||ƒ|jd}t|jƒ}|dkrÜ|j |j |j kj ƒ7_ n~|dkrZ|jd}t ||j ƒ}xPt|ƒD]?} |j |dd…|d| fj |j kj ƒ7_ qWn|j|7_qWdS( 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 dimstfloat32RLiRMiN(RR0RRRXtnumpytargsortRORPR+RQRRtminRWtrangeR*( RR R R&RSt num_samplestnum_dimst num_classesRWtj((s,build/bdist.linux-armv7l/egg/mxnet/metric.pyR¿s !$   $  @N(R R2R3R#RR(((s,build/bdist.linux-armv7l/egg/mxnet/metric.pyRV’s"tF1cB@s)eZdZdddd„Zd„ZRS(sÙComputes the F1 score of a binary classification problem. The F1 score is equivalent to weighted average 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. 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.F1() >>> acc.update(preds = predicts, labels = labels) >>> print acc.get() ('f1', 0.8) tf1cC@s&tt|ƒj|d|d|ƒdS(NRR(R>RbR(RRRR((s,build/bdist.linux-armv7l/egg/mxnet/metric.pyRscC@s×t||ƒxÃt||ƒD]²\}}|jƒ}|jƒjdƒ}tj|ddƒ}t||ƒttj|ƒƒdkr–tdƒ‚nd \}}}xŒt||ƒD]{\} } | dkræ| dkræ|d7}qµ| dkr | dkr |d7}qµ| dkrµ| dkrµ|d7}qµqµW||dkrU|||} nd} ||dkr||||} nd} | | dkr«d| | | | } nd} |j | 7_ |j d7_ qWd S( sßUpdates the internal evaluation result. Parameters ---------- labels : list of `NDArray` The labels of the data. preds : list of `NDArray` Predicted values. RMRLiis1F1 currently only supports binary classification.ggð?iN(ggg( RR0RORPRZRNRtuniqueRR+R*(RR R R&R'RSttrue_positivestfalse_positivestfalse_negativesty_predty_truet precisiontrecalltf1_score((s,build/bdist.linux-armv7l/egg/mxnet/metric.pyR s6     N(R R2R3R#RR(((s,build/bdist.linux-armv7l/egg/mxnet/metric.pyRbÝ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>RmRRoRL(RRoRLRRR((s,build/bdist.linux-armv7l/egg/mxnet/metric.pyRrs    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. %stdtypeRMRLig»½×Ùß|Û=N(RRXR0tsizeRt as_in_contexttcontexttreshapeRtpickRPRLRoR#RpRRtasscalartlogtmaximumR+R*(RR R tlosstnumR&R'tignore((s,build/bdist.linux-armv7l/egg/mxnet/metric.pyRzs  !'.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. (RtmathtexpR+R*(R((s,build/bdist.linux-armv7l/egg/mxnet/metric.pyR.–sN(R R2R3R#RRR.(((s,build/bdist.linux-armv7l/egg/mxnet/metric.pyRm<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((s,build/bdist.linux-armv7l/egg/mxnet/metric.pyRÃscC@s«t||ƒx—t||ƒD]†\}}|jƒ}|jƒ}t|jƒdkrr|j|jddƒ}n|jtj||ƒj ƒ7_|j d7_ qWdS(sßUpdates the internal evaluation result. Parameters ---------- labels : list of `NDArray` The labels of the data. preds : list of `NDArray` Predicted values. iiN( RR0RORRRtR+RZtabstmeanR*(RR R R&R'((s,build/bdist.linux-armv7l/egg/mxnet/metric.pyRÈs   "N(R R2R3R#RR(((s,build/bdist.linux-armv7l/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((s,build/bdist.linux-armv7l/egg/mxnet/metric.pyRýscC@s¦t||ƒx’t||ƒD]\}}|jƒ}|jƒ}t|jƒdkrr|j|jddƒ}n|j||djƒ7_|jd7_qWdS(sßUpdates the internal evaluation result. Parameters ---------- labels : list of `NDArray` The labels of the data. preds : list of `NDArray` Predicted values. iig@N( RR0RORRRtR+RR*(RR R R&R'((s,build/bdist.linux-armv7l/egg/mxnet/metric.pyRs   N(R R2R3R#RR(((s,build/bdist.linux-armv7l/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((s,build/bdist.linux-armv7l/egg/mxnet/metric.pyR7scC@s¯t||ƒx›t||ƒD]Š\}}|jƒ}|jƒ}t|jƒdkrr|j|jddƒ}n|jtj||dj ƒƒ7_|j d7_ qWdS(sßUpdates the internal evaluation result. Parameters ---------- labels : list of `NDArray` The labels of the data. preds : list of `NDArray` Predicted values. iig@N( RR0RORRRtR+RZtsqrtRR*(RR R R&R'((s,build/bdist.linux-armv7l/egg/mxnet/metric.pyR<s   &N(R R2R3R#RR(((s,build/bdist.linux-armv7l/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:Œ0âŽyE>s cross-entropycC@s5tt|ƒj|d|d|d|ƒ||_dS(NtepsRR(R>RˆRR‰(RR‰RRR((s,build/bdist.linux-armv7l/egg/mxnet/metric.pyRys  cC@sÚt||ƒxÆt||ƒD]µ\}}|jƒ}|jƒ}|jƒ}|jd|jdksmt‚|tj|jdƒtj|ƒf}|j tj ||j ƒ j ƒ7_ |j |jd7_ qWdS(sßUpdates the internal evaluation result. Parameters ---------- labels : list of `NDArray` The labels of the data. preds : list of `NDArray` Predicted values. iN(RR0ROtravelRRXRZtarangetint64R+RwR‰RRR*(RR R R&R'tprob((s,build/bdist.linux-armv7l/egg/mxnet/metric.pyR€s     )&N(R R2R3R#RR(((s,build/bdist.linux-armv7l/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>RR(RRRR((s,build/bdist.linux-armv7l/egg/mxnet/metric.pyR·scC@s“t||ƒxt||ƒD]n\}}t||dƒ|jƒ}|jƒ}|jtj|jƒ|jƒƒd7_|jd7_qWdS(sÞUpdates the internal evaluation result. Parameters ---------- labels : list of `NDArray` The labels of the data. preds : list of `NDArray` Predicted values. iiN(ii(RR0ROR+RZtcorrcoefRŠR*(RR R R&R'((s,build/bdist.linux-armv7l/egg/mxnet/metric.pyR¼s   +N(R R2R3R#RR(((s,build/bdist.linux-armv7l/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. RycC@s&tt|ƒj|d|d|ƒdS(NRR(R>R‘R(RRRR((s,build/bdist.linux-armv7l/egg/mxnet/metric.pyRÞscC@sEx>|D]6}|jtj|ƒjƒ7_|j|j7_qWdS(N(R+RRRRvR*Rq(Rt_R R'((s,build/bdist.linux-armv7l/egg/mxnet/metric.pyRãs N(R R2R3R#RR(((s,build/bdist.linux-armv7l/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((s,build/bdist.linux-armv7l/egg/mxnet/metric.pyRìsN(R R2R3R#R(((s,build/bdist.linux-armv7l/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((s,build/bdist.linux-armv7l/egg/mxnet/metric.pyRõsN(R R2R3R#R(((s,build/bdist.linux-armv7l/egg/mxnet/metric.pyR•òsR5cB@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(NtR5Rt_fevalt_allow_extra_outputs(RR˜RR™RR((s,build/bdist.linux-armv7l/egg/mxnet/metric.pyRs     cC@sÁ|jst||ƒnx¡t||ƒD]\}}|jƒ}|jƒ}|j||ƒ}t|tƒr›|\}}|j|7_|j|7_q)|j|7_|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( RœRR0ROR›R/ttupleR+R*(RR R R'R&trevalR+R*((s,build/bdist.linux-armv7l/egg/mxnet/metric.pyR,s    cC@stdƒ‚dS(Ns!CustomMetric cannot be serialized(R)(R((s,build/bdist.linux-armv7l/egg/mxnet/metric.pyR"GsN(R R2R3R#tFalseRRR"(((s,build/bdist.linux-armv7l/egg/mxnet/metric.pyR5ûs # c@s+‡fd†}ˆj|_t|||ƒS(sÆCreates a custom evaluation metric that receives its inputs as numpy arrays. Parameters ---------- numpy_feval : callable(label, pred) Custom evaluation function that receives labels and predictions for a minibatch as numpy arrays and returns the corresponding custom metric as a floating point number. name : str, optional Name of the custom metric. allow_extra_outputs : bool, optional Whether prediction output is allowed to have extra outputs. This is useful in cases like RNN where states are also part of output which can then be fed back to the RNN in the next step. By default, extra outputs are not allowed. Returns ------- float Custom metric corresponding to the provided labels and predictions. Example ------- >>> 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(s,build/bdist.linux-armv7l/egg/mxnet/metric.pyR˜gs(R R5(R RR™R˜((R s,build/bdist.linux-armv7l/egg/mxnet/metric.pytnpLs ((R3t __future__RR|t collectionsRRZtbaseRRtRRRtobjectRtget_register_functregistertget_alias_functaliastget_create_funcR9R8R6RJRVRbRmR~R‚R„RˆRR‘R“R•R5R#RŸR¡(((s,build/bdist.linux-armv7l/egg/mxnet/metric.pyts^   s * € ?I^h:99 C 4P