Transformers documentation

Optimization

You are viewing main version, which requires installation from source. If you'd like regular pip install, checkout the latest stable version (v4.56.2).
Hugging Face's logo
Join the Hugging Face community

and get access to the augmented documentation experience

to get started

Optimization

.optimization 模块提供了:

  • 一个带有固定权重衰减的优化器,可用于微调模型
  • 继承自 _LRSchedule 多个调度器:
  • 一个梯度累积类,用于累积多个批次的梯度

AdaFactor (PyTorch)

class transformers.Adafactor

< >

( params lr = None eps = (1e-30, 0.001) clip_threshold = 1.0 decay_rate = -0.8 beta1 = None weight_decay = 0.0 scale_parameter = True relative_step = True warmup_init = False )

Parameters

  • params (Iterable[nn.parameter.Parameter]) — Iterable of parameters to optimize or dictionaries defining parameter groups.
  • lr (float, optional) — The external learning rate.
  • eps (tuple[float, float], optional, defaults to (1e-30, 0.001)) — Regularization constants for square gradient and parameter scale respectively
  • clip_threshold (float, optional, defaults to 1.0) — Threshold of root mean square of final gradient update
  • decay_rate (float, optional, defaults to -0.8) — Coefficient used to compute running averages of square
  • beta1 (float, optional) — Coefficient used for computing running averages of gradient
  • weight_decay (float, optional, defaults to 0.0) — Weight decay (L2 penalty)
  • scale_parameter (bool, optional, defaults to True) — If True, learning rate is scaled by root mean square
  • relative_step (bool, optional, defaults to True) — If True, time-dependent learning rate is computed instead of external learning rate
  • warmup_init (bool, optional, defaults to False) — Time-dependent learning rate computation depends on whether warm-up initialization is being used

AdaFactor pytorch implementation can be used as a drop in replacement for Adam original fairseq code: https://github.com/pytorch/fairseq/blob/master/fairseq/optim/adafactor.py

Paper: Adafactor: Adaptive Learning Rates with Sublinear Memory Cost https://huggingface.co/papers/1804.04235 Note that this optimizer internally adjusts the learning rate depending on the scale_parameter, relative_step and warmup_init options. To use a manual (external) learning rate schedule you should set scale_parameter=False and relative_step=False.

This implementation handles low-precision (FP16, bfloat) values, but we have not thoroughly tested.

Recommended T5 finetuning settings (https://discuss.huggingface.co/t/t5-finetuning-tips/684/3):

  • Training without LR warmup or clip_threshold is not recommended.

  • Disable relative updates

  • Use scale_parameter=False

  • Additional optimizer operations like gradient clipping should not be used alongside Adafactor

Example:

Adafactor(model.parameters(), scale_parameter=False, relative_step=False, warmup_init=False, lr=1e-3)

Others reported the following combination to work well:

Adafactor(model.parameters(), scale_parameter=True, relative_step=True, warmup_init=True, lr=None)

When using lr=None with Trainer you will most likely need to use AdafactorSchedule

scheduler as following:

from transformers.optimization import Adafactor, AdafactorSchedule

optimizer = Adafactor(model.parameters(), scale_parameter=True, relative_step=True, warmup_init=True, lr=None)
lr_scheduler = AdafactorSchedule(optimizer)
trainer = Trainer(..., optimizers=(optimizer, lr_scheduler))

Usage:

# replace AdamW with Adafactor
optimizer = Adafactor(
    model.parameters(),
    lr=1e-3,
    eps=(1e-30, 1e-3),
    clip_threshold=1.0,
    decay_rate=-0.8,
    beta1=None,
    weight_decay=0.0,
    relative_step=False,
    scale_parameter=False,
    warmup_init=False,
)

step

< >

( closure = None )

Parameters

  • closure (callable, optional) — A closure that reevaluates the model and returns the loss.

Performs a single optimization step

Schedules

Learning Rate Schedules (Pytorch)

class transformers.SchedulerType

< >

( value names = None module = None qualname = None type = None start = 1 )

Scheduler names for the parameter lr_scheduler_type in TrainingArguments. By default, it uses “linear”. Internally, this retrieves get_linear_schedule_with_warmup scheduler from Trainer. Scheduler types:

transformers.get_scheduler

< >

( name: typing.Union[str, transformers.trainer_utils.SchedulerType] optimizer: Optimizer num_warmup_steps: typing.Optional[int] = None num_training_steps: typing.Optional[int] = None scheduler_specific_kwargs: typing.Optional[dict] = None )

Parameters

  • name (str or SchedulerType) — The name of the scheduler to use.
  • optimizer (torch.optim.Optimizer) — The optimizer that will be used during training.
  • num_warmup_steps (int, optional) — The number of warmup steps to do. This is not required by all schedulers (hence the argument being optional), the function will raise an error if it’s unset and the scheduler type requires it.
  • num_training_steps (`int“, optional) — The number of training steps to do. This is not required by all schedulers (hence the argument being optional), the function will raise an error if it’s unset and the scheduler type requires it.
  • scheduler_specific_kwargs (dict, optional) — Extra parameters for schedulers such as cosine with restarts. Mismatched scheduler types and scheduler parameters will cause the scheduler function to raise a TypeError.

Unified API to get any scheduler from its name.

transformers.get_constant_schedule

< >

( optimizer: Optimizer last_epoch: int = -1 )

Parameters

  • optimizer (~torch.optim.Optimizer) — The optimizer for which to schedule the learning rate.
  • last_epoch (int, optional, defaults to -1) — The index of the last epoch when resuming training.

Create a schedule with a constant learning rate, using the learning rate set in optimizer.

transformers.get_constant_schedule_with_warmup

< >

( optimizer: Optimizer num_warmup_steps: int last_epoch: int = -1 )

Parameters

  • optimizer (~torch.optim.Optimizer) — The optimizer for which to schedule the learning rate.
  • num_warmup_steps (int) — The number of steps for the warmup phase.
  • last_epoch (int, optional, defaults to -1) — The index of the last epoch when resuming training.

Create a schedule with a constant learning rate preceded by a warmup period during which the learning rate increases linearly between 0 and the initial lr set in the optimizer.

transformers.get_cosine_schedule_with_warmup

< >

( optimizer: Optimizer num_warmup_steps: int num_training_steps: int num_cycles: float = 0.5 last_epoch: int = -1 )

Parameters

  • optimizer (~torch.optim.Optimizer) — The optimizer for which to schedule the learning rate.
  • num_warmup_steps (int) — The number of steps for the warmup phase.
  • num_training_steps (int) — The total number of training steps.
  • num_cycles (float, optional, defaults to 0.5) — The number of waves in the cosine schedule (the defaults is to just decrease from the max value to 0 following a half-cosine).
  • last_epoch (int, optional, defaults to -1) — The index of the last epoch when resuming training.

Create a schedule with a learning rate that decreases following the values of the cosine function between the initial lr set in the optimizer to 0, after a warmup period during which it increases linearly between 0 and the initial lr set in the optimizer.

transformers.get_cosine_with_hard_restarts_schedule_with_warmup

< >

( optimizer: Optimizer num_warmup_steps: int num_training_steps: int num_cycles: int = 1 last_epoch: int = -1 )

Parameters

  • optimizer (~torch.optim.Optimizer) — The optimizer for which to schedule the learning rate.
  • num_warmup_steps (int) — The number of steps for the warmup phase.
  • num_training_steps (int) — The total number of training steps.
  • num_cycles (int, optional, defaults to 1) — The number of hard restarts to use.
  • last_epoch (int, optional, defaults to -1) — The index of the last epoch when resuming training.

Create a schedule with a learning rate that decreases following the values of the cosine function between the initial lr set in the optimizer to 0, with several hard restarts, after a warmup period during which it increases linearly between 0 and the initial lr set in the optimizer.

transformers.get_linear_schedule_with_warmup

< >

( optimizer num_warmup_steps num_training_steps last_epoch = -1 )

Parameters

  • optimizer (~torch.optim.Optimizer) — The optimizer for which to schedule the learning rate.
  • num_warmup_steps (int) — The number of steps for the warmup phase.
  • num_training_steps (int) — The total number of training steps.
  • last_epoch (int, optional, defaults to -1) — The index of the last epoch when resuming training.

Create a schedule with a learning rate that decreases linearly from the initial lr set in the optimizer to 0, after a warmup period during which it increases linearly from 0 to the initial lr set in the optimizer.

transformers.get_polynomial_decay_schedule_with_warmup

< >

( optimizer num_warmup_steps num_training_steps lr_end = 1e-07 power = 1.0 last_epoch = -1 )

Parameters

  • optimizer (~torch.optim.Optimizer) — The optimizer for which to schedule the learning rate.
  • num_warmup_steps (int) — The number of steps for the warmup phase.
  • num_training_steps (int) — The total number of training steps.
  • lr_end (float, optional, defaults to 1e-7) — The end LR.
  • power (float, optional, defaults to 1.0) — Power factor.
  • last_epoch (int, optional, defaults to -1) — The index of the last epoch when resuming training.

Create a schedule with a learning rate that decreases as a polynomial decay from the initial lr set in the optimizer to end lr defined by lr_end, after a warmup period during which it increases linearly from 0 to the initial lr set in the optimizer.

Note: power defaults to 1.0 as in the fairseq implementation, which in turn is based on the original BERT implementation at https://github.com/google-research/bert/blob/f39e881b169b9d53bea03d2d341b31707a6c052b/optimization.py#L37

transformers.get_inverse_sqrt_schedule

< >

( optimizer: Optimizer num_warmup_steps: int timescale: typing.Optional[int] = None last_epoch: int = -1 )

Parameters

  • optimizer (~torch.optim.Optimizer) — The optimizer for which to schedule the learning rate.
  • num_warmup_steps (int) — The number of steps for the warmup phase.
  • timescale (int, optional, defaults to num_warmup_steps) — Time scale.
  • last_epoch (int, optional, defaults to -1) — The index of the last epoch when resuming training.

Create a schedule with an inverse square-root learning rate, from the initial lr set in the optimizer, after a warmup period which increases lr linearly from 0 to the initial lr set in the optimizer.

< > Update on GitHub