Skip to content

Commit

Permalink
Add torch losses to constant
Browse files Browse the repository at this point in the history
  • Loading branch information
v1docq committed Nov 28, 2023
1 parent 2f273cc commit 962ae11
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
25 changes: 16 additions & 9 deletions fedot_ind/core/architecture/settings/constanst_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,17 @@
import torch
from fedot.core.repository.dataset_types import DataTypesEnum
from torch import nn, Tensor

from fedot_ind.core.models.nn.network_modules.losses import *
from fedot_ind.core.models.quantile.stat_features import *
from fedot_ind.core.models.topological.topofeatures import *
from fedot_ind.core.operation.transformation.data.hankel import HankelMatrix
from torch.nn.modules import Module


def beta_thr(beta):
return 0.56 * np.power(beta, 3) - 0.95 * np.power(beta, 2) + 1.82 * beta + 1.43


class SMAPELoss(Module):
def __init__(self):
super().__init__()

def forward(self, input: Tensor, target: Tensor) -> Tensor:
return 100 * torch.mean(2 * torch.abs(input - target) / (torch.abs(target) + torch.abs(input)) + 1e-8)


class ComputationalConstant(Enum):
CPU_NUMBERS = math.ceil(cpu_count() * 0.7) if cpu_count() > 1 else 1

Expand Down Expand Up @@ -145,6 +138,13 @@ class TorchLossesConstant(Enum):
MULTI_CLASS_CROSS_ENTROPY = nn.BCEWithLogitsLoss
MSE = nn.MSELoss
SMAPE = SMAPELoss
TWEEDIE_LOSS = TweedieLoss
FOCAL_LOSS = FocalLoss
CENTER_PLUS_LOSS = CenterPlusLoss
CENTER_LOSS = CenterLoss
MASK_LOSS = MaskedLossWrapper
LOG_COSH_LOSS = LogCoshLoss
HUBER_LOSS = HuberLoss


STAT_METHODS = FeatureConstant.STAT_METHODS.value
Expand Down Expand Up @@ -178,3 +178,10 @@ class TorchLossesConstant(Enum):
MULTI_CLASS_CROSS_ENTROPY = TorchLossesConstant.MULTI_CLASS_CROSS_ENTROPY.value
MSE = TorchLossesConstant.MSE.value
SMAPE = TorchLossesConstant.SMAPE.value
TWEEDIE_LOSS = TorchLossesConstant.TWEEDIE_LOSS.value
FOCAL_LOSS = TorchLossesConstant.FOCAL_LOSS.value
CENTER_PLUS_LOSS = TorchLossesConstant.CENTER_PLUS_LOSS.value
CENTER_LOSS = TorchLossesConstant.CENTER_LOSS.value
MASK_LOSS = TorchLossesConstant.MASK_LOSS.value
LOG_COSH_LOSS = TorchLossesConstant.LOG_COSH_LOSS.value
HUBER_LOSS = TorchLossesConstant.HUBER_LOSS.value
11 changes: 8 additions & 3 deletions fedot_ind/core/models/nn/network_modules/losses.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from fastai.torch_core import Module
import torch.nn.functional as F


class HuberLoss(nn.Module):
"""Huber loss
Expand Down Expand Up @@ -64,7 +63,6 @@ def forward(self, inp, targ):
return self.loss(inp, targ)


# %% ../nbs/016_losses.ipynb 9
class CenterLoss(Module):
r"""
Code in Pytorch has been slightly modified from: https://github.com/KaiyangZhou/pytorch-center-loss/blob/master/center_loss.py
Expand Down Expand Up @@ -142,7 +140,6 @@ def forward(self, x: Tensor, y: Tensor) -> Tensor:
return loss


# %% ../nbs/016_losses.ipynb 14
class TweedieLoss(Module):
def __init__(self, p=1.5, eps=1e-8):
"""
Expand All @@ -163,3 +160,11 @@ def forward(self, inp, targ):
b = torch.exp((2 - self.p) * torch.log(inp)) / (2 - self.p)
loss = -a + b
return loss.mean()


class SMAPELoss(Module):
def __init__(self):
super().__init__()

def forward(self, input: Tensor, target: Tensor) -> Tensor:
return 100 * torch.mean(2 * torch.abs(input - target) / (torch.abs(target) + torch.abs(input)) + 1e-8)

0 comments on commit 962ae11

Please sign in to comment.