model/train.py
def train_and_evaluate(model, optimizer, scheduler, loss_fn, 
    metrics, params, model_dir, restore_file=None):
    """Train the model and evaluate every epoch.

    Args:
        model: (torch.nn.Module) the neural network
        train_dataloader: (DataLoader) a torch.utils.data.DataLoader object that fetches training data
        val_dataloader: (DataLoader) a torch.utils.data.DataLoader object that fetches validation data
        optimizer: (torch.optim) optimizer for parameters of model
        loss_fn: a function that takes batch_output and batch_labels and computes the loss for the batch
        metrics: (dict) a dictionary of functions that compute a metric using the output and labels of each batch
        params: (Params) hyperparameters
        model_dir: (string) directory containing config, weights and log
        restore_file: (string) optional- name of file to restore from (without its extension .pth.tar)
    """

    # reload weights from restore_file if specified
    if restore_file is not None:
        restore_path = os.path.join(args.model_dir, args.restore_file + '.pth.tar')
        logging.info("Restoring parameters from {}".format(restore_path))
        utils.load_checkpoint(restore_path, model, optimizer)

    best_val_loss = 1e10
    best_test_loss = 1e10

    for epoch in range(params.num_epochs):
        logging.info("Generate the train and test datasets...")
        # fetch dataloaders for every epoch
        dataloaders = data_loader.fetch_dataloader(['train', 'val', 'test'], args.data_dir, params)
        logging.info("- done.")

        # Run one epoch
        logging.info("Epoch {}/{}".format(epoch + 1, params.num_epochs))

        # compute number of batches in one epoch (one full pass over the training set)
        train(model, optimizer, loss_fn, dataloaders['train'], metrics, params)

        # Evaluate for one epoch on validation set
        val_metrics = evaluate(model, loss_fn, dataloaders['val'], metrics, params, 'Val')
        val_loss = val_metrics['rmse']
        is_best_val = val_loss<=best_val_loss

        # Evaluate for one epoch on test set
        test_metrics = evaluate(model, loss_fn, dataloaders['test'], metrics, params, 'Test')
        test_loss = test_metrics['rmse']
        is_best_test = test_loss<=best_test_loss


        # Note that step should be called after validation
        # scheduler.step(val_loss)
        # for param_group in optimizer.param_groups:
            # print(param_group['lr'])

        # Save weights
        utils.save_checkpoint({'epoch': epoch + 1,
                               'state_dict': model.state_dict(),
                               'optim_dict' : optimizer.state_dict()},
                               is_best=is_best_val,
                               checkpoint=model_dir)

        # If best_eval, best_save_path
        # if is_best and (epoch > params.num_epochs/3):
        if is_best_val:
            logging.info("- Found new best val result")
            best_val_loss = val_loss
            # Save metrics in a json file in the model directory
            best_json_path = os.path.join(model_dir, "metrics_val_best_weights.json")
            utils.save_dict_to_json(val_metrics, best_json_path)

        if is_best_test:
            logging.info("- Found new best test result")
            best_test_loss = test_loss
            # Save metrics in a json file in the model directory
            best_json_path = os.path.join(model_dir, "metrics_test_best_weights.json")
            utils.save_dict_to_json(test_metrics, best_json_path)

            best_prediction = get_predictions(model, dataloaders['test'], params)
            with open(os.path.join(args.model_dir, "best_test.pk"), 'wb') as f:
                pickle.dump(best_prediction, f)

            # p = predict(model, params)
            

        # Save latest val metrics in a json file in the model directory
        # last_json_path = os.path.join(model_dir, "metrics_test_last_weights.json")
        # utils.save_dict_to_json(test_metrics, last_json_path)
        # last_json_path = os.path.join(model_dir, "metrics_val_last_weights.json")
        # utils.save_dict_to_json(val_metrics, last_json_path)
Train the model and evaluate every epoch.

    Args:
        model: (torch.nn.Module) the neural network
        train_dataloader: (DataLoader) a torch.utils.data.DataLoader object that fetches training data
        val_dataloader: (DataLoader) a torch.utils.data.DataLoader object that fetches validation data
        optimizer: (torch.optim) optimizer for parameters of model
        loss_fn: a function that takes batch_output and batch_labels and computes the loss for the batch
        metrics: (dict) a dictionary of functions that compute a metric using the output and labels of each batch
        params: (Params) hyperparameters
        model_dir: (string) directory containing config, weights and log
        restore_file: (string) optional- name of file to restore from (without its extension .pth.tar)
    
Train the model and evaluate every epoch.
Train the model and evaluate every epoch .
def train_and_evaluate(model, optimizer, scheduler, loss_fn, 
    metrics, params, model_dir, restore_file=None):
    if restore_file is not None:
        restore_path = os.path.join(args.model_dir, args.restore_file + '.pth.tar')
        logging.info("Restoring parameters from {}".format(restore_path))
        utils.load_checkpoint(restore_path, model, optimizer)
    best_val_loss = 1e10
    best_test_loss = 1e10
    for epoch in range(params.num_epochs):
        logging.info("Generate the train and test datasets...")
        dataloaders = data_loader.fetch_dataloader(['train', 'val', 'test'], args.data_dir, params)
        logging.info("- done.")
        logging.info("Epoch {}/{}".format(epoch + 1, params.num_epochs))
        train(model, optimizer, loss_fn, dataloaders['train'], metrics, params)
        val_metrics = evaluate(model, loss_fn, dataloaders['val'], metrics, params, 'Val')
        val_loss = val_metrics['rmse']
        is_best_val = val_loss<=best_val_loss
        test_metrics = evaluate(model, loss_fn, dataloaders['test'], metrics, params, 'Test')
        test_loss = test_metrics['rmse']
        is_best_test = test_loss<=best_test_loss
        utils.save_checkpoint({'epoch': epoch + 1,
                               'state_dict': model.state_dict(),
                               'optim_dict' : optimizer.state_dict()},
                               is_best=is_best_val,
                               checkpoint=model_dir)
        if is_best_val:
            logging.info("- Found new best val result")
            best_val_loss = val_loss
            best_json_path = os.path.join(model_dir, "metrics_val_best_weights.json")
            utils.save_dict_to_json(val_metrics, best_json_path)
        if is_best_test:
            logging.info("- Found new best test result")
            best_test_loss = test_loss
            best_json_path = os.path.join(model_dir, "metrics_test_best_weights.json")
            utils.save_dict_to_json(test_metrics, best_json_path)
            best_prediction = get_predictions(model, dataloaders['test'], params)
            with open(os.path.join(args.model_dir, "best_test.pk"), 'wb') as f:
                pickle.dump(best_prediction, f)
def train_and_evaluate ( model , optimizer , scheduler , loss_fn , metrics , params , model_dir , restore_file = None ) : if restore_file is not None : restore_path = os . path . join ( args . model_dir , args . restore_file + '.pth.tar' ) logging . info ( "Restoring parameters from {}" . format ( restore_path ) ) utils . load_checkpoint ( restore_path , model , optimizer ) best_val_loss = 1e10 best_test_loss = 1e10 for epoch in range ( params . num_epochs ) : logging . info ( "Generate the train and test datasets..." ) dataloaders = data_loader . fetch_dataloader ( [ 'train' , 'val' , 'test' ] , args . data_dir , params ) logging . info ( "- done." ) logging . info ( "Epoch {}/{}" . format ( epoch + 1 , params . num_epochs ) ) train ( model , optimizer , loss_fn , dataloaders [ 'train' ] , metrics , params ) val_metrics = evaluate ( model , loss_fn , dataloaders [ 'val' ] , metrics , params , 'Val' ) val_loss = val_metrics [ 'rmse' ] is_best_val = val_loss <= best_val_loss test_metrics = evaluate ( model , loss_fn , dataloaders [ 'test' ] , metrics , params , 'Test' ) test_loss = test_metrics [ 'rmse' ] is_best_test = test_loss <= best_test_loss utils . save_checkpoint ( { 'epoch' : epoch + 1 , 'state_dict' : model . state_dict ( ) , 'optim_dict' : optimizer . state_dict ( ) } , is_best = is_best_val , checkpoint = model_dir ) if is_best_val : logging . info ( "- Found new best val result" ) best_val_loss = val_loss best_json_path = os . path . join ( model_dir , "metrics_val_best_weights.json" ) utils . save_dict_to_json ( val_metrics , best_json_path ) if is_best_test : logging . info ( "- Found new best test result" ) best_test_loss = test_loss best_json_path = os . path . join ( model_dir , "metrics_test_best_weights.json" ) utils . save_dict_to_json ( test_metrics , best_json_path ) best_prediction = get_predictions ( model , dataloaders [ 'test' ] , params ) with open ( os . path . join ( args . model_dir , "best_test.pk" ) , 'wb' ) as f : pickle . dump ( best_prediction , f )
Train the model and evaluate every epoch.
Train the model and evaluate every epoch .
"""Train the model and evaluate every epoch.

    Args:
        model: (torch.nn.Module) the neural network
        train_dataloader: (DataLoader) a torch.utils.data.DataLoader object that fetches training data
        val_dataloader: (DataLoader) a torch.utils.data.DataLoader object that fetches validation data
        optimizer: (torch.optim) optimizer for parameters of model
        loss_fn: a function that takes batch_output and batch_labels and computes the loss for the batch
        metrics: (dict) a dictionary of functions that compute a metric using the output and labels of each batch
        params: (Params) hyperparameters
        model_dir: (string) directory containing config, weights and log
        restore_file: (string) optional- name of file to restore from (without its extension .pth.tar)
    """ # reload weights from restore_file if specified # fetch dataloaders for every epoch # Run one epoch # compute number of batches in one epoch (one full pass over the training set) # Evaluate for one epoch on validation set # Evaluate for one epoch on test set # Note that step should be called after validation # scheduler.step(val_loss) # for param_group in optimizer.param_groups: # print(param_group['lr']) # Save weights # If best_eval, best_save_path # if is_best and (epoch > params.num_epochs/3): # Save metrics in a json file in the model directory # Save metrics in a json file in the model directory # p = predict(model, params) # Save latest val metrics in a json file in the model directory # last_json_path = os.path.join(model_dir, "metrics_test_last_weights.json") # utils.save_dict_to_json(test_metrics, last_json_path) # last_json_path = os.path.join(model_dir, "metrics_val_last_weights.json") # utils.save_dict_to_json(val_metrics, last_json_path)
model
(torch.nn.Module) the neural network
( torch . nn . Module ) the neural network
optimizer
(torch.optim) optimizer for parameters of model
( torch . optim ) optimizer for parameters of model
scheduler
loss_fn
a function that takes batch_output and batch_labels and computes the loss for the batch
a function that takes batch_output and batch_labels and computes the loss for the batch
metrics
(dict) a dictionary of functions that compute a metric using the output and labels of each batch
( dict ) a dictionary of functions that compute a metric using the output and labels of each batch
params
None
model_dir
(string) directory containing config, weights and log
( string ) directory containing config weights and log
restore_file
(string) optional- name of file to restore from (without its extension .pth.tar)
( string ) optional - name of file to restore from ( without its extension . pth . tar )
train_dataloader
(DataLoader) a torch.utils.data.DataLoader object that fetches training data
( DataLoader ) a torch . utils . data . DataLoader object that fetches training data
val_dataloader
(DataLoader) a torch.utils.data.DataLoader object that fetches validation data
( DataLoader ) a torch . utils . data . DataLoader object that fetches validation data