model/train.py
def train(model, optimizer, loss_fn, dataloader, metrics, params):
    """Train the model on `num_steps` batches

    Args:
        model: (torch.nn.Module) the neural network
        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
        dataloader: (DataLoader) a torch.utils.data.DataLoader object that fetches training data
        metrics: (dict) a dictionary of functions that compute a metric using the output and labels of each batch
        params: (Params) hyperparameters
        num_steps: (int) number of batches to train on, each of size params.batch_size
    """

    # set model to training mode
    model.train()

    # summary for current training loop and a running average object for loss
    summ = []
    loss_avg = utils.RunningAverage()

    # Use tqdm for progress bar
    with tqdm(total=len(dataloader), ascii=True) as t:
        for i, (train_batch, labels_batch) in enumerate(dataloader):
            
            # move to GPU if available
            if params.cuda:
                train_batch, labels_batch = train_batch.cuda(async=True), labels_batch.float().cuda(async=True)
            # convert to torch Variables
            train_batch, labels_batch = Variable(train_batch), Variable(labels_batch)

            # compute model output and loss
            output_batch = model(train_batch)
            loss = loss_fn(output_batch, labels_batch)

            # clear previous gradients, compute gradients of all variables wrt loss
            optimizer.zero_grad()
            loss.backward()

            # performs updates using calculated gradients
            optimizer.step()

            # Evaluate summaries only once in a while
            if i % params.save_summary_steps == 0:
                # extract data from torch Variable, move to cpu, convert to numpy arrays
                output_batch = output_batch.data.cpu().numpy()
                labels_batch = labels_batch.data.cpu().numpy()

                # compute all metrics on this batch
                summary_batch = {metric:metrics[metric](output_batch, labels_batch)
                                 for metric in metrics}
                summary_batch['loss'] = loss.item() # loss.data[0]
                summ.append(summary_batch)

            # update the average loss
            loss_avg.update(loss.item())

            t.set_postfix(loss='{:05.3f}'.format(loss_avg()))
            t.update()

    # compute mean of all metrics in summary
    metrics_mean = {metric:np.mean([x[metric] for x in summ]) for metric in summ[0]}
    metrics_string = " ; ".join("{}: {:05.3f}".format(k, v) for k, v in metrics_mean.items())
    logging.info("- Train metrics: " + metrics_string)
Train the model on `num_steps` batches

    Args:
        model: (torch.nn.Module) the neural network
        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
        dataloader: (DataLoader) a torch.utils.data.DataLoader object that fetches training data
        metrics: (dict) a dictionary of functions that compute a metric using the output and labels of each batch
        params: (Params) hyperparameters
        num_steps: (int) number of batches to train on, each of size params.batch_size
    
Train the model on `num_steps` batches
Train the model on ` num_steps ` batches
def train(model, optimizer, loss_fn, dataloader, metrics, params):
    model.train()
    summ = []
    loss_avg = utils.RunningAverage()
    with tqdm(total=len(dataloader), ascii=True) as t:
        for i, (train_batch, labels_batch) in enumerate(dataloader):
            if params.cuda:
                train_batch, labels_batch = train_batch.cuda(async=True), labels_batch.float().cuda(async=True)
            train_batch, labels_batch = Variable(train_batch), Variable(labels_batch)
            output_batch = model(train_batch)
            loss = loss_fn(output_batch, labels_batch)
            optimizer.zero_grad()
            loss.backward()
            optimizer.step()
            if i % params.save_summary_steps == 0:
                output_batch = output_batch.data.cpu().numpy()
                labels_batch = labels_batch.data.cpu().numpy()
                summary_batch = {metric:metrics[metric](output_batch, labels_batch)
                                 for metric in metrics}
                summary_batch['loss'] = loss.item() 
                summ.append(summary_batch)
            loss_avg.update(loss.item())
            t.set_postfix(loss='{:05.3f}'.format(loss_avg()))
            t.update()
    metrics_mean = {metric:np.mean([x[metric] for x in summ]) for metric in summ[0]}
    metrics_string = " ; ".join("{}: {:05.3f}".format(k, v) for k, v in metrics_mean.items())
    logging.info("- Train metrics: " + metrics_string)
def train ( model , optimizer , loss_fn , dataloader , metrics , params ) : model . train ( ) summ = [ ] loss_avg = utils . RunningAverage ( ) with tqdm ( total = len ( dataloader ) , ascii = True ) as t : for i , ( train_batch , labels_batch ) in enumerate ( dataloader ) : if params . cuda : train_batch , labels_batch = train_batch . cuda ( async = True ) , labels_batch . float ( ) . cuda ( async = True ) train_batch , labels_batch = Variable ( train_batch ) , Variable ( labels_batch ) output_batch = model ( train_batch ) loss = loss_fn ( output_batch , labels_batch ) optimizer . zero_grad ( ) loss . backward ( ) optimizer . step ( ) if i % params . save_summary_steps == 0 : output_batch = output_batch . data . cpu ( ) . numpy ( ) labels_batch = labels_batch . data . cpu ( ) . numpy ( ) summary_batch = { metric : metrics [ metric ] ( output_batch , labels_batch ) for metric in metrics } summary_batch [ 'loss' ] = loss . item ( ) summ . append ( summary_batch ) loss_avg . update ( loss . item ( ) ) t . set_postfix ( loss = '{:05.3f}' . format ( loss_avg ( ) ) ) t . update ( ) metrics_mean = { metric : np . mean ( [ x [ metric ] for x in summ ] ) for metric in summ [ 0 ] } metrics_string = " ; " . join ( "{}: {:05.3f}" . format ( k , v ) for k , v in metrics_mean . items ( ) ) logging . info ( "- Train metrics: " + metrics_string )
Train the model on `num_steps` batches
Train the model on ` num_steps ` batches
"""Train the model on `num_steps` batches

    Args:
        model: (torch.nn.Module) the neural network
        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
        dataloader: (DataLoader) a torch.utils.data.DataLoader object that fetches training data
        metrics: (dict) a dictionary of functions that compute a metric using the output and labels of each batch
        params: (Params) hyperparameters
        num_steps: (int) number of batches to train on, each of size params.batch_size
    """ # set model to training mode # summary for current training loop and a running average object for loss # Use tqdm for progress bar # move to GPU if available # convert to torch Variables # compute model output and loss # clear previous gradients, compute gradients of all variables wrt loss # performs updates using calculated gradients # Evaluate summaries only once in a while # extract data from torch Variable, move to cpu, convert to numpy arrays # compute all metrics on this batch # loss.data[0] # update the average loss # compute mean of all metrics in summary
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
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
dataloader
(DataLoader) a torch.utils.data.DataLoader object that fetches training data
( DataLoader ) a torch . utils . data . DataLoader object that fetches training data
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
num_steps
(int) number of batches to train on, each of size params.batch_size
( int ) number of batches to train on each of size params . batch_size