EMA¶
ocean_emulators.utils.ema
¶
Exponential Moving Average (EMA) module.
Copied from https://github.com/CompVis/latent-diffusion/blob/main/ldm/modules/ema.py and modified.
MIT License
Copyright (c) 2022 Machine Vision and Learning Group, LMU Munich
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
EMATracker(model, decay=0.999, faster_decay_at_start=True)
¶
Exponential Moving Average (EMA) tracker.
This tracks the moving average of the parameters of a model, and has methods that can be used to temporarily replace the parameters of the model with its EMA.
Create a new EMA tracker.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
Module
|
The model whose parameters should be tracked. |
required |
decay
|
float
|
The decay rate of the moving average. |
0.999
|
faster_decay_at_start
|
bool
|
Whether to use the number of updates to determine the decay rate. If True, the decay rate will be min(decay, (1 + num_updates) / (10 + num_updates)). If False, the decay rate will be decay. |
True
|
Source code in src/ocean_emulators/utils/ema.py
__call__(model)
¶
Update the moving average of the parameters.
Does not mutate the input, only updates the moving average.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
BaseModel | DistributedDataParallel
|
The model whose parameters should be updated. Should be a model specified identically to the one passed when this object was instantiated. |
required |
Source code in src/ocean_emulators/utils/ema.py
copy_to(model)
¶
Copy the averaged parameters to the model, overwriting its values.
Source code in src/ocean_emulators/utils/ema.py
store(parameters)
¶
Save the current parameters for restoring later.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
parameters
|
Iterable[Parameter]
|
The parameters to be stored for later restoration by |
required |
Source code in src/ocean_emulators/utils/ema.py
restore(parameters)
¶
Restore the parameters stored with the store method.
Useful to validate the model with EMA parameters without affecting the
original optimization process. Store the parameters before the
copy_to method. After validation (or model saving), use this to
restore the former parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
parameters
|
Iterable[Parameter]
|
The parameters to be updated with the values stored by |
required |
Source code in src/ocean_emulators/utils/ema.py
get_state(include_ema_params)
¶
Get the state of the EMA tracker.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
include_ema_params
|
bool
|
Whether to include the EMA parameters in the state. |
required |
Returns:
| Type | Description |
|---|---|
|
The state of the EMA tracker. |
Source code in src/ocean_emulators/utils/ema.py
from_state(state, model)
classmethod
¶
Create an EMA tracker from a state.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
The state of the EMA tracker. |
required | |
model
|
Module
|
The model whose parameters should be tracked, used to initialize the EMA weights if they are not provided in the state. |
required |
Returns:
| Type | Description |
|---|---|
EMATracker
|
The EMA tracker. |