Commit 2b2453e4 authored by Janos Borst's avatar Janos Borst
Browse files

save

parent d12b11ba
Pipeline #50428 passed with stage
in 9 minutes and 58 seconds
......@@ -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()))}
)
......
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
......@@ -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
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment