Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
I
ICV-mmengine_basecode
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Florian Schiffel
ICV-mmengine_basecode
Commits
f2b0540f
Commit
f2b0540f
authored
2 years ago
by
RangiLyu
Committed by
Zaida Zhou
2 years ago
Browse files
Options
Downloads
Patches
Plain Diff
[Enhance] Raise warning for abnormal momentum (#655)
parent
4a9df3bd
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
mmengine/model/averaged_model.py
+14
-0
14 additions, 0 deletions
mmengine/model/averaged_model.py
tests/test_model/test_averaged_model.py
+7
-0
7 additions, 0 deletions
tests/test_model/test_averaged_model.py
with
21 additions
and
0 deletions
mmengine/model/averaged_model.py
+
14
−
0
View file @
f2b0540f
# Copyright (c) OpenMMLab. All rights reserved.
# Copyright (c) OpenMMLab. All rights reserved.
import
warnings
from
abc
import
abstractmethod
from
abc
import
abstractmethod
from
copy
import
deepcopy
from
copy
import
deepcopy
from
typing
import
Optional
from
typing
import
Optional
...
@@ -151,6 +152,13 @@ class ExponentialMovingAverage(BaseAveragedModel):
...
@@ -151,6 +152,13 @@ class ExponentialMovingAverage(BaseAveragedModel):
Xema_{t+1} = (1 - momentum) * Xema_{t} + momentum * X_t
Xema_{t+1} = (1 - momentum) * Xema_{t} + momentum * X_t
.. note::
This :attr:`momentum` argument is different from one used in optimizer
classes and the conventional notion of momentum. Mathematically,
:math:`Xema_{t+1}` is the moving average and :math:`X_t` is the
new observed value. The value of momentum is usually a small number,
allowing observed values to slowly update the ema parameters.
Args:
Args:
model (nn.Module): The model to be averaged.
model (nn.Module): The model to be averaged.
momentum (float): The momentum used for updating ema parameter.
momentum (float): The momentum used for updating ema parameter.
...
@@ -175,6 +183,12 @@ class ExponentialMovingAverage(BaseAveragedModel):
...
@@ -175,6 +183,12 @@ class ExponentialMovingAverage(BaseAveragedModel):
super
().
__init__
(
model
,
interval
,
device
,
update_buffers
)
super
().
__init__
(
model
,
interval
,
device
,
update_buffers
)
assert
0.0
<
momentum
<
1.0
,
'
momentum must be in range (0.0, 1.0)
'
\
assert
0.0
<
momentum
<
1.0
,
'
momentum must be in range (0.0, 1.0)
'
\
f
'
but got
{
momentum
}
'
f
'
but got
{
momentum
}
'
if
momentum
>
0.5
:
warnings
.
warn
(
'
The value of momentum in EMA is usually a small number,
'
'
which is different from the conventional notion of
'
f
'
momentum but got
{
momentum
}
. Please make sure the
'
f
'
value is correct.
'
)
self
.
momentum
=
momentum
self
.
momentum
=
momentum
def
avg_func
(
self
,
averaged_param
:
Tensor
,
source_param
:
Tensor
,
def
avg_func
(
self
,
averaged_param
:
Tensor
,
source_param
:
Tensor
,
...
...
This diff is collapsed.
Click to expand it.
tests/test_model/test_averaged_model.py
+
7
−
0
View file @
f2b0540f
...
@@ -93,6 +93,13 @@ class TestAveragedModel(TestCase):
...
@@ -93,6 +93,13 @@ class TestAveragedModel(TestCase):
model
=
torch
.
nn
.
Sequential
(
model
=
torch
.
nn
.
Sequential
(
torch
.
nn
.
Conv2d
(
1
,
5
,
kernel_size
=
3
),
torch
.
nn
.
Linear
(
5
,
10
))
torch
.
nn
.
Conv2d
(
1
,
5
,
kernel_size
=
3
),
torch
.
nn
.
Linear
(
5
,
10
))
ExponentialMovingAverage
(
model
,
momentum
=
3
)
ExponentialMovingAverage
(
model
,
momentum
=
3
)
with
self
.
assertWarnsRegex
(
Warning
,
'
The value of momentum in EMA is usually a small number
'
):
model
=
torch
.
nn
.
Sequential
(
torch
.
nn
.
Conv2d
(
1
,
5
,
kernel_size
=
3
),
torch
.
nn
.
Linear
(
5
,
10
))
ExponentialMovingAverage
(
model
,
momentum
=
0.9
)
# test EMA
# test EMA
model
=
torch
.
nn
.
Sequential
(
model
=
torch
.
nn
.
Sequential
(
torch
.
nn
.
Conv2d
(
1
,
5
,
kernel_size
=
3
),
torch
.
nn
.
Linear
(
5
,
10
))
torch
.
nn
.
Conv2d
(
1
,
5
,
kernel_size
=
3
),
torch
.
nn
.
Linear
(
5
,
10
))
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment