algo/transformers/run_model.py
def train_model(
        self,
        train_df,
        multi_label=False,
        output_dir=None,
        show_running_loss=True,
        args=None,
        eval_df=None,
        verbose=True,
        **kwargs,
    ):
        """
        Trains the model using 'train_df'

        Args:
            train_df: Pandas Dataframe containing at least two columns. If the Dataframe has a header, it should contain a 'text' and a 'labels' column. If no header is present,
            the Dataframe should contain at least two columns, with the first column containing the text, and the second column containing the label. The model will be trained on this Dataframe.
            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_df (optional): A DataFrame against which evaluation will be performed when evaluate_during_training is enabled. Is required if evaluate_during_training is enabled.
            **kwargs: Additional metrics that should be used. Pass in the metrics as keyword arguments (name of metric: function to use). E.g. f1=sklearn.metrics.f1_score.
                        A metric function should take in two parameters. The first parameter will be the true labels, and the second parameter will be the predictions.

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

        if args:
            self.args.update(args)

        if self.args["silent"]:
            show_running_loss = False

        if self.args["evaluate_during_training"] and eval_df is None:
            raise ValueError(
                "evaluate_during_training is enabled but eval_df is not specified."
                " Pass eval_df 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."
                " Use --overwrite_output_dir to overcome.".format(output_dir)
            )

        self._move_model_to_device()

        if "text_a" in train_df.columns and "text_b" in train_df.columns:
            train_examples = [
                InputExample(i, text_a, text_b, label)
                for i, (text_a, text_b, label) in enumerate(
                    zip(train_df["text_a"], train_df["text_b"], train_df["labels"])
                )
            ]
        else:
            raise ValueError(
                "Passed DataFrame is not in the correct format. Please rename your columns to text_a, text_b and labels"
            )

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

        os.makedirs(output_dir, exist_ok=True)

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

        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:
            print("Training of {} model complete. Saved to {}.".format(self.args["model_type"], output_dir))

        Trains the model using 'train_df'

        Args:
            train_df: Pandas Dataframe containing at least two columns. If the Dataframe has a header, it should contain a 'text' and a 'labels' column. If no header is present,
            the Dataframe should contain at least two columns, with the first column containing the text, and the second column containing the label. The model will be trained on this Dataframe.
            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_df (optional): A DataFrame against which evaluation will be performed when evaluate_during_training is enabled. Is required if evaluate_during_training is enabled.
            **kwargs: Additional metrics that should be used. Pass in the metrics as keyword arguments (name of metric: function to use). E.g. f1=sklearn.metrics.f1_score.
                        A metric function should take in two parameters. The first parameter will be the true labels, and the second parameter will be the predictions.

        Returns:
            None
        
Trains the model using 'train_df'
Args:
train_df: Pandas Dataframe containing at least two columns. If the Dataframe has a header, it should contain a 'text' and a 'labels' column. If no header is present,
the Dataframe should contain at least two columns, with the first column containing the text, and the second column containing the label. The model will be trained on this Dataframe.
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_df (optional): A DataFrame against which evaluation will be performed when evaluate_during_training is enabled. Is required if evaluate_during_training is enabled.
kwargs: Additional metrics that should be used. Pass in the metrics as keyword arguments (name of metric: function to use).

None
Trains the model using ' train_df ' Args : train_df : Pandas Dataframe containing at least two columns . If the Dataframe has a header it should contain a ' text ' and a ' labels ' column . If no header is present the Dataframe should contain at least two columns with the first column containing the text and the second column containing the label . The model will be trained on this Dataframe . 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_df ( optional ) : A DataFrame against which evaluation will be performed when evaluate_during_training is enabled . Is required if evaluate_during_training is enabled . kwargs : Additional metrics that should be used . Pass in the metrics as keyword arguments ( name of metric : function to use ) . None
def train_model(
        self,
        train_df,
        multi_label=False,
        output_dir=None,
        show_running_loss=True,
        args=None,
        eval_df=None,
        verbose=True,
        **kwargs,
    ):
        if args:
            self.args.update(args)
        if self.args["silent"]:
            show_running_loss = False
        if self.args["evaluate_during_training"] and eval_df is None:
            raise ValueError(
                "evaluate_during_training is enabled but eval_df is not specified."
                " Pass eval_df 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."
                " Use --overwrite_output_dir to overcome.".format(output_dir)
            )
        self._move_model_to_device()
        if "text_a" in train_df.columns and "text_b" in train_df.columns:
            train_examples = [
                InputExample(i, text_a, text_b, label)
                for i, (text_a, text_b, label) in enumerate(
                    zip(train_df["text_a"], train_df["text_b"], train_df["labels"])
                )
            ]
        else:
            raise ValueError(
                "Passed DataFrame is not in the correct format. Please rename your columns to text_a, text_b and labels"
            )
        train_dataset = self.load_and_cache_examples(train_examples, verbose=verbose)
        os.makedirs(output_dir, exist_ok=True)
        global_step, tr_loss = self.train(
            train_dataset,
            output_dir,
            multi_label=multi_label,
            show_running_loss=show_running_loss,
            eval_df=eval_df,
            verbose=verbose,
            **kwargs,
        )
        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:
            print("Training of {} model complete. Saved to {}.".format(self.args["model_type"], output_dir))
def train_model ( self , train_df , multi_label = False , output_dir = None , show_running_loss = True , args = None , eval_df = None , verbose = True , ** kwargs , ) : if args : self . args . update ( args ) if self . args [ "silent" ] : show_running_loss = False if self . args [ "evaluate_during_training" ] and eval_df is None : raise ValueError ( "evaluate_during_training is enabled but eval_df is not specified." " Pass eval_df 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." " Use --overwrite_output_dir to overcome." . format ( output_dir ) ) self . _move_model_to_device ( ) if "text_a" in train_df . columns and "text_b" in train_df . columns : train_examples = [ InputExample ( i , text_a , text_b , label ) for i , ( text_a , text_b , label ) in enumerate ( zip ( train_df [ "text_a" ] , train_df [ "text_b" ] , train_df [ "labels" ] ) ) ] else : raise ValueError ( "Passed DataFrame is not in the correct format. Please rename your columns to text_a, text_b and labels" ) train_dataset = self . load_and_cache_examples ( train_examples , verbose = verbose ) os . makedirs ( output_dir , exist_ok = True ) global_step , tr_loss = self . train ( train_dataset , output_dir , multi_label = multi_label , show_running_loss = show_running_loss , eval_df = eval_df , verbose = verbose , ** kwargs , ) 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 : print ( "Training of {} model complete. Saved to {}." . format ( self . args [ "model_type" ] , output_dir ) )
Trains the model using 'train_df'
Args:
train_df: Pandas Dataframe containing at least two columns.
Trains the model using ' train_df ' Args : train_df : Pandas Dataframe containing at least two columns .
"""
        Trains the model using 'train_df'

        Args:
            train_df: Pandas Dataframe containing at least two columns. If the Dataframe has a header, it should contain a 'text' and a 'labels' column. If no header is present,
            the Dataframe should contain at least two columns, with the first column containing the text, and the second column containing the label. The model will be trained on this Dataframe.
            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_df (optional): A DataFrame against which evaluation will be performed when evaluate_during_training is enabled. Is required if evaluate_during_training is enabled.
            **kwargs: Additional metrics that should be used. Pass in the metrics as keyword arguments (name of metric: function to use). E.g. f1=sklearn.metrics.f1_score.
                        A metric function should take in two parameters. The first parameter will be the true labels, and the second parameter will be the predictions.

        Returns:
            None
        """ # noqa: ignore flake8"
self
train_df
multi_label
output_dir
show_running_loss
args
eval_df
verbose