Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
decentralizepy
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
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
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
SaCS
decentralizepy
Commits
0d50bc39
Commit
0d50bc39
authored
3 years ago
by
Rishi Sharma
Browse files
Options
Downloads
Patches
Plain Diff
GradientAccumulator migration to steps
parent
3f1ff51f
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/decentralizepy/training/GradientAccumulator.py
+45
-22
45 additions, 22 deletions
src/decentralizepy/training/GradientAccumulator.py
src/decentralizepy/training/Training.py
+8
-7
8 additions, 7 deletions
src/decentralizepy/training/Training.py
with
53 additions
and
29 deletions
src/decentralizepy/training/GradientAccumulator.py
+
45
−
22
View file @
0d50bc39
...
...
@@ -26,35 +26,58 @@ class GradientAccumulator(Training):
"""
super
().
__init__
(
model
,
optimizer
,
loss
,
epochs_per_round
,
batch_size
,
shuffle
)
def
train
(
self
,
data
s
et
):
def
train
step
(
self
,
data
,
targ
et
):
"""
One training iteration with accumulation of gradients in model.accumulated_gradients.
Goes through the entire dataset.
One training step on a minibatch.
Parameters
----------
dataset : decentralizepy.datasets.Dataset
The training dataset. Should implement get_trainset(batch_size, shuffle)
data : any
Data item
target : any
Label
Returns
-------
int
Loss Value for the step
"""
trainset
=
dataset
.
get_trainset
(
self
.
batch_size
,
self
.
shuffle
)
self
.
model
.
accumulated_gradients
=
[]
self
.
model
.
zero_grad
()
output
=
self
.
model
(
data
)
loss_val
=
self
.
loss
(
output
,
target
)
loss_val
.
backward
()
logging
.
debug
(
"
Accumulating Gradients
"
)
self
.
model
.
accumulated_gradients
.
append
(
{
k
:
v
.
grad
.
clone
().
detach
()
for
k
,
v
in
zip
(
self
.
model
.
state_dict
(),
self
.
model
.
parameters
())
}
)
self
.
optimizer
.
step
()
return
loss_val
.
item
()
for
epoch
in
range
(
self
.
epochs_per_round
):
def
train_full
(
self
,
trainset
):
"""
One training iteration, goes through the entire dataset
Parameters
----------
trainset : torch.utils.data.Dataloader
The training dataset.
"""
for
epoch
in
range
(
self
.
rounds
):
epoch_loss
=
0.0
count
=
0
for
data
,
target
in
trainset
:
self
.
model
.
zero_grad
()
output
=
self
.
model
(
data
)
loss_val
=
self
.
loss
(
output
,
target
)
epoch_loss
+=
loss_val
.
item
()
loss_val
.
backward
()
self
.
model
.
accumulated_gradients
.
append
(
{
k
:
v
.
grad
.
clone
().
detach
()
for
k
,
v
in
zip
(
self
.
model
.
state_dict
(),
self
.
model
.
parameters
()
)
}
)
self
.
optimizer
.
step
()
epoch_loss
+=
self
.
trainstep
(
data
,
target
)
count
+=
1
logging
.
info
(
"
Epoch: {} loss: {}
"
.
format
(
epoch
,
epoch_loss
/
count
))
def
train
(
self
,
dataset
):
"""
One training iteration with accumulation of gradients in model.accumulated_gradients.
Goes through the entire dataset.
Parameters
----------
dataset : decentralizepy.datasets.Dataset
The training dataset. Should implement get_trainset(batch_size, shuffle)
"""
self
.
model
.
accumulated_gradients
=
[]
super
().
train
(
dataset
)
This diff is collapsed.
Click to expand it.
src/decentralizepy/training/Training.py
+
8
−
7
View file @
0d50bc39
...
...
@@ -107,7 +107,7 @@ class Training:
trainset : torch.utils.data.Dataloader
The training dataset.
"""
for
epoch
in
range
(
self
.
epochs_per_
round
):
for
epoch
in
range
(
self
.
round
s
):
epoch_loss
=
0.0
count
=
0
for
data
,
target
in
trainset
:
...
...
@@ -130,9 +130,10 @@ class Training:
else
:
iter_loss
=
0.0
count
=
0
for
data
,
target
in
trainset
:
iter_loss
+=
self
.
trainstep
(
data
,
target
)
count
+=
1
logging
.
info
(
"
Round: {} loss: {}
"
.
format
(
count
,
iter_loss
/
count
))
if
count
>=
self
.
rounds
:
break
while
count
<
self
.
rounds
:
for
data
,
target
in
trainset
:
iter_loss
+=
self
.
trainstep
(
data
,
target
)
count
+=
1
logging
.
info
(
"
Round: {} loss: {}
"
.
format
(
count
,
iter_loss
/
count
))
if
count
>=
self
.
rounds
:
break
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