def gradients_for_var_group(var_groups, gradients, name):
  """Returns a slice of `gradients` belonging to the var group `name`."""
  start = 0
  for group_name in sorted(var_groups.keys()):
    n = len(var_groups[group_name])
    if group_name == name:
      return gradients[start:start+n]
    start += n
  return []