def avg_eval_metrics(metrics):
  """Average evaluation metrics (divide values by sample count).

  Args:
    metrics: evaluation metrics

  Returns:
    averaged metrics as a dict
  """
  n = metrics['sample_count']
  metrics['loss'] = metrics['loss_sum'] / n
  metrics['error_rate'] = metrics['error_count'] / n
  if 'top5_error_count' in metrics:
    metrics['top5_error_rate'] = metrics['top5_error_count'] / n
  return metrics