import torch


def recurrent_state_as_numpy(recurrent_state):
    """Convert a recurrent state in torch.Tensor to numpy.ndarray.

    Args:
        recurrent_state (object): Recurrent state in torch.Tensor.

    Returns:
        object: Recurrent state in numpy.ndarray.
    """
    if recurrent_state is None:
        return None
    elif isinstance(recurrent_state, torch.Tensor):
        return recurrent_state.detach().cpu().numpy()
    elif isinstance(recurrent_state, tuple):
        return tuple(recurrent_state_as_numpy(s) for s in recurrent_state)
    else:
        raise ValueError("Invalid recurrent state: {}".format(recurrent_state))