(self, num_epochs, early_stopping=None, save=True)
return best_iteration, all_metrics
Run the training loop for given number of epochs. The model
        is evaluated at the end of every epoch and saved as well
        Parameters:
            num_epochs: Total number of epochs to run
            early_stopping: A string indicating how to perform early stopping
                Should be of the form lowest/highest_n_metric where:
                    lowest/highest: Track lowest or highest values
                    n: The window size within which to track best
                    metric: Name of the metric to track. Should be available
                        in the dict returned by evaluator
            save: Save model every epoch if true
        Returns:
            Tuple of best checkpoint number and metrics array (for plotting etc)
Run the training loop for given number of epochs. The model
        is evaluated at the end of every epoch and saved as well
        Parameters:
            num_epochs: Total number of epochs to run
            early_stopping: A string indicating how to perform early stopping
                Should be of the form lowest/highest_n_metric where:
                    lowest/highest: Track lowest or highest values
                    n: The window size within which to track best
                    metric: Name of the metric to track. Should be available
                        in the dict returned by evaluator
            save: Save model every epoch if true
        Returns:
            Tuple of best checkpoint number and metrics array (for plotting etc)
Run the training loop for given number of epochs . The model is evaluated at the end of every epoch and saved as well Parameters : num_epochs : Total number of epochs to run early_stopping : A string indicating how to perform early stopping Should be of the form lowest / highest_n_metric where : lowest / highest : Track lowest or highest values n : The window size within which to track best metric : Name of the metric to track . Should be available in the dict returned by evaluator save : Save model every epoch if true Returns : Tuple of best checkpoint number and metrics array ( for plotting etc )
def train(self, num_epochs, early_stopping=None, save=True):
        """
        Run the training loop for given number of epochs. The model
        is evaluated at the end of every epoch and saved as well
        Parameters:
            num_epochs: Total number of epochs to run
            early_stopping: A string indicating how to perform early stopping
                Should be of the form lowest/highest_n_metric where:
                    lowest/highest: Track lowest or highest values
                    n: The window size within which to track best
                    metric: Name of the metric to track. Should be available
                        in the dict returned by evaluator
            save: Save model every epoch if true
        Returns:
            Tuple of best checkpoint number and metrics array (for plotting etc)
        """

        all_metrics = defaultdict(list)
        best_iteration = 0

        if early_stopping:
            if not save:
                raise ValueError('save should be True for early stopping')
            if self.evaluator is None:
                raise ValueError('early stopping requires an eval function')

            best_fn, best_window, best_metric_name = self._get_early_stopping_criteria(early_stopping)
            tracking = deque([], best_window + 1)


        for epoch in range(num_epochs):
            self.train_iter.init_epoch()
            epoch_loss = 0
            count = 0

            logger.info('Epoch %d (%d)'%(epoch + 1, int(self.model.iterations)))

            prog_iter = tqdm(self.train_iter, leave=False)
            for batch in prog_iter:
                # Train mode
                self.model.train()

                self.optimizer.zero_grad()
                loss, _ = self.model.loss(batch)
                loss.backward()
                self.optimizer.step()

                if self.lr_scheduler_step:
                    self.lr_scheduler_step.step()

                epoch_loss += loss.item()
                count += 1
                self.model.iterations += 1

                # Display loss
                prog_iter.set_description('Training')
                prog_iter.set_postfix(loss=(epoch_loss/count))

            
            if self.lr_scheduler_epoch:
                self.lr_scheduler_epoch.step()

            train_loss = epoch_loss/count
            all_metrics['train_loss'].append(train_loss)
            logger.info('Train Loss: {:3.5f}'.format(train_loss))

            best_iteration = int(self.model.iterations)

            # Run evaluation
            if self.evaluator:
                eval_metrics = self.evaluator.evaluate(self.model)
                if not isinstance(eval_metrics, dict):
                    raise ValueError('eval_fn should return a dict of metrics')

                # Display eval metrics
                logger.info('Validation metrics: ')
                logger.info(', '.join(['{}={:3.5f}'.format(k, v) for k,v in eval_metrics.items()]))

                # Append metrics
                for k, v in eval_metrics.items():
                    all_metrics[k].append(v)

                # Handle early stopping
                tracking.append((eval_metrics[best_metric_name], int(self.model.iterations), epoch))
                logger.debug('Epoch {} Tracking: {}'.format(epoch, tracking))
                
                if epoch >= best_window:
                    # Get the best value of metric in the window
                    best_metric, best_iteration, best_epoch = best_fn(tracking)
                    if tracking[0][1] == best_iteration:
                        # The best value has gone outside the desired window
                        # hence stop
                        logger.info('Early stopping at iteration {}, epoch {}, {}={:3.5f}'
                                    .format(best_iteration, best_epoch, best_metric_name, best_metric))
                        # Update the file time of that checkpoint file to latest
                        self.model.set_latest(self.task_name, best_iteration)
                        break
                
            if save:
                self.model.save(self.task_name)
                torch.save(self.optimizer.state_dict(), self.opt_path)

        return best_iteration, all_metrics
def train ( self , num_epochs , early_stopping = None , save = True ) : all_metrics = defaultdict ( list ) best_iteration = 0 if early_stopping : if not save : raise ValueError ( 'save should be True for early stopping' ) if self . evaluator is None : raise ValueError ( 'early stopping requires an eval function' ) best_fn , best_window , best_metric_name = self . _get_early_stopping_criteria ( early_stopping ) tracking = deque ( [ ] , best_window + 1 ) for epoch in range ( num_epochs ) : self . train_iter . init_epoch ( ) epoch_loss = 0 count = 0 logger . info ( 'Epoch %d (%d)' % ( epoch + 1 , int ( self . model . iterations ) ) ) prog_iter = tqdm ( self . train_iter , leave = False ) for batch in prog_iter : # Train mode self . model . train ( ) self . optimizer . zero_grad ( ) loss , _ = self . model . loss ( batch ) loss . backward ( ) self . optimizer . step ( ) if self . lr_scheduler_step : self . lr_scheduler_step . step ( ) epoch_loss += loss . item ( ) count += 1 self . model . iterations += 1 # Display loss prog_iter . set_description ( 'Training' ) prog_iter . set_postfix ( loss = ( epoch_loss / count ) ) if self . lr_scheduler_epoch : self . lr_scheduler_epoch . step ( ) train_loss = epoch_loss / count all_metrics [ 'train_loss' ] . append ( train_loss ) logger . info ( 'Train Loss: {:3.5f}' . format ( train_loss ) ) best_iteration = int ( self . model . iterations ) # Run evaluation if self . evaluator : eval_metrics = self . evaluator . evaluate ( self . model ) if not isinstance ( eval_metrics , dict ) : raise ValueError ( 'eval_fn should return a dict of metrics' ) # Display eval metrics logger . info ( 'Validation metrics: ' ) logger . info ( ', ' . join ( [ '{}={:3.5f}' . format ( k , v ) for k , v in eval_metrics . items ( ) ] ) ) # Append metrics for k , v in eval_metrics . items ( ) : all_metrics [ k ] . append ( v ) # Handle early stopping tracking . append ( ( eval_metrics [ best_metric_name ] , int ( self . model . iterations ) , epoch ) ) logger . debug ( 'Epoch {} Tracking: {}' . format ( epoch , tracking ) ) if epoch >= best_window : # Get the best value of metric in the window best_metric , best_iteration , best_epoch = best_fn ( tracking ) if tracking [ 0 ] [ 1 ] == best_iteration : # The best value has gone outside the desired window # hence stop logger . info ( 'Early stopping at iteration {}, epoch {}, {}={:3.5f}' . format ( best_iteration , best_epoch , best_metric_name , best_metric ) ) # Update the file time of that checkpoint file to latest self . model . set_latest ( self . task_name , best_iteration ) break if save : self . model . save ( self . task_name ) torch . save ( self . optimizer . state_dict ( ) , self . opt_path ) return best_iteration , all_metrics