def train(model, optimizer, data_iterator, metrics, save_summary_steps, num_steps, label_encoder): # set model to training mode model.train() # summary for current training loop and a running average object for loss summ = [] loss_avg = utils.RunningAverage() preds_all = [] true_all = [] loss_all = [] # Use tqdm for progress bar t = trange(num_steps) for i in t: model.train() # zero the gradients model.zero_grad() # fetch the next training batch train_batch, labels_batch = next(data_iterator) # compute model output and log_lik loss, logits = model( input=train_batch, labels=labels_batch, label_pad_idx=label_encoder[ClassEncoder.FEATURE_NAME].pad_idx) # clear previous gradients, compute gradients of all variables wrt loss loss.backward() # performs updates using calculated gradients optimizer.step() max_norm(model) # Evaluate summaries only once in a while if i % save_summary_steps == 0: model.eval() # 1. get the predictions from model preds = model.predict( logits=logits, mask=(train_batch[WordEncoder.FEATURE_NAME] != data_encoder[WordEncoder.FEATURE_NAME].pad_idx).float()) labels_batch = labels_batch[ ClassEncoder.FEATURE_NAME].data.cpu().numpy().squeeze() # 2. decode the predictions and the ground_truths labels = label_encoder[ClassEncoder.FEATURE_NAME].decode( labels_batch) preds = label_encoder[ClassEncoder.FEATURE_NAME].decode(preds) # 3. gather stats preds_all.extend(preds) true_all.extend(labels) loss_all.append(loss.item()) # update the average loss loss_avg.update(loss.item()) t.set_postfix(loss='{:05.3f}'.format(loss_avg())) # compute mean of all metrics in summary scores = { metric: metrics[metric](preds_all, true_all) for metric in metrics } scores['loss'] = np.mean(loss_all) metrics_string = " ; ".join("{}: {:05.3f}".format(k, v) for k, v in scores.items()) logging.info("- Train metrics: " + metrics_string) return scores
def train(model, optimizer, data_iterator, metrics, params, num_steps, data_encoder, label_encoder): # summary for current training loop and a running average object for loss loss_avg = utils.RunningAverage() preds_all = [] true_all = [] loss_all = [] # Use tqdm for progress bar t = trange(num_steps) for i in t: model.train() # zero the gradients model.zero_grad() # fetch the next training batch train_batch, labels_batch = next(data_iterator) # compute model output and log_lik loss = model(train_X=train_batch, train_Y=labels_batch, mode='train') # clear previous gradients, compute gradients of all variables wrt loss loss.backward() # gradient clipping torch.nn.utils.clip_grad_norm_(model.parameters(), 5.0) # performs updates using calculated gradients optimizer.step() # Evaluate summaries only once in a while if i % params.save_summary_steps == 0: model.eval() # 1. get the predictions from model preds = model.predict(train_X=train_batch, mode='train') labels_batch = labels_batch[EntityEncoder.FEATURE_NAME].data.cpu().numpy() labels_batch_trunc = [labels_batch[o_idx][:len(o)] for o_idx, o in enumerate(preds)] # 2. decode the predictions and the ground_truths labels_batch_trunc = [label_encoder[EntityEncoder.FEATURE_NAME].decode(l) for l in labels_batch_trunc] preds = [label_encoder[EntityEncoder.FEATURE_NAME].decode(l) for l in preds] # 3. gather stats preds_all.extend(preds) true_all.extend(labels_batch_trunc) loss_all.append(loss.item()) # update the average loss loss_avg.update(loss.item()) t.set_postfix(loss='{:05.3f}'.format(loss_avg())) del loss # compute mean of all metrics in summary scores = {metric: metrics[metric](preds_all, true_all) for metric in metrics} scores['loss'] = np.mean(loss_all) metrics_string = " ; ".join("{}: {}".format(k, v) for k, v in scores.items()) logging.info("- Train metrics: " + metrics_string) return scores