import torch


def save_hidden(hidden):
    """Wraps hidden states in new Tensors, to declare it not to need gradients. So that the initial hidden state for this batch is constant and doesn’t depend on anything."""

    if isinstance(hidden, torch.Tensor):
        return hidden.detach()
    else:
        return tuple(save_hidden(v) for v in hidden)