Computes the Top-k accuracy (target is in the top k predictions).
489804
def top_k_accuracy(input:Tensor, targs:Tensor, k:int=5)->Rank0Tensor:
    ""
    input = input.topk(k=k, dim=-1)[1]
    targs = targs.unsqueeze(dim=-1).expand_as(input)
    return (input == targs).max(dim=-1)[0].float().mean()
593423
public void compute() 
	{
		if (!computable) {
			throw new RuntimeException("can't re-'compute' experiment results after a 'restore'");
		}
		expt = new MatchExpt[blockers.size()][learners.size()][datasets.size()];
		for (int i=0; i<blockers.size(); i++) {
			Blocker blocker = (Blocker)blockers.get(i);
			for (int j=0; j<learners.size(); j++) {
				StringDistanceLearner distance = (StringDistanceLearner)learners.get(j);
				for (int k=0; k<datasets.size(); k++) {
					MatchData dataset = (MatchData)datasets.get(k);
					expt[i][j][k] = new MatchExpt(dataset,distance,blocker);
				}
			}
		}
	}
1363978
private BigInteger computeNfactdivkNkFact(final int n,
                                              final int k)
    {
        final int nminusk = n - k;

        BigInteger kfactTimesnmkfact = factorial(k).multiply(factorial(nminusk));
        BigInteger nfactorial = factorial(n);

        return nfactorial.divide(kfactTimesnmkfact);
    }
469730
def _compute_average_correct(input_, labels, per_example_weights, topk=1):
  """"""
  return _compute_sparse_average_correct(
      input_,
      tf.reshape(tf.argmax(labels, 1), [-1, 1]), per_example_weights, topk=topk)
1213038
def evaluate(self, truth, predicted):
        
        correct = 0.0
        total = 0.0
        for i in range(len(truth)):
            if(truth[i] == predicted[i]):
                correct += 1
            total += 1
        return 1.0*correct/total
1302720
protected void bprop(float target) {
      assert (target != missing_real_value);
      if (params.loss != Loss.MeanSquare) throw new UnsupportedOperationException("Regression is only implemented for MeanSquare error.");
      final int row = 0;
      // Computing partial derivative: dE/dnet = dE/dy * dy/dnet = dE/dy * 1
      final float g = target - _a.get(row); //for MSE -dMSE/dy = target-y
      float m = momentum();
      float r = _minfo.adaDelta() ? 0 : rate(_minfo.get_processed_total()) * (1f - m);
      bprop(row, g, r, m);
    }
285064
def computeOutput(self):

    """"""

    # todo: This operation can be sped up by:
    #  1.)  Pre-allocating space for the currentOutput
    #  2.)  Making predictedState and activeState of type 'float32' up front
    #  3.)  Using logical_or(self.predictedState['t'], self.activeState['t'],
    #          self.currentOutput)
    #
    self.currentOutput = \
        logical_or(self.predictedState['t'], self.activeState['t'])

    return self.currentOutput.reshape(-1).astype('float32')
1288312
def kl_error(self, input_data, targets, average=True,
                 cache=None, prediction=True):
        

        if cache is not None:
            activations = cache
        else:
            activations = \
              self.feed_forward(input_data, prediction=prediction)

        targets_non_nan = gpuarray.empty_like(targets)
        nan_to_zeros(targets, targets_non_nan)
        kl_error = gpuarray.sum(targets_non_nan *
                                (cumath.log(targets_non_nan + eps) -
                                 cumath.log(activations + eps)))
        if average:
            kl_error /= targets.shape[0]
        return kl_error.get()
577238
private void eval(List<Tuple> training, List<Tuple> testing, int nfold) {
        classifier.train(training);

        for (Tuple tuple : testing) {
            String actual = classifier.predict(tuple).entrySet().stream()
                    .max((e1, e2) -> e1.getValue().compareTo(e2.getValue()))
                    .map(Map.Entry::getKey).orElse(StringUtils.EMPTY);
            updateScore(tuple, actual, nfold);
        }
    }
422481
@Override
    public void preProcess(@NonNull MultiDataSet toPreProcess) {
        int numFeatures = toPreProcess.numFeatureArrays();
        int numLabels = toPreProcess.numLabelsArrays();

        for (int i = 0; i < numFeatures; i++) {
            strategy.preProcess(toPreProcess.getFeatures(i), toPreProcess.getFeaturesMaskArray(i), getFeatureStats(i));
        }
        if (isFitLabel()) {
            for (int i = 0; i < numLabels; i++) {
                strategy.preProcess(toPreProcess.getLabels(i), toPreProcess.getLabelsMaskArray(i), getLabelStats(i));
            }
        }
    }
1137687
def K(self, X, X2, target):
        """"""
        self._K_computations(X, X2)
        target += self.variance*self._K_dvar
752026
def _cl_int_plot_top_losses(self, k, largest=True, figsize=(12,12), heatmap:bool=True, heatmap_thresh:int=16,
                            return_fig:bool=None)->Optional[plt.Figure]:
    ""
    tl_val,tl_idx = self.top_losses(k, largest)
    classes = self.data.classes
    cols = math.ceil(math.sqrt(k))
    rows = math.ceil(k/cols)
    fig,axes = plt.subplots(rows, cols, figsize=figsize)
    fig.suptitle('prediction/actual/loss/probability', weight='bold', size=14)
    for i,idx in enumerate(tl_idx):
        im,cl = self.data.dl(self.ds_type).dataset[idx]
        cl = int(cl)
        im.show(ax=axes.flat[i], title=
            f'{classes[self.pred_class[idx]]}/{classes[cl]} / {self.losses[idx]:.2f} / {self.probs[idx][cl]:.2f}')
        if heatmap:
            xb,_ = self.data.one_item(im, detach=False, denorm=False)
            m = self.learn.model.eval()
            with hook_output(m[0]) as hook_a:
                with hook_output(m[0], grad= True) as hook_g:
                    preds = m(xb)
                    preds[0,cl].backward()
            acts = hook_a.stored[0].cpu()
            if (acts.shape[-1]*acts.shape[-2]) >= heatmap_thresh:
                grad = hook_g.stored[0][0].cpu()
                grad_chan = grad.mean(1).mean(1)
                mult = F.relu(((acts*grad_chan[...,None,None])).sum(0))
                sz = list(im.shape[-2:])
                axes.flat[i].imshow(mult, alpha=0.6, extent=(0,*sz[::-1],0), interpolation='bilinear', cmap='magma')                
    if ifnone(return_fig, defaults.return_fig): return fig
954394
def accuracy(logits, labels):
  """"""
  is_correct = tf.nn.in_top_k(logits, labels, 1)
  correct = tf.reduce_sum(tf.cast(is_correct, tf.int32))
  incorrect = tf.reduce_sum(tf.cast(tf.logical_not(is_correct), tf.int32))
  correct_count = tf.Variable(0, False)
  incorrect_count = tf.Variable(0, False)
  correct_count_update = tf.assign_add(correct_count, correct)
  incorrect_count_update = tf.assign_add(incorrect_count, incorrect)
  accuracy_op = tf.cast(correct_count, tf.float32) / tf.cast(
      correct_count + incorrect_count, tf.float32)
  return [correct_count_update, incorrect_count_update], accuracy_op
537538
def evaluation_metrics(predicted, actual, bow=True):
    
    if bow:
        p = set(predicted)
        a = set(actual)

        true_positive = 0
        for token in p:
            if token in a:
                true_positive += 1
    else:
        # shove actual into a hash, count up the unique occurances of each token
        # iterate through predicted, check which occur in actual
        from collections import defaultdict
        act = defaultdict(lambda: 0)
        for token in actual:
            act[token] += 1

        true_positive = 0
        for token in predicted:
            if act[token] > 0:
                true_positive += 1
                act[token] -= 1

        # for shared logic below
        p = predicted
        a = actual

    try:
        precision = true_positive / len(p)
    except ZeroDivisionError:
        precision = 0.0
    try:
        recall = true_positive / len(a)
    except ZeroDivisionError:
        recall = 0.0
    try:
        f1 = 2.0 * (precision * recall) / (precision + recall)
    except ZeroDivisionError:
        f1 = 0.0

    # return (precision, recall, f1, dameraulevenshtein(predicted, actual))
    return (precision, recall, f1)
637699
def infer(self, bottomUpInput):
    

    self.iterationIdx = self.iterationIdx + 1

    if self.verbosity >= 3:
      print "\n==== Iteration: %d =====" % (self.iterationIdx)
      print "Active cols:", bottomUpInput.nonzero()[0]

    # Copy t into t-1
    # Don't need to copy learnState, which is not used in inference
    self.activeState['t-1'][:,:] = self.activeState['t'][:,:]
    self.predictedState['t-1'][:,:] = self.predictedState['t'][:,:]
    self.confidence['t-1'][:,:] = self.confidence['t'][:,:]
    self.activeState['t'].fill(0)
    self.predictedState['t'].fill(0)
    self.confidence['t'].fill(0.0)

    # Phase 1: calculate current state for each cell
    # For each column (winning in the SP):
    # - if the bottom up input was predicted by one of the sequence
    #   segments of a cell, this cell bursts,
    # - if the bottom up input was not predicted by any cell, the entire
    #   column bursts.
    activeColumns = bottomUpInput.nonzero()[0]

    for c in activeColumns:

      buPredicted = False
      predictingCells = numpy.where(self.predictedState['t-1'][c] == 1)[0]

      for i in predictingCells:
        buPredicted = True
        self.activeState['t'][c,i] = 1

      if not buPredicted:
        self.activeState['t'][c,:] = 1 # whole column bursts

    # Phase 2: calculate predictive output for each cell
    # A cell turns on its predictedState if any of its segments
    # has enough horizontal connections currently firing due to
    # bottom up input.
    self.computePhase2(doLearn=False)

    # Update the prediction score stats
    if self.collectStats:
      # import pdb; pdb.set_trace()
      self._updateStatsInferEnd(self._internalStats,
                                activeColumns,
                                self.predictedState['t-1'],
                                self.confidence['t-1'])



    # Get the output
    output = self.computeOutput()

    # Print diagnostic information based on the current verbosity level
    self.printComputeEnd(output, learn=False)

    self.resetCalled = False

    return output
1164754
def K(self, X, X2, target):
        """"""
        if X2 is None: X2 = X
       # i1 = X[:,1]
       # i2 = X2[:,1]
       # X = X[:,0].reshape(-1,1)
       # X2 = X2[:,0].reshape(-1,1)
        dist = np.abs(X - X2.T)
        
        ly=1/self.lengthscaleY
        lu=np.sqrt(3)/self.lengthscaleU
        #ly=self.lengthscaleY
        #lu=self.lengthscaleU

        k1 = np.exp(-ly*dist)*(2*lu+ly)/(lu+ly)**2
        k2 = (np.exp(-lu*dist)*(ly-2*lu+lu*ly*dist-lu**2*dist) + np.exp(-ly*dist)*(2*lu-ly) ) / (ly-lu)**2 
        k3 = np.exp(-lu*dist) * ( (1+lu*dist)/(lu+ly) + (lu)/(lu+ly)**2 )

        np.add(self.varianceU*self.varianceY*(k1+k2+k3), target, target)
1018710
def _eval_metric(input_, topk, correct_predictions, examples, phase):
  """"""
  my_parameters = {}
  if phase in (Phase.test, Phase.infer):
    dtype = tf.float32
    # Create the variables using tf.Variable because we don't want to share.
    count = tf.Variable(tf.constant(0, dtype=dtype),
                        name='count_%d' % topk,
                        collections=[bookkeeper.GraphKeys.TEST_VARIABLES],
                        trainable=False)
    correct = tf.Variable(tf.constant(0, dtype=dtype),
                          name='correct_%d' % topk,
                          collections=[bookkeeper.GraphKeys.TEST_VARIABLES],
                          trainable=False)
    my_parameters['count'] = count
    my_parameters['correct'] = correct
    with input_.g.device(count.device):
      examples = tf.assign_add(count, examples)
    with input_.g.device(correct.device):
      correct_predictions = tf.assign_add(correct, correct_predictions)
  return correct_predictions, examples, my_parameters