( output_dir: str = 'checkpoints' batch_size: Union[int, Tuple[int, int]] = (16, 2) num_epochs: Union[int, Tuple[int, int]] = (1, 16) max_steps: int = -1 sampling_strategy: str = 'oversampling' num_iterations: Optional[int] = None body_learning_rate: Union[float, Tuple[float, float]] = (2e-05, 1e-05) head_learning_rate: float = 0.01 loss: Callable = <class 'sentence_transformers.losses.CosineSimilarityLoss.CosineSimilarityLoss'> distance_metric: Callable = <function BatchHardTripletLossDistanceFunction.cosine_distance at 0x7fdc203ae440> margin: float = 0.25 end_to_end: bool = False use_amp: bool = False warmup_proportion: float = 0.1 l2_weight: Optional[float] = None max_length: Optional[int] = None samples_per_label: int = 2 show_progress_bar: bool = True seed: int = 42 report_to: str = 'all' run_name: Optional[str] = None logging_dir: Optional[str] = None logging_strategy: str = 'steps' logging_first_step: bool = True logging_steps: int = 50 eval_strategy: str = 'no' evaluation_strategy: Optional[str] = None eval_steps: Optional[int] = None eval_delay: int = 0 eval_max_steps: int = -1 save_strategy: str = 'steps' save_steps: int = 500 save_total_limit: Optional[int] = 1 load_best_model_at_end: bool = False metric_for_best_model: str = 'embedding_loss' greater_is_better: bool = False )
Parameters
str
, defaults to "checkpoints"
) —
The output directory where the model predictions and checkpoints will be written. Union[int, Tuple[int, int]]
, defaults to (16, 2)
) —
Set the batch sizes for the embedding and classifier training phases respectively,
or set both if an integer is provided.
Note that the batch size for the classifier is only used with a differentiable PyTorch head. Union[int, Tuple[int, int]]
, defaults to (1, 16)
) —
Set the number of epochs the embedding and classifier training phases respectively,
or set both if an integer is provided.
Note that the number of epochs for the classifier is only used with a differentiable PyTorch head. int
, defaults to -1
) —
If set to a positive number, the total number of training steps to perform. Overrides num_epochs
.
The training may stop before reaching the set number of steps when all data is exhausted. str
, defaults to "oversampling"
) —
The sampling strategy of how to draw pairs in training. Possible values are:
"oversampling"
: Draws even number of positive/ negative sentence pairs until every
sentence pair has been drawn."undersampling"
: Draws the minimum number of positive/ negative sentence pairs until
every sentence pair in the minority class has been drawn."unique"
: Draws every sentence pair combination (likely resulting in unbalanced
number of positive/ negative sentence pairs).The default is set to "oversampling"
, ensuring all sentence pairs are drawn at least once.
Alternatively, setting num_iterations
will override this argument and determine the number
of generated sentence pairs.
int
, optional) —
If not set the sampling_strategy
will determine the number of sentence pairs to generate.
This argument sets the number of iterations to generate sentence pairs for
and provides compatability with Setfit Union[float, Tuple[float, float]]
, defaults to (2e-5, 1e-5)
) —
Set the learning rate for the SentenceTransformer
body for the embedding and classifier
training phases respectively, or set both if a float is provided.
Note that the body learning rate for the classifier is only used with a differentiable PyTorch
head and if end_to_end=True
. float
, defaults to 1e-2
) —
Set the learning rate for the head for the classifier training phase. Only used with a
differentiable PyTorch head. nn.Module
, defaults to CosineSimilarityLoss
) —
The loss function to use for contrastive training of the embedding training phase. Callable
, defaults to BatchHardTripletLossDistanceFunction.cosine_distance
) —
Function that returns a distance between two embeddings.
It is set for the triplet loss and ignored for CosineSimilarityLoss
and SupConLoss
. float
, defaults to 0.25
) —
Margin for the triplet loss.
Negative samples should be at least margin further apart from the anchor than the positive.
It is ignored for CosineSimilarityLoss
, BatchHardSoftMarginTripletLoss
and SupConLoss
. bool
, defaults to False
) —
If True, train the entire model end-to-end during the classifier training phase.
Otherwise, freeze the SentenceTransformer
body and only train the head.
Only used with a differentiable PyTorch head. bool
, defaults to False
) —
Whether to use Automatic Mixed Precision (AMP) during the embedding training phase.
Only for Pytorch >= 1.6.0 float
, defaults to 0.1
) —
Proportion of the warmup in the total training steps.
Must be greater than or equal to 0.0 and less than or equal to 1.0. float
, optional) —
Optional l2 weight for both the model body and head, passed to the AdamW
optimizer in the
classifier training phase if a differentiable PyTorch head is used. int
, optional) —
The maximum token length a tokenizer can generate. If not provided, the maximum length for
the SentenceTransformer
body is used. int
, defaults to 2
) — Number of consecutive, random and unique samples drawn per label.
This is only relevant for triplet loss and ignored for CosineSimilarityLoss
.
Batch size should be a multiple of samples_per_label. bool
, defaults to True
) —
Whether to display a progress bar for the training epochs and iterations. int
, defaults to 42
) —
Random seed that will be set at the beginning of training. To ensure reproducibility across
runs, use the model_init
argument to Trainer to instantiate the model if it has some
randomly initialized parameters. str
or List[str]
, optional, defaults to "all"
) —
The list of integrations to report the results and logs to. Supported platforms are "azure_ml"
,
"comet_ml"
, "mlflow"
, "neptune"
, "tensorboard"
,"clearml"
and "wandb"
. Use "all"
to report to
all integrations installed, "none"
for no integrations. str
, optional) —
A descriptor for the run. Typically used for wandb and
mlflow logging. str
, optional) —
TensorBoard log directory. Will default to
*runs/CURRENT_DATETIME_HOSTNAME*. str
or IntervalStrategy
, optional, defaults to "steps"
) —
The logging strategy to adopt during training. Possible values are:
"no"
: No logging is done during training."epoch"
: Logging is done at the end of each epoch."steps"
: Logging is done every logging_steps
.bool
, optional, defaults to False
) —
Whether to log and evaluate the first global_step
or not. int
, defaults to 50) —
Number of update steps between two logs if logging_strategy="steps"
. str
or IntervalStrategy
, optional, defaults to "no"
) —
The evaluation strategy to adopt during training. Possible values are:
"no"
: No evaluation is done during training."steps"
: Evaluation is done (and logged) every eval_steps
."epoch"
: Evaluation is done at the end of each epoch.int
, optional) —
Number of update steps between two evaluations if eval_strategy="steps"
. Will default to the same
value as logging_steps
if not set. float
, optional) —
Number of epochs or steps to wait for before the first evaluation can be performed, depending on the
eval_strategy. int
, defaults to -1
) —
If set to a positive number, the total number of evaluation steps to perform. The evaluation may stop
before reaching the set number of steps when all data is exhausted. str
or IntervalStrategy
, optional, defaults to "steps"
) —
The checkpoint save strategy to adopt during training. Possible values are:
"no"
: No save is done during training."epoch"
: Save is done at the end of each epoch."steps"
: Save is done every save_steps
.int
, optional, defaults to 500) —
Number of updates steps before two checkpoint saves if save_strategy="steps"
. int
, optional, defaults to 1
) —
If a value is passed, will limit the total amount of checkpoints. Deletes the older checkpoints in
output_dir
. Note, the best model is always preserved if the eval_strategy
is not "no"
. bool
, optional, defaults to False
) —
Whether or not to load the best model found during training at the end of training.
When set to True
, the parameters save_strategy
needs to be the same as eval_strategy
, and in
the case it is “steps”, save_steps
must be a round multiple of eval_steps
.
TrainingArguments is the subset of the arguments which relate to the training loop itself.
Note that training with SetFit consists of two phases behind the scenes: finetuning embeddings and
training a classification head. As a result, some of the training arguments can be tuples,
where the two values are used for each of the two phases, respectively. The second value is often only
used when training the model was loaded using use_differentiable_head=True
.
( ) → Dict[str, Any]
Returns
Dict[str, Any]
The dictionary variant of this dataclass.
Convert this instance to a dictionary.
( arguments: Dict[str, Any] ignore_extra: bool = False ) → TrainingArguments
Initialize a TrainingArguments instance from a dictionary.
Create a shallow copy of this TrainingArguments instance.
( model: Optional = None args: Optional = None train_dataset: Optional = None eval_dataset: Optional = None model_init: Optional = None metric: Union = 'accuracy' metric_kwargs: Optional = None callbacks: Optional = None column_mapping: Optional = None )
Parameters
SetFitModel
, optional) —
The model to train. If not provided, a model_init
must be passed. TrainingArguments
, optional) —
The training arguments to use. Dataset
) —
The training dataset. Dataset
, optional) —
The evaluation dataset. Callable[[], SetFitModel]
, optional) —
A function that instantiates the model to be used. If provided, each call to
Trainer.train() will start from a new instance of the model as given by this
function when a trial
is passed. str
or Callable
, optional, defaults to "accuracy"
) —
The metric to use for evaluation. If a string is provided, we treat it as the metric
name and load it with default settings. If a callable is provided, it must take two arguments
(y_pred
, y_test
) and return a dictionary with metric keys to values. Dict[str, Any]
, optional) —
Keyword arguments passed to the evaluation function if metric
is an evaluation string like “f1”.
For example useful for providing an averaging strategy for computing f1 in a multi-label setting. List[
TrainerCallback]
, optional) —
A list of callbacks to customize the training loop. Will add those to the list of default callbacks
detailed in here.
If you want to remove one of the default callbacks used, use the Trainer.remove_callback() method. Dict[str, str]
, optional) —
A mapping from the column names in the dataset to the column names expected by the model.
The expected format is a dictionary with the following format:
{"text_column_name": "text", "label_column_name: "label"}
. Trainer to train a SetFit model.
( callback: Union )
Parameters
type
or TrainerCallback) —
A TrainerCallback class or an instance of a TrainerCallback. In the
first case, will instantiate a member of that class. Add a callback to the current list of TrainerCallback.
( params: Dict final_model: bool = False )
Applies a dictionary of hyperparameters to both the trainer and the model
( dataset: Optional = None metric_key_prefix: str = 'test' ) → Dict[str, float]
Computes the metrics for a given classifier.
( hp_space: Optional = None compute_objective: Optional = None n_trials: int = 10 direction: str = 'maximize' backend: Union = None hp_name: Optional = None **kwargs ) → trainer_utils.BestRun
Parameters
Callable[["optuna.Trial"], Dict[str, float]]
, optional) —
A function that defines the hyperparameter search space. Will default to
default_hp_space_optuna
. Callable[[Dict[str, float]], float]
, optional) —
A function computing the objective to minimize or maximize from the metrics returned by the evaluate
method. Will default to default_compute_objective
which uses the sum of metrics. int
, optional, defaults to 100) —
The number of trial runs to test. str
, optional, defaults to "maximize"
) —
Whether to optimize greater or lower objects. Can be "minimize"
or "maximize"
, you should pick
"minimize"
when optimizing the validation loss, "maximize"
when optimizing one or several metrics. str
or HPSearchBackend
, optional) —
The backend to use for hyperparameter search. Only optuna is supported for now.
TODO: add support for ray and sigopt. Callable[["optuna.Trial"], str]]
, optional) —
A function that defines the trial/run name. Will default to None. Dict[str, Any]
, optional) —
Additional keyword arguments passed along to optuna.create_study
. For more
information see:
Returns
trainer_utils.BestRun
All the information about the best run.
Launch a hyperparameter search using optuna
. The optimized quantity is determined
by compute_objective
, which defaults to a function returning the evaluation loss when no metric is provided,
the sum of all metrics otherwise.
To use this method, you need to have provided a model_init
when initializing your Trainer: we need to
reinitialize the model at each new run.
( args: TrainingArguments logs: Dict )
Log logs
on the various objects watching training.
Subclass and override this method to inject custom behavior.
( callback: Union ) → TrainerCallback
Parameters
type
or TrainerCallback) —
A TrainerCallback class or an instance of a TrainerCallback. In the
first case, will pop the first member of that class found in the list of callbacks. Returns
The callback removed, if found.
Remove a callback from the current list of TrainerCallback and returns it.
If the callback is not found, returns None
(and no error is raised).
( repo_id: str **kwargs ) → str
Parameters
str
) —
The full repository ID to push to, e.g. "tomaarsen/setfit-sst2"
. dict
, optional) —
Configuration object to be saved alongside the model weights. str
, optional) —
Message to commit while pushing. bool
, optional, defaults to False
) —
Whether the repository created should be private. str
, optional) —
The API endpoint to use when pushing the model to the hub. str
, optional) —
The token to use as HTTP bearer authorization for remote files.
If not set, will use the token set when logging in with
transformers-cli login
(stored in ~/.huggingface
). str
, optional) —
The git branch on which to push the model. This defaults to
the default branch as specified in your repository, which
defaults to "main"
. boolean
, optional) —
Whether or not to create a Pull Request from branch
with that commit.
Defaults to False
. List[str]
or str
, optional) —
If provided, only files matching at least one pattern are pushed. List[str]
or str
, optional) —
If provided, files matching any of the patterns are not pushed. Returns
str
The url of the commit of your model in the given repository.
Upload model checkpoint to the Hub using huggingface_hub
.
See the full list of parameters for your huggingface_hub
version in the huggingface_hub documentation.
( callback: Union )
Parameters
type
or TrainerCallback) —
A TrainerCallback class or an instance of a TrainerCallback. In the
first case, will remove the first member of that class found in the list of callbacks. Remove a callback from the current list of TrainerCallback.
( args: Optional = None trial: Union = None **kwargs )
Main training entry point.
( x_train: List y_train: Union args: Optional = None )
Method to perform the classifier phase: fitting a classifier head.
( x_train: List y_train: Union = None x_eval: Optional = None y_eval: Union = None args: Optional = None )
Method to perform the embedding phase: finetuning the SentenceTransformer
body.
( teacher_model: SetFitModel student_model: Optional = None args: TrainingArguments = None train_dataset: Optional = None eval_dataset: Optional = None model_init: Optional = None metric: Union = 'accuracy' column_mapping: Optional = None )
Parameters
SetFitModel
) —
The teacher model to mimic. SetFitModel
, optional) —
The model to train. If not provided, a model_init
must be passed. TrainingArguments
, optional) —
The training arguments to use. Dataset
) —
The training dataset. Dataset
, optional) —
The evaluation dataset. Callable[[], SetFitModel]
, optional) —
A function that instantiates the model to be used. If provided, each call to
train() will start from a new instance of the model as given by this
function when a trial
is passed. str
or Callable
, optional, defaults to "accuracy"
) —
The metric to use for evaluation. If a string is provided, we treat it as the metric
name and load it with default settings.
If a callable is provided, it must take two arguments (y_pred
, y_test
). Dict[str, str]
, optional) —
A mapping from the column names in the dataset to the column names expected by the model.
The expected format is a dictionary with the following format:
{"text_column_name": "text", "label_column_name: "label"}
. Trainer to compress a SetFit model with knowledge distillation.
( callback: Union )
Parameters
type
or TrainerCallback) —
A TrainerCallback class or an instance of a TrainerCallback. In the
first case, will instantiate a member of that class. Add a callback to the current list of TrainerCallback.
( params: Dict final_model: bool = False )
Applies a dictionary of hyperparameters to both the trainer and the model
( dataset: Optional = None metric_key_prefix: str = 'test' ) → Dict[str, float]
Computes the metrics for a given classifier.
( hp_space: Optional = None compute_objective: Optional = None n_trials: int = 10 direction: str = 'maximize' backend: Union = None hp_name: Optional = None **kwargs ) → trainer_utils.BestRun
Parameters
Callable[["optuna.Trial"], Dict[str, float]]
, optional) —
A function that defines the hyperparameter search space. Will default to
default_hp_space_optuna
. Callable[[Dict[str, float]], float]
, optional) —
A function computing the objective to minimize or maximize from the metrics returned by the evaluate
method. Will default to default_compute_objective
which uses the sum of metrics. int
, optional, defaults to 100) —
The number of trial runs to test. str
, optional, defaults to "maximize"
) —
Whether to optimize greater or lower objects. Can be "minimize"
or "maximize"
, you should pick
"minimize"
when optimizing the validation loss, "maximize"
when optimizing one or several metrics. str
or HPSearchBackend
, optional) —
The backend to use for hyperparameter search. Only optuna is supported for now.
TODO: add support for ray and sigopt. Callable[["optuna.Trial"], str]]
, optional) —
A function that defines the trial/run name. Will default to None. Dict[str, Any]
, optional) —
Additional keyword arguments passed along to optuna.create_study
. For more
information see:
Returns
trainer_utils.BestRun
All the information about the best run.
Launch a hyperparameter search using optuna
. The optimized quantity is determined
by compute_objective
, which defaults to a function returning the evaluation loss when no metric is provided,
the sum of all metrics otherwise.
To use this method, you need to have provided a model_init
when initializing your Trainer: we need to
reinitialize the model at each new run.
( args: TrainingArguments logs: Dict )
Log logs
on the various objects watching training.
Subclass and override this method to inject custom behavior.
( callback: Union ) → TrainerCallback
Parameters
type
or TrainerCallback) —
A TrainerCallback class or an instance of a TrainerCallback. In the
first case, will pop the first member of that class found in the list of callbacks. Returns
The callback removed, if found.
Remove a callback from the current list of TrainerCallback and returns it.
If the callback is not found, returns None
(and no error is raised).
( repo_id: str **kwargs ) → str
Parameters
str
) —
The full repository ID to push to, e.g. "tomaarsen/setfit-sst2"
. dict
, optional) —
Configuration object to be saved alongside the model weights. str
, optional) —
Message to commit while pushing. bool
, optional, defaults to False
) —
Whether the repository created should be private. str
, optional) —
The API endpoint to use when pushing the model to the hub. str
, optional) —
The token to use as HTTP bearer authorization for remote files.
If not set, will use the token set when logging in with
transformers-cli login
(stored in ~/.huggingface
). str
, optional) —
The git branch on which to push the model. This defaults to
the default branch as specified in your repository, which
defaults to "main"
. boolean
, optional) —
Whether or not to create a Pull Request from branch
with that commit.
Defaults to False
. List[str]
or str
, optional) —
If provided, only files matching at least one pattern are pushed. List[str]
or str
, optional) —
If provided, files matching any of the patterns are not pushed. Returns
str
The url of the commit of your model in the given repository.
Upload model checkpoint to the Hub using huggingface_hub
.
See the full list of parameters for your huggingface_hub
version in the huggingface_hub documentation.
( callback: Union )
Parameters
type
or TrainerCallback) —
A TrainerCallback class or an instance of a TrainerCallback. In the
first case, will remove the first member of that class found in the list of callbacks. Remove a callback from the current list of TrainerCallback.
( args: Optional = None trial: Union = None **kwargs )
Main training entry point.
( x_train: List args: Optional = None )
Method to perform the classifier phase: fitting the student classifier head.
( x_train: List y_train: Union = None x_eval: Optional = None y_eval: Union = None args: Optional = None )
Method to perform the embedding phase: finetuning the SentenceTransformer
body.
( model: AbsaModel args: Optional = None polarity_args: Optional = None train_dataset: Optional = None eval_dataset: Optional = None metric: Union = 'accuracy' metric_kwargs: Optional = None callbacks: Optional = None column_mapping: Optional = None )
Parameters
AbsaModel
) —
The AbsaModel model to train. TrainingArguments
, optional) —
The training arguments to use. If polarity_args
is not defined, then args
is used for both
the aspect and the polarity model. TrainingArguments
, optional) —
The training arguments to use for the polarity model. If not defined, args
is used for both
the aspect and the polarity model. Dataset
) —
The training dataset. The dataset must have “text”, “span”, “label” and “ordinal” columns. Dataset
, optional) —
The evaluation dataset. The dataset must have “text”, “span”, “label” and “ordinal” columns. str
or Callable
, optional, defaults to "accuracy"
) —
The metric to use for evaluation. If a string is provided, we treat it as the metric
name and load it with default settings.
If a callable is provided, it must take two arguments (y_pred
, y_test
). Dict[str, Any]
, optional) —
Keyword arguments passed to the evaluation function if metric
is an evaluation string like “f1”.
For example useful for providing an averaging strategy for computing f1 in a multi-label setting. List[
TrainerCallback]
, optional) —
A list of callbacks to customize the training loop. Will add those to the list of default callbacks
detailed in here.
If you want to remove one of the default callbacks used, use the Trainer.remove_callback() method. Dict[str, str]
, optional) —
A mapping from the column names in the dataset to the column names expected by the model.
The expected format is a dictionary with the following format:
{"text_column_name": "text", "span_column_name": "span", "label_column_name: "label", "ordinal_column_name": "ordinal"}
. Trainer to train a SetFit ABSA model.
( callback: Union )
Parameters
type
or TrainerCallback) —
A TrainerCallback class or an instance of a TrainerCallback. In the
first case, will instantiate a member of that class. Add a callback to the current list of TrainerCallback.
( dataset: Optional = None ) → Dict[str, Dict[str, float]]
Computes the metrics for a given classifier.
( callback: Union ) → Tuple[
TrainerCallback, TrainerCallback]
Parameters
type
or TrainerCallback) —
A TrainerCallback class or an instance of a TrainerCallback. In the
first case, will pop the first member of that class found in the list of callbacks. Returns
Tuple[
TrainerCallback, TrainerCallback]
The callbacks removed from the aspect and polarity trainers, if found.
Remove a callback from the current list of TrainerCallback and returns it.
If the callback is not found, returns None
(and no error is raised).
( repo_id: str polarity_repo_id: Optional = None **kwargs )
Parameters
str
) —
The full repository ID to push to, e.g. "tomaarsen/setfit-aspect"
. str
) —
The full repository ID to push to, e.g. "tomaarsen/setfit-sst2"
. dict
, optional) —
Configuration object to be saved alongside the model weights. str
, optional) —
Message to commit while pushing. bool
, optional, defaults to False
) —
Whether the repository created should be private. str
, optional) —
The API endpoint to use when pushing the model to the hub. str
, optional) —
The token to use as HTTP bearer authorization for remote files.
If not set, will use the token set when logging in with
transformers-cli login
(stored in ~/.huggingface
). str
, optional) —
The git branch on which to push the model. This defaults to
the default branch as specified in your repository, which
defaults to "main"
. boolean
, optional) —
Whether or not to create a Pull Request from branch
with that commit.
Defaults to False
. List[str]
or str
, optional) —
If provided, only files matching at least one pattern are pushed. List[str]
or str
, optional) —
If provided, files matching any of the patterns are not pushed. Upload model checkpoint to the Hub using huggingface_hub
.
See the full list of parameters for your huggingface_hub
version in the huggingface_hub documentation.
( callback: Union )
Parameters
type
or TrainerCallback) —
A TrainerCallback class or an instance of a TrainerCallback. In the
first case, will remove the first member of that class found in the list of callbacks. Remove a callback from the current list of TrainerCallback.
( args: Optional = None polarity_args: Optional = None trial: Union = None **kwargs )
Parameters
TrainingArguments
, optional) —
Temporarily change the aspect training arguments for this training call. TrainingArguments
, optional) —
Temporarily change the polarity training arguments for this training call. optuna.Trial
or Dict[str, Any]
, optional) —
The trial run or the hyperparameter dictionary for hyperparameter search. Main training entry point.
( args: Optional = None trial: Union = None **kwargs )
Train the aspect model only.
( args: Optional = None trial: Union = None **kwargs )
Train the polarity model only.