import torch


def tensor_to_num(tensors):
  """Given a (nested) list/tuple of singlton tensors, get their corresponding numerical values by call tensor.item()
    For example: if input tensors = [(torch.tensor(1), torch.tensor(2))]
      then returns [(1, 2)];
      note the nested list and tuple structure are kept
    This is another example of recursive function
  """
  if isinstance(tensors, torch.Tensor):
    return tensors.item()
  elif isinstance(tensors, tuple):
    return tuple([tensor_to_num(tensor) for tensor in tensors])
  elif isinstance(tensors, list):
    return [tensor_to_num(tensor) for tensor in tensors]