Skip to content

Commit

Permalink
add ability to interpolate current model with ema model, to further e…
Browse files Browse the repository at this point in the history
…xplore some ideas set forth from hare/tortoise paper
  • Loading branch information
lucidrains committed Oct 4, 2024
1 parent bbff51d commit 8bdba16
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,14 @@ synthesized_ema_output = synthesized_ema(data)
url = {https://api.semanticscholar.org/CorpusID:265659032}
}
```

```bibtex
@article{Lee2024SlowAS,
title = {Slow and Steady Wins the Race: Maintaining Plasticity with Hare and Tortoise Networks},
author = {Hojoon Lee and Hyeonseo Cho and Hyunseung Kim and Donghu Kim and Dugki Min and Jaegul Choo and Clare Lyle},
journal = {ArXiv},
year = {2024},
volume = {abs/2406.02596},
url = {https://api.semanticscholar.org/CorpusID:270258586}
}
```
11 changes: 9 additions & 2 deletions ema_pytorch/ema_pytorch.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,12 @@ def copy_params_from_ema_to_model(self):
for (_, ma_buffers), (_, current_buffers) in zip(self.get_buffers_iter(self.ema_model), self.get_buffers_iter(self.model)):
copy(current_buffers.data, ma_buffers.data)

def update_model_from_ema(self, decay):
if decay == 0.:
return self.copy_params_from_ema_to_model()

self.update_moving_average(self.model, self.ema_model, decay)

def get_current_decay(self):
epoch = (self.step - self.update_after_step - 1).clamp(min = 0.)
value = 1 - (1 + epoch / self.inv_gamma) ** - self.power
Expand Down Expand Up @@ -247,7 +253,7 @@ def update(self):
self.update_moving_average(self.ema_model, self.model)

@torch.no_grad()
def update_moving_average(self, ma_model, current_model):
def update_moving_average(self, ma_model, current_model, current_decay = None):
if self.is_frozen:
return

Expand All @@ -258,7 +264,8 @@ def update_moving_average(self, ma_model, current_model):

# get current decay

current_decay = self.get_current_decay()
if not exists(current_decay):
current_decay = self.get_current_decay()

# store all source and target tensors to copy or lerp

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
setup(
name = 'ema-pytorch',
packages = find_packages(exclude=[]),
version = '0.6.4',
version = '0.6.5',
license='MIT',
description = 'Easy way to keep track of exponential moving average version of your pytorch module',
author = 'Phil Wang',
Expand Down

0 comments on commit 8bdba16

Please sign in to comment.