Adafactor#
- class torch.optim.Adafactor(params, lr=0.01, beta2_decay=-0.8, eps=(None, 0.001), d=1.0, weight_decay=0.0, *, foreach=None, maximize=False)#
Implements Adafactor algorithm.
For further details regarding the algorithm we refer to Adafactor: Adaptive Learning Rates with Sublinear Memory Cost.
- 引數
params (iterable) – 要最佳化的引數或命名引數的迭代器,或者是定義引數組的字典的迭代器。使用命名引數時,所有組中的所有引數都應該命名。
lr (float, Tensor, optional) – unlike other optimizers, Adafactor does not require a learning rate, and Noam Shazeer and Mitchell Stern do not use lr at all. Deviating from the paper, this implementation uses lr for applying weight decay and as the maximum value for relative step size rho_t. Note that in the paper, a constant of 0.01 is used as the maximum value for relative step size, and so we set 0.01 as the default value. (default: 1e-2)
beta2_decay (float, optional) – the decay rate of beta2. beta2 standardly refers to the coefficient used for computing the running average of the gradient squared. (default: -0.8)
eps (Tuple[float, float], optional) – epsilon1 is the term added to the denominator of the update calculation to improve numerical stability. This use of epsilon1 deviates from the algorithm written in the paper! See note below for more details. epsilon2 is the term used to avoid having too small a weight update when applying parameter scaling. (default: (None, 1e-3))
d (float, optional) – the clipping threshold, used to avoid larger-than-desired updates.
weight_decay (float, optional) – weight decay coefficient (default: 1e-2)
foreach (bool, optional) – whether foreach implementation of optimizer is used. Note that the foreach implementation uses ~ sizeof(params) more peak memory than the for-loop version due to the intermediates being a tensorlist vs just one tensor. As Adafactor is commonly used when memory is prohibitive, Adafactor will default to the slower single tensor for-loop implementation unless this flag is explicitly True. This behavior is contrary to other optimizers, which will attempt defaulting to foreach on CUDA for faster runtime. (default: None)
maximize (bool, optional) – 最大化目標函式相對於 params,而不是最小化 (預設: False)
注意
The implementation of Adafactor subtly differs from Noam Shazeer and Mitchell Stern and implementations in some other frameworks with its use of learning rate and .
Regarding the learning rate hyperparameter: Noam Shazeer and Mitchell Stern do not use lr at all, as the stated algorithm uses and update clipping to affect the step size.
This implementation allows lr to influence the maximum value for
This differs from Noam Shazeer and Mitchell Stern, who use a constant of 0.01 as the maximum value of
Noam Shazeer and Mitchell Stern do not enforce an opinion on how weight decay should be computed, and so we use the learning rate as a coefficient for decoupled weight decay, similar to what is suggested in Decoupled Weight Decay Regularization.
Regarding the use of : The implementation attempts to replicate the presumed intention of Noam Shazeer and Mitchell Stern to use as a stabilizing term when the squared gradient becomes small.
This stabilization can be written as
where the row and column factors of gradient squared and are left alone, and we apply at the final calculation of the variance estimate and for the update .
This is in contrast to Noam Shazeer and Mitchell Stern and other frameworks which apply to both row and column factors of the squared gradient, but not in the calculations after
您可能會注意到,Noam Shazeer 和 Mitchell Stern 描述使用了梯度的平方和,而本實現則使用了平均值。這個選擇在數學上是等價的,並且對於大的求和來說,能夠提供更好的數值穩定性。
- add_param_group(param_group)[source]#
向
Optimizer的 param_groups 新增一個引數組。這在微調預訓練網路時可能很有用,因為隨著訓練的進行,可以使凍結的層變得可訓練並新增到
Optimizer中。- 引數
param_group (dict) – 指定哪些 Tensor 應該被最佳化,以及組特定的最佳化選項。
- load_state_dict(state_dict)[source]#
載入最佳化器狀態。
- 引數
state_dict (dict) – 最佳化器狀態。應為呼叫
state_dict()返回的物件。
警告
請確保在初始化
torch.optim.lr_scheduler.LRScheduler後呼叫此方法,因為在此之前呼叫會覆蓋載入的學習率。注意
引數的名稱(如果它們存在於
state_dict()中每個引數組的“param_names”鍵下)不會影響載入過程。要為自定義情況(例如,當載入的狀態字典中的引數與最佳化器中初始化的引數不同時)使用引數名稱,應實現自定義的register_load_state_dict_pre_hook來相應地調整載入的字典。如果載入的狀態字典param_groups中存在param_names,則它們將被儲存並覆蓋最佳化器狀態中當前存在的名稱。如果它們不存在於載入的狀態字典中,最佳化器的param_names將保持不變。示例
>>> model = torch.nn.Linear(10, 10) >>> optim = torch.optim.SGD(model.parameters(), lr=3e-4) >>> scheduler1 = torch.optim.lr_scheduler.LinearLR( ... optim, ... start_factor=0.1, ... end_factor=1, ... total_iters=20, ... ) >>> scheduler2 = torch.optim.lr_scheduler.CosineAnnealingLR( ... optim, ... T_max=80, ... eta_min=3e-5, ... ) >>> lr = torch.optim.lr_scheduler.SequentialLR( ... optim, ... schedulers=[scheduler1, scheduler2], ... milestones=[20], ... ) >>> lr.load_state_dict(torch.load("./save_seq.pt")) >>> # now load the optimizer checkpoint after loading the LRScheduler >>> optim.load_state_dict(torch.load("./save_optim.pt"))
- register_load_state_dict_post_hook(hook, prepend=False)[source]#
註冊一個 load_state_dict 後置鉤子,它將在呼叫
load_state_dict()後被呼叫。它應該具有以下簽名:hook(optimizer) -> None
引數
optimizer是正在使用的最佳化器例項。呼叫
load_state_dict到self上後,鉤子將使用引數self呼叫。註冊的鉤子可用於在load_state_dict載入了state_dict後執行後處理。- 引數
hook (Callable) – 使用者定義的待註冊鉤子。
prepend (bool) – 如果為 True,則提供的後置
hook將在load_state_dict上所有已註冊的後置鉤子之前執行。否則,提供的hook將在所有已註冊的後置鉤子之後執行。(預設: False)
- 返回
一個控制代碼,可用於透過呼叫
handle.remove()來移除新增的鉤子- 返回型別
torch.utils.hooks.RemoveableHandle
- register_load_state_dict_pre_hook(hook, prepend=False)[source]#
註冊一個 load_state_dict 前置鉤子,它將在呼叫
load_state_dict()之前被呼叫。它應該具有以下簽名:hook(optimizer, state_dict) -> state_dict or None
引數
optimizer是正在使用的最佳化器例項,引數state_dict是使用者傳遞給load_state_dict的state_dict的淺複製。鉤子可以就地修改 state_dict,或者選擇性地返回一個新的。如果返回了 state_dict,它將被用於載入到最佳化器中。鉤子將使用引數
self和state_dict呼叫,在呼叫load_state_dict到self上之前。註冊的鉤子可用於在呼叫load_state_dict之前執行預處理。- 引數
hook (Callable) – 使用者定義的待註冊鉤子。
prepend (bool) – 如果為 True,則提供的預置
hook將在load_state_dict上所有已註冊的預置鉤子之前執行。否則,提供的hook將在所有已註冊的預置鉤子之後執行。(預設: False)
- 返回
一個控制代碼,可用於透過呼叫
handle.remove()來移除新增的鉤子- 返回型別
torch.utils.hooks.RemoveableHandle
- register_state_dict_post_hook(hook, prepend=False)[source]#
註冊一個 state_dict 後置鉤子,它將在呼叫
state_dict()後被呼叫。它應具有以下簽名
hook(optimizer, state_dict) -> state_dict or None
鉤子將使用引數
self和state_dict呼叫,在self上生成state_dict後。鉤子可以就地修改 state_dict,或者選擇性地返回一個新的。註冊的鉤子可用於在返回state_dict之前對其進行後處理。- 引數
hook (Callable) – 使用者定義的待註冊鉤子。
prepend (bool) – 如果為 True,則提供的後置
hook將在state_dict上所有已註冊的後置鉤子之前執行。否則,提供的hook將在所有已註冊的後置鉤子之後執行。(預設: False)
- 返回
一個控制代碼,可用於透過呼叫
handle.remove()來移除新增的鉤子- 返回型別
torch.utils.hooks.RemoveableHandle
- register_state_dict_pre_hook(hook, prepend=False)[source]#
註冊一個 state_dict 前置鉤子,它將在呼叫
state_dict()之前被呼叫。它應具有以下簽名
hook(optimizer) -> None
引數
optimizer是正在使用的最佳化器例項。鉤子將使用引數self呼叫,在呼叫state_dict到self上之前。註冊的鉤子可用於在呼叫state_dict之前執行預處理。- 引數
hook (Callable) – 使用者定義的待註冊鉤子。
prepend (bool) – 如果為 True,則提供的預置
hook將在state_dict上所有已註冊的預置鉤子之前執行。否則,提供的hook將在所有已註冊的預置鉤子之後執行。(預設: False)
- 返回
一個控制代碼,可用於透過呼叫
handle.remove()來移除新增的鉤子- 返回型別
torch.utils.hooks.RemoveableHandle
- register_step_post_hook(hook)[source]#
註冊一個最佳化器步驟後鉤子,它將在最佳化器步驟之後被呼叫。
它應具有以下簽名
hook(optimizer, args, kwargs) -> None
引數
optimizer是正在使用的最佳化器例項。- 引數
hook (Callable) – 使用者定義的待註冊鉤子。
- 返回
一個控制代碼,可用於透過呼叫
handle.remove()來移除新增的鉤子- 返回型別
torch.utils.hooks.RemovableHandle
- register_step_pre_hook(hook)[source]#
註冊一個最佳化器步驟預鉤子,它將在最佳化器步驟之前被呼叫。
它應具有以下簽名
hook(optimizer, args, kwargs) -> None or modified args and kwargs
引數
optimizer是正在使用的最佳化器例項。如果 args 和 kwargs 被前置鉤子修改,則轉換後的值將作為包含 new_args 和 new_kwargs 的元組返回。- 引數
hook (Callable) – 使用者定義的待註冊鉤子。
- 返回
一個控制代碼,可用於透過呼叫
handle.remove()來移除新增的鉤子- 返回型別
torch.utils.hooks.RemovableHandle
- state_dict()[source]#
將最佳化器的狀態作為
dict返回。它包含兩個條目
state:一個包含當前最佳化狀態的 Dict。其內容在不同的最佳化器類中會有所不同,但有一些共同的特點。例如,狀態是按引數儲存的,而引數本身不儲存。
state是一個對映引數 ID 到一個包含每個引數對應狀態的 Dict 的字典。
param_groups:一個包含所有引數組的 List,其中每個引數組是一個 Dict。每個引數組包含最佳化器特有的元資料,例如學習率和權重衰減,以及組中引數的 ID 列表。如果引數組使用
named_parameters()初始化,則名稱內容也會儲存在狀態字典中。
注意:引數 ID 可能看起來像索引,但它們只是將狀態與 param_group 關聯的 ID。從 state_dict 載入時,最佳化器會按順序匹配 param_group 的
params(int ID)和最佳化器的param_groups(實際的nn.Parameter),以匹配狀態,而無需額外驗證。返回的狀態字典可能看起來像
{ 'state': { 0: {'momentum_buffer': tensor(...), ...}, 1: {'momentum_buffer': tensor(...), ...}, 2: {'momentum_buffer': tensor(...), ...}, 3: {'momentum_buffer': tensor(...), ...} }, 'param_groups': [ { 'lr': 0.01, 'weight_decay': 0, ... 'params': [0] 'param_names' ['param0'] (optional) }, { 'lr': 0.001, 'weight_decay': 0.5, ... 'params': [1, 2, 3] 'param_names': ['param1', 'layer.weight', 'layer.bias'] (optional) } ] }
- zero_grad(set_to_none=True)[source]#
重置所有最佳化過的
torch.Tensor的梯度。- 引數
set_to_none (bool, optional) –
將梯度設定為 None,而不是設定為零。預設值:
True這通常會降低記憶體佔用,並能適度提高效能。但是,它會改變某些行為。例如:
當用戶嘗試訪問梯度並對其進行手動運算時,None 屬性或全零的 Tensor 會產生不同的行為。
如果使用者請求
zero_grad(set_to_none=True)然後執行 backward,對於未收到梯度的引數,其.grad保證為 None。torch.optim最佳化器在梯度為 0 或 None 時行為不同(一種情況是以 0 梯度執行步長,另一種情況是跳過該步長)。