Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
M
mu-map
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
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Tamino Huxohl
mu-map
Commits
e0e94b14
Commit
e0e94b14
authored
2 years ago
by
Tamino Huxohl
Browse files
Options
Downloads
Patches
Plain Diff
make use of the weighted loss function in default training and log floats in more detail
parent
de30c17e
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
mu_map/training/default.py
+16
-12
16 additions, 12 deletions
mu_map/training/default.py
with
16 additions
and
12 deletions
mu_map/training/default.py
+
16
−
12
View file @
e0e94b14
...
...
@@ -4,7 +4,7 @@ from typing import Dict
import
torch
from
mu_map.logging
import
get_logger
from
mu_map.training.loss
import
GradientDifference
Loss
from
mu_map.training.loss
import
Weighted
Loss
class
Training
:
...
...
@@ -14,6 +14,7 @@ class Training:
data_loaders
:
Dict
[
str
,
torch
.
utils
.
data
.
DataLoader
],
epochs
:
int
,
device
:
torch
.
device
,
loss_func
:
WeightedLoss
,
lr
:
float
,
lr_decay_factor
:
float
,
lr_decay_epoch
:
int
,
...
...
@@ -39,13 +40,7 @@ class Training:
self
.
lr_scheduler
=
torch
.
optim
.
lr_scheduler
.
StepLR
(
self
.
optimizer
,
step_size
=
self
.
lr_decay_epoch
,
gamma
=
self
.
lr_decay_factor
)
# self.loss_func = torch.nn.MSELoss(reduction="mean")
# self.loss_func = torch.nn.L1Loss(reduction="mean")
_loss1
=
torch
.
nn
.
MSELoss
()
_loss2
=
GradientDifferenceLoss
()
def
_loss_func
(
outputs
,
targets
):
return
_loss1
(
outputs
,
targets
)
+
_loss2
(
outputs
,
targets
)
self
.
loss_func
=
_loss_func
self
.
loss_func
=
loss_func
def
run
(
self
):
for
epoch
in
range
(
1
,
self
.
epochs
+
1
):
...
...
@@ -56,20 +51,19 @@ class Training:
loss_training
=
self
.
_run_epoch
(
self
.
data_loaders
[
"
train
"
],
phase
=
"
val
"
)
logger
.
info
(
f
"
Epoch
{
str
(
epoch
)
:
>
{
len
(
str
(
self
.
epochs
))
}}
/
{
self
.
epochs
}
- Loss
TRAIN
:
{
loss_training
:
.
4
f
}
"
f
"
Epoch
{
str
(
epoch
)
:
>
{
len
(
str
(
self
.
epochs
))
}}
/
{
self
.
epochs
}
- Loss
train
:
{
loss_training
:
.
6
f
}
"
)
loss_validation
=
self
.
_run_epoch
(
self
.
data_loaders
[
"
validation
"
],
phase
=
"
val
"
)
logger
.
info
(
f
"
Epoch
{
str
(
epoch
)
:
>
{
len
(
str
(
self
.
epochs
))
}}
/
{
self
.
epochs
}
- Loss
VAL
:
{
loss_validation
:
.
4
f
}
"
f
"
Epoch
{
str
(
epoch
)
:
>
{
len
(
str
(
self
.
epochs
))
}}
/
{
self
.
epochs
}
- Loss
validation
:
{
loss_validation
:
.
6
f
}
"
)
# ToDo: log outputs and time
_previous
=
self
.
lr_scheduler
.
get_last_lr
()[
0
]
self
.
lr_scheduler
.
step
()
logger
.
debug
(
f
"
Update learning rate from
{
_previous
:
.
4
f
}
to
{
self
.
lr_scheduler
.
get_last_lr
()[
0
]
:
.
4
f
}
"
f
"
Update learning rate from
{
_previous
:
.
6
f
}
to
{
self
.
lr_scheduler
.
get_last_lr
()[
0
]
:
.
6
f
}
"
)
if
epoch
%
self
.
snapshot_epoch
==
0
:
...
...
@@ -212,6 +206,12 @@ if __name__ == "__main__":
default
=
"
cuda:0
"
if
torch
.
cuda
.
is_available
()
else
"
cpu
"
,
help
=
"
the device (cpu or gpu) with which the training is performed
"
,
)
parser
.
add_argument
(
"
--loss_func
"
,
type
=
str
,
default
=
"
l1
"
,
help
=
"
define the loss function used for training, e.g. 0.75*l1+0.25*gdl
"
,
)
parser
.
add_argument
(
"
--lr
"
,
type
=
float
,
default
=
0.001
,
help
=
"
the initial learning rate for training
"
)
...
...
@@ -303,11 +303,15 @@ if __name__ == "__main__":
)
data_loaders
[
split
]
=
data_loader
criterion
=
WeightedLoss
.
from_str
(
args
.
loss_func
)
logger
.
debug
(
f
"
Criterion:
{
criterion
}
"
)
training
=
Training
(
model
=
model
,
data_loaders
=
data_loaders
,
epochs
=
args
.
epochs
,
device
=
device
,
loss_func
=
criterion
,
lr
=
args
.
lr
,
lr_decay_factor
=
args
.
lr_decay_factor
,
lr_decay_epoch
=
args
.
lr_decay_epoch
,
...
...
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