Args: parameters (Iterable[torch.nn.Parameter]): The parameters to track. decay (float): The decay factor for the exponential moving average. min_decay (float): The minimum decay factor for the exponential moving average. update_after_step (in
(
self,
parameters: Iterable[torch.nn.Parameter],
decay: float = 0.9999,
min_decay: float = 0.0,
update_after_step: int = 0,
use_ema_warmup: bool = False,
inv_gamma: Union[float, int] = 1.0,
power: Union[float, int] = 2 / 3,
model_cls: Optional[Any] = None,
model_config: Dict[str, Any] = None,
**kwargs,
)
| 172 | """ |
| 173 | |
| 174 | def __init__( |
| 175 | self, |
| 176 | parameters: Iterable[torch.nn.Parameter], |
| 177 | decay: float = 0.9999, |
| 178 | min_decay: float = 0.0, |
| 179 | update_after_step: int = 0, |
| 180 | use_ema_warmup: bool = False, |
| 181 | inv_gamma: Union[float, int] = 1.0, |
| 182 | power: Union[float, int] = 2 / 3, |
| 183 | model_cls: Optional[Any] = None, |
| 184 | model_config: Dict[str, Any] = None, |
| 185 | **kwargs, |
| 186 | ): |
| 187 | """ |
| 188 | Args: |
| 189 | parameters (Iterable[torch.nn.Parameter]): The parameters to track. |
| 190 | decay (float): The decay factor for the exponential moving average. |
| 191 | min_decay (float): The minimum decay factor for the exponential moving average. |
| 192 | update_after_step (int): The number of steps to wait before starting to update the EMA weights. |
| 193 | use_ema_warmup (bool): Whether to use EMA warmup. |
| 194 | inv_gamma (float): |
| 195 | Inverse multiplicative factor of EMA warmup. Default: 1. Only used if `use_ema_warmup` is True. |
| 196 | power (float): Exponential factor of EMA warmup. Default: 2/3. Only used if `use_ema_warmup` is True. |
| 197 | device (Optional[Union[str, torch.device]]): The device to store the EMA weights on. If None, the EMA |
| 198 | weights will be stored on CPU. |
| 199 | |
| 200 | @crowsonkb's notes on EMA Warmup: |
| 201 | If gamma=1 and power=1, implements a simple average. gamma=1, power=2/3 are good values for models you plan |
| 202 | to train for a million or more steps (reaches decay factor 0.999 at 31.6K steps, 0.9999 at 1M steps), |
| 203 | gamma=1, power=3/4 for models you plan to train for less (reaches decay factor 0.999 at 10K steps, 0.9999 |
| 204 | at 215.4k steps). |
| 205 | """ |
| 206 | |
| 207 | if isinstance(parameters, torch.nn.Module): |
| 208 | deprecation_message = ( |
| 209 | "Passing a `torch.nn.Module` to `ExponentialMovingAverage` is deprecated. " |
| 210 | "Please pass the parameters of the module instead." |
| 211 | ) |
| 212 | deprecate( |
| 213 | "passing a `torch.nn.Module` to `ExponentialMovingAverage`", |
| 214 | "1.0.0", |
| 215 | deprecation_message, |
| 216 | standard_warn=False, |
| 217 | ) |
| 218 | parameters = parameters.parameters() |
| 219 | |
| 220 | # set use_ema_warmup to True if a torch.nn.Module is passed for backwards compatibility |
| 221 | use_ema_warmup = True |
| 222 | |
| 223 | if kwargs.get("max_value", None) is not None: |
| 224 | deprecation_message = "The `max_value` argument is deprecated. Please use `decay` instead." |
| 225 | deprecate("max_value", "1.0.0", deprecation_message, standard_warn=False) |
| 226 | decay = kwargs["max_value"] |
| 227 | |
| 228 | if kwargs.get("min_value", None) is not None: |
| 229 | deprecation_message = "The `min_value` argument is deprecated. Please use `min_decay` instead." |
| 230 | deprecate("min_value", "1.0.0", deprecation_message, standard_warn=False) |
| 231 | min_decay = kwargs["min_value"] |