jiant/trainer.py
def build_trainer(
    args,
    task_names,
    model,
    run_dir,
    metric_should_decrease=True,
    train_type="SamplingMultiTaskTrainer",
    phase="pretrain",
):
    """Build a trainer from params.

    Parameters
    ----------
    params: Trainer parameters as built by build_trainer_params.
    model: A module with trainable parameters.
    run_dir: The directory where we save the models.

    Returns
    -------
    A trainer object, a trainer config object, an optimizer config object,
        and a scheduler config object.
    """
    params = build_trainer_params(args, task_names, phase)

    opt_params = {"type": params["optimizer"], "lr": params["lr"]}
    if params["optimizer"] == "adam":
        # AMSGrad is a flag variant of Adam, not its own object.
        opt_params["amsgrad"] = True
    elif params["optimizer"] == "bert_adam":
        # Transformer scheduler uses number of opt steps, if known in advance, to set the LR.
        # We leave it as -1 here (unknown) and set it if known later.
        opt_params["t_total"] = -1
        opt_params["warmup"] = 0.1
    opt_params = Params(opt_params)

    schd_params = Params(
        {
            "type": "reduce_on_plateau",
            "mode": "min" if metric_should_decrease else "max",
            "factor": params["lr_decay_factor"],
            "patience": params["lr_patience"],
            "threshold": params["scheduler_threshold"],
            "threshold_mode": "abs",
            "verbose": True,
        }
    )

    train_params = Params(
        {
            "cuda_device": params["cuda"],
            "patience": params["patience"],
            "grad_norm": params["max_grad_norm"],
            "val_interval": params["val_interval"],
            "max_vals": params["max_vals"],
            "lr_decay": 0.99,
            "min_lr": params["min_lr"],
            "keep_all_checkpoints": params["keep_all_checkpoints"],
            "val_data_limit": params["val_data_limit"],
            "max_epochs": params["max_epochs"],
            "dec_val_scale": params["dec_val_scale"],
            "training_data_fraction": params["training_data_fraction"],
            "run_name": args.run_name
        }
    )
    assert (
        train_type == "SamplingMultiTaskTrainer"
    ), "We currently only support SamplingMultiTaskTrainer"
    if train_type == "SamplingMultiTaskTrainer":
        trainer = SamplingMultiTaskTrainer.from_params(model, run_dir, copy.deepcopy(train_params))
    return trainer, train_params, opt_params, schd_params
Build a trainer from params.

    Parameters
    ----------
    params: Trainer parameters as built by build_trainer_params.
    model: A module with trainable parameters.
    run_dir: The directory where we save the models.

    Returns
    -------
    A trainer object, a trainer config object, an optimizer config object,
        and a scheduler config object.
    
Build a trainer from params.
Parameters

Trainer parameters as built by build_trainer_params.
model: A module with trainable parameters.
run_dir: The directory where we save the models.

Returns

A trainer object, a trainer config object, an optimizer config object,
and a scheduler config object.
Build a trainer from params . Parameters Trainer parameters as built by build_trainer_params . model : A module with trainable parameters . run_dir : The directory where we save the models . Returns A trainer object a trainer config object an optimizer config object and a scheduler config object .
def build_trainer(
    args,
    task_names,
    model,
    run_dir,
    metric_should_decrease=True,
    train_type="SamplingMultiTaskTrainer",
    phase="pretrain",
):
    params = build_trainer_params(args, task_names, phase)
    opt_params = {"type": params["optimizer"], "lr": params["lr"]}
    if params["optimizer"] == "adam":
        opt_params["amsgrad"] = True
    elif params["optimizer"] == "bert_adam":
        opt_params["t_total"] = -1
        opt_params["warmup"] = 0.1
    opt_params = Params(opt_params)
    schd_params = Params(
        {
            "type": "reduce_on_plateau",
            "mode": "min" if metric_should_decrease else "max",
            "factor": params["lr_decay_factor"],
            "patience": params["lr_patience"],
            "threshold": params["scheduler_threshold"],
            "threshold_mode": "abs",
            "verbose": True,
        }
    )
    train_params = Params(
        {
            "cuda_device": params["cuda"],
            "patience": params["patience"],
            "grad_norm": params["max_grad_norm"],
            "val_interval": params["val_interval"],
            "max_vals": params["max_vals"],
            "lr_decay": 0.99,
            "min_lr": params["min_lr"],
            "keep_all_checkpoints": params["keep_all_checkpoints"],
            "val_data_limit": params["val_data_limit"],
            "max_epochs": params["max_epochs"],
            "dec_val_scale": params["dec_val_scale"],
            "training_data_fraction": params["training_data_fraction"],
            "run_name": args.run_name
        }
    )
    assert (
        train_type == "SamplingMultiTaskTrainer"
    ), "We currently only support SamplingMultiTaskTrainer"
    if train_type == "SamplingMultiTaskTrainer":
        trainer = SamplingMultiTaskTrainer.from_params(model, run_dir, copy.deepcopy(train_params))
    return trainer, train_params, opt_params, schd_params
def build_trainer ( args , task_names , model , run_dir , metric_should_decrease = True , train_type = "SamplingMultiTaskTrainer" , phase = "pretrain" , ) : params = build_trainer_params ( args , task_names , phase ) opt_params = { "type" : params [ "optimizer" ] , "lr" : params [ "lr" ] } if params [ "optimizer" ] == "adam" : opt_params [ "amsgrad" ] = True elif params [ "optimizer" ] == "bert_adam" : opt_params [ "t_total" ] = - 1 opt_params [ "warmup" ] = 0.1 opt_params = Params ( opt_params ) schd_params = Params ( { "type" : "reduce_on_plateau" , "mode" : "min" if metric_should_decrease else "max" , "factor" : params [ "lr_decay_factor" ] , "patience" : params [ "lr_patience" ] , "threshold" : params [ "scheduler_threshold" ] , "threshold_mode" : "abs" , "verbose" : True , } ) train_params = Params ( { "cuda_device" : params [ "cuda" ] , "patience" : params [ "patience" ] , "grad_norm" : params [ "max_grad_norm" ] , "val_interval" : params [ "val_interval" ] , "max_vals" : params [ "max_vals" ] , "lr_decay" : 0.99 , "min_lr" : params [ "min_lr" ] , "keep_all_checkpoints" : params [ "keep_all_checkpoints" ] , "val_data_limit" : params [ "val_data_limit" ] , "max_epochs" : params [ "max_epochs" ] , "dec_val_scale" : params [ "dec_val_scale" ] , "training_data_fraction" : params [ "training_data_fraction" ] , "run_name" : args . run_name } ) assert ( train_type == "SamplingMultiTaskTrainer" ) , "We currently only support SamplingMultiTaskTrainer" if train_type == "SamplingMultiTaskTrainer" : trainer = SamplingMultiTaskTrainer . from_params ( model , run_dir , copy . deepcopy ( train_params ) ) return trainer , train_params , opt_params , schd_params
Build a trainer from params.
Build a trainer from params .
"""Build a trainer from params.

    Parameters
    ----------
    params: Trainer parameters as built by build_trainer_params.
    model: A module with trainable parameters.
    run_dir: The directory where we save the models.

    Returns
    -------
    A trainer object, a trainer config object, an optimizer config object,
        and a scheduler config object.
    """ # AMSGrad is a flag variant of Adam, not its own object. # Transformer scheduler uses number of opt steps, if known in advance, to set the LR. # We leave it as -1 here (unknown) and set it if known later.
args
task_names
model
run_dir
metric_should_decrease
train_type
phase