def mapflat(flatmap):
  """ Transform a map variable -> gradient position into the associated ordered list of variables.
  Args:
    flatmap Mapping between variables and their gradient's position in the flattened tensor
  Returns:
    List of variables in the order defined by their respective position in the flat gradient
  """
  res = [None] * len(flatmap)
  for variable, position in flatmap.items():
    res[position] = variable
  return res