def compute_metrics(tp, fp, fn):
  """Computes precision, recall, and fscore."""
  if tp < 0:
    raise ValueError('True positive should be non-negative.')
  if fp < 0:
    raise ValueError('False positive should be non-negative.')
  if fn < 0:
    raise ValueError('False negative should be non-negative.')
  if tp + fp > 0:
    precision = tp / (tp + fp)
  else:
    precision = 0.
  if tp + fn > 0.:
    recall = tp / (tp + fn)
  else:
    recall = 0.

  if precision + recall > 0.:
    fscore = 2. * precision * recall /  (precision + recall)
  else:
    fscore = 0.
  return dict(precision=precision, recall=recall, fscore=fscore)