|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
| 3 | +import math |
3 | 4 | from typing import Any, Generic, Literal, Sequence |
4 | 5 |
|
5 | 6 | import numpy as np |
6 | 7 | import torch |
7 | 8 | import torch.nn as nn |
| 9 | +import torch.nn.functional as F |
8 | 10 |
|
9 | 11 | from ...types import TX, TY, TEstimator |
10 | 12 | from .reshaper import SkorchCNNReshaper, SkorchReshaper |
@@ -64,6 +66,78 @@ def forward(self, y_pred: torch.Tensor, y_true: torch.Tensor) -> torch.Tensor: |
64 | 66 | return torch.abs(y_true - y_pred).pow(self.n) |
65 | 67 |
|
66 | 68 |
|
| 69 | +class LogCoshErrors(nn.Module): |
| 70 | + """Log cosh errors. |
| 71 | + Loss = log(cosh(errors + eps)) |
| 72 | +
|
| 73 | + See also |
| 74 | + -------- |
| 75 | + https://datascience.stackexchange.com/questions/96271/logcoshloss-on-pytorch |
| 76 | + """ |
| 77 | + |
| 78 | + def __init__(self, *, softplus: bool = True, eps: float | None = None) -> None: |
| 79 | + """Returns log(cosh(errors)). |
| 80 | +
|
| 81 | + Parameters |
| 82 | + ---------- |
| 83 | + softplus : bool, optional |
| 84 | + If True, uses softplus to get stable results, by default True""" |
| 85 | + super().__init__() |
| 86 | + self.softplus = softplus |
| 87 | + self.eps = eps |
| 88 | + |
| 89 | + def forward(self, y_pred: torch.Tensor, y_true: torch.Tensor) -> torch.Tensor: |
| 90 | + x = y_pred - y_true |
| 91 | + if self.softplus: |
| 92 | + return x + F.softplus(-2.0 * x) - math.log(2.0) |
| 93 | + else: |
| 94 | + eps = self.eps or torch.finfo(x.dtype).eps |
| 95 | + return torch.log(torch.cosh(x + eps)) |
| 96 | + |
| 97 | + |
| 98 | +class XTanhErrors(nn.Module): |
| 99 | + """XTanh errors. |
| 100 | + Loss = x * tanh(x) |
| 101 | +
|
| 102 | + See also |
| 103 | + -------- |
| 104 | + https://github.com/tuantle/regression-losses-pytorch |
| 105 | + """ |
| 106 | + |
| 107 | + def forward(self, y_pred: torch.Tensor, y_true: torch.Tensor) -> torch.Tensor: |
| 108 | + x = y_pred - y_true |
| 109 | + return x * torch.tanh(x) |
| 110 | + |
| 111 | + |
| 112 | +class XSigmoidErrors(nn.Module): |
| 113 | + """XSigmoid errors. |
| 114 | + Loss = x * (2 * sigmoid(x) - 1) |
| 115 | +
|
| 116 | + See also |
| 117 | + -------- |
| 118 | + https://github.com/tuantle/regression-losses-pytorch |
| 119 | + """ |
| 120 | + |
| 121 | + def forward(self, y_pred: torch.Tensor, y_true: torch.Tensor) -> torch.Tensor: |
| 122 | + x = y_pred - y_true |
| 123 | + return x * (2 * torch.sigmoid(x) - 1) |
| 124 | + |
| 125 | + |
| 126 | +class AlgebraicErrors(nn.Module): |
| 127 | + """Algebraic errors. |
| 128 | + Loss = x^2 / sqrt(1 + x^2) |
| 129 | +
|
| 130 | + See also |
| 131 | + -------- |
| 132 | + https://github.com/tuantle/regression-losses-pytorch |
| 133 | + """ |
| 134 | + |
| 135 | + def forward(self, y_pred: torch.Tensor, y_true: torch.Tensor) -> torch.Tensor: |
| 136 | + x = y_pred - y_true |
| 137 | + x2 = torch.pow(x, 2) |
| 138 | + return x2 / torch.sqrt(1 + x2) |
| 139 | + |
| 140 | + |
67 | 141 | class AsymmetricLoss(nn.Module): |
68 | 142 | def __init__( |
69 | 143 | self, |
|
0 commit comments