nlpmodels/utils/train.py
def run(self):
        """
        Main running function for training a model.
        """
        for epoch in range(self._args.num_epochs):

            self._model.train()

            pbar = tqdm(self._train_data)
            pbar.set_description("[Epoch {}]".format(epoch))

            self._iter = 0

            correct = 0.0

            # iterate over batches
            for data in pbar:
                # re-format data for Transformer model
                data = self._reformat_data(data)

                # step 1. zero the gradients
                self._optimizer.zero_grad()

                # step 2. forward_prop
                y_hat = self._model(data)

                # step 3. compute the loss
                loss = self._calc_loss_function(y_hat, data)

                loss = loss.mean()

                # step 4. back_prop
                loss.backward()

                # step 5. use optimizer to take gradient step
                self._optimizer.step()

                # NOTE: Usually makes sense to measure the loss from the val set (and add early stopping).
                # For this experiment, just running on training set entirely as an example.

                if self._calc_accuracy:
                    target, text = data

                    correct += int((torch.max(y_hat, 1)[1].view(-1) == target.view(-1)).sum())

                    # status bar
                    pbar.set_postfix(loss=loss.item(), accuracy=100.0 * int(correct) / len(self._train_data.dataset))
                else:
                    # status bar
                    pbar.set_postfix(loss=loss.item())

                # debug
                if self._debug:
                    self._iter += 1
                    if self._iter >= self._max_iter:
                        break

            self._loss_cache.append(loss)

        print("Finished Training...")

        Main running function for training a model.
        
Main running function for training a model.
Main running function for training a model .
def run(self):
        for epoch in range(self._args.num_epochs):
            self._model.train()
            pbar = tqdm(self._train_data)
            pbar.set_description("[Epoch {}]".format(epoch))
            self._iter = 0
            correct = 0.0
            for data in pbar:
                data = self._reformat_data(data)
                self._optimizer.zero_grad()
                y_hat = self._model(data)
                loss = self._calc_loss_function(y_hat, data)
                loss = loss.mean()
                loss.backward()
                self._optimizer.step()
                if self._calc_accuracy:
                    target, text = data
                    correct += int((torch.max(y_hat, 1)[1].view(-1) == target.view(-1)).sum())
                    pbar.set_postfix(loss=loss.item(), accuracy=100.0 * int(correct) / len(self._train_data.dataset))
                else:
                    pbar.set_postfix(loss=loss.item())
                if self._debug:
                    self._iter += 1
                    if self._iter >= self._max_iter:
                        break
            self._loss_cache.append(loss)
        print("Finished Training...")
def run ( self ) : for epoch in range ( self . _args . num_epochs ) : self . _model . train ( ) pbar = tqdm ( self . _train_data ) pbar . set_description ( "[Epoch {}]" . format ( epoch ) ) self . _iter = 0 correct = 0.0 for data in pbar : data = self . _reformat_data ( data ) self . _optimizer . zero_grad ( ) y_hat = self . _model ( data ) loss = self . _calc_loss_function ( y_hat , data ) loss = loss . mean ( ) loss . backward ( ) self . _optimizer . step ( ) if self . _calc_accuracy : target , text = data correct += int ( ( torch . max ( y_hat , 1 ) [ 1 ] . view ( - 1 ) == target . view ( - 1 ) ) . sum ( ) ) pbar . set_postfix ( loss = loss . item ( ) , accuracy = 100.0 * int ( correct ) / len ( self . _train_data . dataset ) ) else : pbar . set_postfix ( loss = loss . item ( ) ) if self . _debug : self . _iter += 1 if self . _iter >= self . _max_iter : break self . _loss_cache . append ( loss ) print ( "Finished Training..." )
Main running function for training a model.
Main running function for training a model .
"""
        Main running function for training a model.
        """ # iterate over batches # re-format data for Transformer model # step 1. zero the gradients # step 2. forward_prop # step 3. compute the loss # step 4. back_prop # step 5. use optimizer to take gradient step # NOTE: Usually makes sense to measure the loss from the val set (and add early stopping). # For this experiment, just running on training set entirely as an example. # status bar # status bar # debug
self