/usr/local/lib/python3.6/site-packages/transformers/__pycache__
Edit: /usr/local/lib/python3.6/site-packages/transformers/__pycache__/optimization.cpython-36.pyc (22653B)
3
Egll @ s d Z ddlZddlZddlmZmZmZmZmZ ddl Z ddl m
Z
ddlmZ ddl
mZ ddlmZ dd lmZ dd
lmZ ejeZd*eeddd
Zd,eeedddZd.ddZd0eeeeedddZd2eeeeedddZd4ddZej eej!eej"eej#eej$eej%eiZ&d5ee'ef eee ee dddZ(G d d! d!eZ)G d"d# d#eZ*G d$d% d%eZ+d6d'd(Z,dS )7z$PyTorch optimization for BERT model. N)CallableIterableOptionalTupleUnion)nn) Optimizer)LambdaLR )
SchedulerType)logging)require_version) optimizer
last_epochc C s t | dd |dS )a
Create a schedule with a constant learning rate, using the learning rate set in optimizer.
Args:
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.
Return:
`torch.optim.lr_scheduler.LambdaLR` with the appropriate schedule.
c S s dS )Nr
)_r r C/usr/local/lib/python3.6/site-packages/transformers/optimization.py
/ s z'get_constant_schedule..)r )r )r r r r r get_constant_schedule" s
r )r num_warmup_stepsr c s t d fdd}t| ||dS )ad
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.
Args:
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.
Return:
`torch.optim.lr_scheduler.LambdaLR` with the appropriate schedule.
)current_stepc s" | k rt | t td S dS )Ng ?)floatmax)r )r r r lr_lambdaC s z4get_constant_schedule_with_warmup..lr_lambda)r )intr )r r r r r )r r !get_constant_schedule_with_warmup2 s r c s t d fdd}t| ||S )a
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.
Args:
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.
Return:
`torch.optim.lr_scheduler.LambdaLR` with the appropriate schedule.
)r c sB | k rt | t td S tdt | t td S )Nr
g )r r )r )num_training_stepsr r r r ^ s z2get_linear_schedule_with_warmup..lr_lambda)r r )r r r r r r )r r r get_linear_schedule_with_warmupK s r ?)r r r
num_cyclesr c s fdd}t | ||S )a
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.
Args:
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.
Return:
`torch.optim.lr_scheduler.LambdaLR` with the appropriate schedule.
c sf | k rt | t td S t | t td }tdddtjtjt d | S )Nr
g g ?g ?g @)r r mathcospi)r progress)r r r r r r s z2get_cosine_schedule_with_warmup..lr_lambda)r )r r r r r r r )r r r r get_cosine_schedule_with_warmuph s r$ c s fdd}t | ||S )a
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.
Args:
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.
Return:
`torch.optim.lr_scheduler.LambdaLR` with the appropriate schedule.
c sr | k rt | t td S t | t td }|dkrHdS tdddtjtjt | d S )Nr
g ?g g ?)r r r r! r" )r r# )r r r r r r s zEget_cosine_with_hard_restarts_schedule_with_warmup..lr_lambda)r )r r r r r r r )r r r r 2get_cosine_with_hard_restarts_schedule_with_warmup s r% Hz> ?c sN | j d ks(td d dtd fdd}t| ||S )a
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.
Args:
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.
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
Return:
`torch.optim.lr_scheduler.LambdaLR` with the appropriate schedule.
lrzlr_end (z&) must be be smaller than initial lr ())r c sj | k rt | t td S | kr. S } }d| | }|| }| S d S )Nr
)r r )r Zlr_rangeZdecay_stepsZ
pct_remainingZdecay)lr_endlr_initr r powerr r r s z.lr_lambda)defaults
ValueErrorr r )r r r r* r, r r r )r* r+ r r r, r )get_polynomial_decay_schedule_with_warmup s
r/ )namer r r c C sr t | } t| }| t jkr"||S |dkr8t| d| t jkrN|||dS |dkrdt| d||||dS )a
Unified API to get any scheduler from its name.
Args:
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.
Nz; requires `num_warmup_steps`, please provide that argument.)r z= requires `num_training_steps`, please provide that argument.)r r )r TYPE_TO_SCHEDULER_FUNCTIONCONSTANTr. CONSTANT_WITH_WARMUP)r0 r r r Z
schedule_funcr r r
get_scheduler s
r4 c sT e Zd ZdZdeejj ee eef eee
e
d fd
d
Zded
ddZ
ZS )AdamWa5
Implements Adam algorithm with weight decay fix as introduced in [Decoupled Weight Decay
Regularization](https://arxiv.org/abs/1711.05101).
Parameters:
params (`Iterable[nn.parameter.Parameter]`):
Iterable of parameters to optimize or dictionaries defining parameter groups.
lr (`float`, *optional*, defaults to 1e-3):
The learning rate to use.
betas (`Tuple[float,float]`, *optional*, defaults to (0.9, 0.999)):
Adam's betas parameters (b1, b2).
eps (`float`, *optional*, defaults to 1e-6):
Adam's epsilon for numerical stability.
weight_decay (`float`, *optional*, defaults to 0):
Decoupled weight decay to apply.
correct_bias (`bool`, *optional*, defaults to `True`):
Whether or not to correct bias in Adam (for instance, in Bert TF repository they use `False`).
no_deprecation_warning (`bool`, *optional*, defaults to `False`):
A flag used to disable the deprecation warning (set to `True` to disable the warning).
MbP??+?ư> TF)paramsr( betasepsweight_decaycorrect_biasno_deprecation_warningc s |st jdt td |dk r0td| dd|d koFdk n s`td|d d d|d
kovdk n std|d
d d|kstd| dt|||||d}t j|| d S )
NzThis implementation of AdamW is deprecated and will be removed in a future version. Use the PyTorch implementation torch.optim.AdamW instead, or set `no_deprecation_warning=True` to disable this warningztorch>=1.5.0g zInvalid learning rate: z - should be >= 0.0r g ?zInvalid beta parameter: z - should be in [0.0, 1.0)r
zInvalid epsilon value: )r( r<