class_docstr: this is modified from `howto100m/metrics.py`.
  History of changes:
  refactor as a class.
  add metric_key in __init__
class_docstr_tok: ["this","is","modified","from","`","howto100m\/metrics.py","`",".","History","of","changes",":","refactor","as","a","class",".","add","metric_key","in","__init__"]
code: def compute_metrics(self, outputs, texts, **kwargs):
      x = outputs
      sx = np.sort(-x, axis=1)
      d = np.diag(-x)
      d = d[:, np.newaxis]
      ind = sx - d
      ind = np.where(ind == 0)
      ind = ind[1]
      metrics = {}
      metrics["R1"] = float(np.sum(ind == 0)) / len(ind)
      metrics["R5"] = float(np.sum(ind < 5)) / len(ind)
      metrics["R10"] = float(np.sum(ind < 10)) / len(ind)
      metrics["MR"] = np.median(ind) + 1
  
      max_idx = np.argmax(outputs, axis=1)
      if self.error:
          # print top-20 errors.
          error = []
          for ex_idx in range(20):
              error.append((texts[ex_idx], texts[max_idx[ex_idx]]))
          metrics["error"] = error
      return metrics
  
imports: import numpy
  import json
label_desc: Use image node_id 2 for calling the RetrievalMetric obj's underlying member method code with example usage: obj.compute_metrics(outputs, texts) and returns: metrics