simpletransformers/language_modeling/language_modeling_model.py
def train_model(
        self, train_file, output_dir=None, show_running_loss=True, args=None, eval_file=None, verbose=True, **kwargs,
    ):
        """
        Trains the model using 'train_file'

        Args:
            train_file: Path to text file containing the text to train the language model on.
            output_dir: The directory where model files will be saved. If not given, self.args.output_dir will be used.
            show_running_loss (optional): Set to False to prevent running loss from being printed to console. Defaults to True.
            args (optional): Optional changes to the args dict of the model. Any changes made will persist for the model.
            eval_file (optional): Path to eval file containing the text to evaluate the language model on.

        Returns:
            None
        """  # noqa: ignore flake8"

        if args:
            self.args.update_from_dict(args)

        if self.args.silent:
            show_running_loss = False

        if self.args.evaluate_during_training and eval_file is None:
            raise ValueError(
                "evaluate_during_training is enabled but eval_file is not specified."
                " Pass eval_file to model.train_model() if using evaluate_during_training."
            )

        if not output_dir:
            output_dir = self.args.output_dir

        if os.path.exists(output_dir) and os.listdir(output_dir) and not self.args.overwrite_output_dir:
            raise ValueError(
                "Output directory ({}) already exists and is not empty."
                " Set args.overwrite_output_dir = True to overcome.".format(output_dir)
            )

        self._move_model_to_device()

        train_dataset = self.load_and_cache_examples(train_file, verbose=verbose)

        os.makedirs(output_dir, exist_ok=True)

        global_step, tr_loss = self.train(
            train_dataset,
            output_dir,
            show_running_loss=show_running_loss,
            eval_file=eval_file,
            verbose=verbose,
            **kwargs,
        )

        self.save_model(output_dir, model=self.model)
        if self.args.model_type == "electra":
            self.save_discriminator()
            self.save_generator()
        # model_to_save = self.model.module if hasattr(self.model, "module") else self.model
        # model_to_save.save_pretrained(output_dir)
        # self.tokenizer.save_pretrained(output_dir)
        # torch.save(self.args, os.path.join(output_dir, "training_args.bin"))

        if verbose:
            logger.info(" Training of {} model complete. Saved to {}.".format(self.args.model_type, output_dir))

        Trains the model using 'train_file'

        Args:
            train_file: Path to text file containing the text to train the language model on.
            output_dir: The directory where model files will be saved. If not given, self.args.output_dir will be used.
            show_running_loss (optional): Set to False to prevent running loss from being printed to console. Defaults to True.
            args (optional): Optional changes to the args dict of the model. Any changes made will persist for the model.
            eval_file (optional): Path to eval file containing the text to evaluate the language model on.

        Returns:
            None
        
Trains the model using 'train_file'
Trains the model using ' train_file '
def train_model(
        self, train_file, output_dir=None, show_running_loss=True, args=None, eval_file=None, verbose=True, **kwargs,
    ):
        if args:
            self.args.update_from_dict(args)
        if self.args.silent:
            show_running_loss = False
        if self.args.evaluate_during_training and eval_file is None:
            raise ValueError(
                "evaluate_during_training is enabled but eval_file is not specified."
                " Pass eval_file to model.train_model() if using evaluate_during_training."
            )
        if not output_dir:
            output_dir = self.args.output_dir
        if os.path.exists(output_dir) and os.listdir(output_dir) and not self.args.overwrite_output_dir:
            raise ValueError(
                "Output directory ({}) already exists and is not empty."
                " Set args.overwrite_output_dir = True to overcome.".format(output_dir)
            )
        self._move_model_to_device()
        train_dataset = self.load_and_cache_examples(train_file, verbose=verbose)
        os.makedirs(output_dir, exist_ok=True)
        global_step, tr_loss = self.train(
            train_dataset,
            output_dir,
            show_running_loss=show_running_loss,
            eval_file=eval_file,
            verbose=verbose,
            **kwargs,
        )
        self.save_model(output_dir, model=self.model)
        if self.args.model_type == "electra":
            self.save_discriminator()
            self.save_generator()
        if verbose:
            logger.info(" Training of {} model complete. Saved to {}.".format(self.args.model_type, output_dir))
def train_model ( self , train_file , output_dir = None , show_running_loss = True , args = None , eval_file = None , verbose = True , ** kwargs , ) : if args : self . args . update_from_dict ( args ) if self . args . silent : show_running_loss = False if self . args . evaluate_during_training and eval_file is None : raise ValueError ( "evaluate_during_training is enabled but eval_file is not specified." " Pass eval_file to model.train_model() if using evaluate_during_training." ) if not output_dir : output_dir = self . args . output_dir if os . path . exists ( output_dir ) and os . listdir ( output_dir ) and not self . args . overwrite_output_dir : raise ValueError ( "Output directory ({}) already exists and is not empty." " Set args.overwrite_output_dir = True to overcome." . format ( output_dir ) ) self . _move_model_to_device ( ) train_dataset = self . load_and_cache_examples ( train_file , verbose = verbose ) os . makedirs ( output_dir , exist_ok = True ) global_step , tr_loss = self . train ( train_dataset , output_dir , show_running_loss = show_running_loss , eval_file = eval_file , verbose = verbose , ** kwargs , ) self . save_model ( output_dir , model = self . model ) if self . args . model_type == "electra" : self . save_discriminator ( ) self . save_generator ( ) if verbose : logger . info ( " Training of {} model complete. Saved to {}." . format ( self . args . model_type , output_dir ) )
Trains the model using 'train_file'
Trains the model using ' train_file '
"""
        Trains the model using 'train_file'

        Args:
            train_file: Path to text file containing the text to train the language model on.
            output_dir: The directory where model files will be saved. If not given, self.args.output_dir will be used.
            show_running_loss (optional): Set to False to prevent running loss from being printed to console. Defaults to True.
            args (optional): Optional changes to the args dict of the model. Any changes made will persist for the model.
            eval_file (optional): Path to eval file containing the text to evaluate the language model on.

        Returns:
            None
        """ # noqa: ignore flake8" # model_to_save = self.model.module if hasattr(self.model, "module") else self.model # model_to_save.save_pretrained(output_dir) # self.tokenizer.save_pretrained(output_dir) # torch.save(self.args, os.path.join(output_dir, "training_args.bin"))
None
self
train_file
Path to text file containing the text to train the language model on.
Path to text file containing the text to train the language model on .
output_dir
The directory where model files will be saved. If not given, self.args.output_dir will be used.
The directory where model files will be saved . If not given self . args . output_dir will be used .
show_running_loss
Set to False to prevent running loss from being printed to console. Defaults to True.
Set to False to prevent running loss from being printed to console . Defaults to True .
True
args
Optional changes to the args dict of the model. Any changes made will persist for the model.
Optional changes to the args dict of the model . Any changes made will persist for the model .
eval_file
Path to eval file containing the text to evaluate the language model on.
Path to eval file containing the text to evaluate the language model on .
verbose