Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
asv-ml
mlmc
Commits
2b2453e4
Commit
2b2453e4
authored
Nov 05, 2021
by
Janos Borst
Browse files
save
parent
d12b11ba
Pipeline
#50428
passed with stage
in 9 minutes and 58 seconds
Changes
3
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
mlmc/data/dataset_classes.py
View file @
2b2453e4
...
...
@@ -312,7 +312,7 @@ class SingleLabelDataset(MultiLabelDataset):
y
=
y
if
isinstance
(
y
,
str
)
else
y
[
0
]
return
SingleLabelDataset
(
x
=
df
[
x
].
applymap
(
str
).
agg
(
sep
.
join
,
axis
=
1
).
to_list
(),
y
=
[[
str
(
l
)]
for
l
in
df
[
y
].
tolist
()
]
,
y
=
df
[
y
].
tolist
(),
classes
=
classes
if
classes
is
not
None
else
{
cls
:
i
for
i
,
cls
in
enumerate
(
sorted
(
df
[
y
].
map
(
str
).
unique
()))}
)
...
...
mlmc/loss/__init__.py
View file @
2b2453e4
from
.loss_labelwise_ranking
import
RelativeRankingLoss
from
.loss_labelwise_ranking
import
RelativeRankingLoss
,
RelativeRankingLossEncourage
from
.loss_mask_noise_wrapper
import
MaskNoiseWrapper
\ No newline at end of file
mlmc/loss/loss_labelwise_ranking.py
View file @
2b2453e4
...
...
@@ -24,3 +24,53 @@ class RelativeRankingLoss(torch.nn.Module):
l1
=
torch
.
relu
(
-
inputs_rel
[
target_rel
>
0
]
+
self
.
margin
)
l2
=
torch
.
relu
(
inputs_rel
[
target_rel
<
0
]
+
self
.
margin
)
return
l1
.
mean
()
+
l2
.
mean
()
class
RelativeRankingLossAlt
(
torch
.
nn
.
Module
):
"""
This considers only relative similarities, y = 1, x1 should be large than x2.
"""
def
__init__
(
self
,
margin
=
0.5
,
act
=
None
):
"""
Initialize RelativeRankingLoss
:param margin: margin of the loss function
:param act: If necessary an additional activation function before applying the loss.
"""
super
(
RelativeRankingLossAlt
,
self
).
__init__
()
self
.
margin
=
margin
self
.
act
=
act
def
forward
(
self
,
inputs
,
targets
):
if
self
.
act
is
not
None
:
inputs
=
self
.
act
(
inputs
)
if
targets
.
shape
!=
inputs
.
shape
:
targets
=
torch
.
nn
.
functional
.
one_hot
(
targets
,
inputs
.
shape
[
-
1
])
inputs_rel
=
(
inputs
[...,
None
,:]
-
inputs
[...,
None
])
target_rel
=
(
targets
[...,
None
,:]
-
targets
[...,
None
])
l
=
((
-
inputs_rel
*
target_rel
+
self
.
margin
).
relu
()[
target_rel
!=
0
]).
mean
()
return
l
class
RelativeRankingLossEncourage
(
torch
.
nn
.
Module
):
"""
This considers only relative similarities, y = 1, x1 should be large than x2.
"""
def
__init__
(
self
,
margin
=
0.5
,
act
=
None
):
"""
Initialize RelativeRankingLoss
:param margin: margin of the loss function
:param act: If necessary an additional activation function before applying the loss.
"""
super
(
RelativeRankingLossEncourage
,
self
).
__init__
()
self
.
margin
=
margin
self
.
act
=
act
def
forward
(
self
,
inputs
,
targets
):
if
self
.
act
is
not
None
:
inputs
=
self
.
act
(
inputs
)
if
targets
.
shape
!=
inputs
.
shape
:
targets
=
torch
.
nn
.
functional
.
one_hot
(
targets
,
inputs
.
shape
[
-
1
])
inputs_rel
=
(
inputs
[...,
None
,:]
-
inputs
[...,
None
])
target_rel
=
(
targets
[...,
None
,:]
-
targets
[...,
None
])
l
=
(
-
inputs_rel
*
target_rel
).
relu
().
sum
()
/
(
target_rel
!=
0
).
sum
()
margins
=
(
self
.
margin
-
inputs_rel
*
target_rel
).
relu
().
sum
()
/
(
target_rel
!=
0
).
sum
()
return
l
+
margins
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment