Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
Climax
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor 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
earth_observation_public
Climax
Commits
d8fedcf3
Commit
d8fedcf3
authored
3 years ago
by
Frisinghelli Daniel
Browse files
Options
Downloads
Patches
Plain Diff
Stable implementation of Bernoulli-Weibull loss.
parent
135f805f
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
climax/core/loss.py
+19
-13
19 additions, 13 deletions
climax/core/loss.py
with
19 additions
and
13 deletions
climax/core/loss.py
+
19
−
13
View file @
d8fedcf3
...
...
@@ -58,8 +58,8 @@ class BernoulliGammaLoss(NaNLoss):
def
forward
(
self
,
y_pred
,
y_true
):
# convert to float32
y_pred
=
y_pred
.
type
(
torch
.
float
32
)
y_true
=
y_true
.
type
(
torch
.
float
32
).
squeeze
()
y_pred
=
y_pred
.
type
(
torch
.
float
64
)
y_true
=
y_true
.
type
(
torch
.
float
64
).
squeeze
()
# missing values
mask
=
~
torch
.
isnan
(
y_true
)
...
...
@@ -109,8 +109,8 @@ class BernoulliGenParetoLoss(NaNLoss):
def
forward
(
self
,
y_pred
,
y_true
):
# convert to float32
y_pred
=
y_pred
.
type
(
torch
.
float
32
)
y_true
=
y_true
.
type
(
torch
.
float
32
).
squeeze
()
y_pred
=
y_pred
.
type
(
torch
.
float
64
)
y_true
=
y_true
.
type
(
torch
.
float
64
).
squeeze
()
# missing values
mask
=
~
torch
.
isnan
(
y_true
)
...
...
@@ -158,8 +158,8 @@ class BernoulliWeibullLoss(NaNLoss):
def
forward
(
self
,
y_pred
,
y_true
):
# convert to float32
y_pred
=
y_pred
.
type
(
torch
.
float
32
)
y_true
=
y_true
.
type
(
torch
.
float
32
).
squeeze
()
y_pred
=
y_pred
.
type
(
torch
.
float
64
)
y_true
=
y_true
.
type
(
torch
.
float
64
).
squeeze
()
# missing values
mask
=
~
torch
.
isnan
(
y_true
)
...
...
@@ -181,24 +181,30 @@ class BernoulliWeibullLoss(NaNLoss):
# clip shape and scale to (0, 50)
# NOTE: in general shape, scale in (0, +infinity), but clipping to
# finite numbers is required for numerical stability
gshape
=
torch
.
clamp
(
torch
.
exp
(
y_pred
[:,
1
,
...].
squeeze
()[
mask
]),
max
=
50
)
gscale
=
torch
.
clamp
(
torch
.
exp
(
y_pred
[:,
2
,
...].
squeeze
()[
mask
]),
max
=
50
)
# gshape = torch.clamp(
# torch.exp(y_pred[:, 1, ...].squeeze()[mask]), max=1)
# gscale = torch.clamp(
# torch.exp(y_pred[:, 2, ...].squeeze()[mask]), max=20)
gshape
=
torch
.
sigmoid
(
y_pred
[:,
1
,
...].
squeeze
()[
mask
])
gscale
=
torch
.
exp
(
y_pred
[:,
2
,
...].
squeeze
()[
mask
])
# negative log-likelihood function of Bernoulli-
GenPareto
distribution
# negative log-likelihood function of Bernoulli-
Weibull
distribution
# Bernoulli contribution
loss
=
-
(
1
-
p_true
)
*
torch
.
log
(
1
-
p_pred
+
self
.
epsilon
)
# replace values < min_amount: ensures numerical stability in backprop
y_true
[
y_true
<=
self
.
min_amount
]
=
1
# Weibull contribution
loss
-=
p_true
*
(
torch
.
log
(
p_pred
+
self
.
epsilon
)
+
torch
.
log
(
gshape
+
self
.
epsilon
)
-
gshape
*
torch
.
log
(
gscale
+
self
.
epsilon
)
+
(
gshape
-
1
)
*
torch
.
log
(
y_true
+
self
.
epsilon
)
-
(
y_true
/
(
gscale
+
self
.
epsilon
))
**
gshape
)
torch
.
pow
(
y_true
/
(
gscale
+
self
.
epsilon
),
gshape
)
)
# clip loss to finite values to ensure numerical stability
loss
=
torch
.
clamp
(
loss
,
max
=
100
)
#
loss = torch.clamp(loss, max=100)
return
self
.
reduce
(
loss
)
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