import warnings
import copy


def clone_trainer(trainer, is_reinit_besides_param=False):
    """Clone a trainer with optional possibility of reinitializing everything besides 
    parameters (e.g. optimizers.)"""
    with warnings.catch_warnings():
        warnings.simplefilter("ignore")
        trainer_new = copy.deepcopy(trainer)

    if is_reinit_besides_param:
        trainer_new.initialize_callbacks()
        trainer_new.initialize_criterion()
        trainer_new.initialize_optimizer()
        trainer_new.initialize_history()

    return trainer_new