Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Distributions #48

Merged
merged 6 commits into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/run_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ on:
branches:
- main
- development
paths:
- "tutorials/**"
- "dlordinal/**"
- ".github/workflows/**"

jobs:
tests:
Expand Down
20 changes: 10 additions & 10 deletions dlordinal/distributions/__init__.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
from .beta_distribution import get_beta_probabilities
from .binomial_distribution import get_binomial_probabilities
from .exponential_distribution import get_exponential_probabilities
from .beta_distribution import get_beta_softlabels
from .binomial_distribution import get_binomial_softlabels
from .exponential_distribution import get_exponential_softlabels
from .general_triangular_distribution import (
get_general_triangular_params,
get_general_triangular_probabilities,
get_general_triangular_softlabels,
)
from .poisson_distribution import get_poisson_probabilities
from .triangular_distribution import get_triangular_probabilities
from .triangular_distribution import get_triangular_softlabels

__all__ = [
"get_beta_probabilities",
"get_exponential_probabilities",
"get_binomial_probabilities",
"get_beta_softlabels",
"get_exponential_softlabels",
"get_binomial_softlabels",
"get_poisson_probabilities",
"get_triangular_probabilities",
"get_triangular_softlabels",
"get_general_triangular_params",
"get_general_triangular_probabilities",
"get_general_triangular_softlabels",
]
2 changes: 1 addition & 1 deletion dlordinal/distributions/beta_distribution.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def beta_dist(x, p, q, a=1.0):
return (x ** (a * p)) / (p * beta_func(p, q)) * hyp2f1(p, 1 - q, p + 1, x**a)


def get_beta_probabilities(J, p, q, a=1.0):
def get_beta_softlabels(J, p, q, a=1.0):
"""Get probabilities from a beta distribution :math:`B(p,q,a)` for ``J`` splits.
The :math:`[0,1]` interval is split into ``J`` intervals and the probability for
each interval is computed as the difference between the value of the distribution
Expand Down
2 changes: 1 addition & 1 deletion dlordinal/distributions/binomial_distribution.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from scipy.stats import binom


def get_binomial_probabilities(J):
def get_binomial_softlabels(J):
"""Get probabilities for the binomial distribution for ``J`` classes or splits
using the approach described in :footcite:t:`liu2020unimodal`.
The :math:`[0,1]` interval is split into ``J`` intervals and the probability for
Expand Down
2 changes: 1 addition & 1 deletion dlordinal/distributions/exponential_distribution.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from scipy.special import softmax


def get_exponential_probabilities(J, p=1.0, tau=1.0):
def get_exponential_softlabels(J, p=1.0, tau=1.0):
"""Get probabilities from exponential distribution for ``J`` classes or splits as
described in :footcite:t:`liu2020unimodal` and :footcite:t:`vargas2023exponential`.
The :math:`[0,1]` interval is split into ``J`` intervals and the probability for
Expand Down
6 changes: 3 additions & 3 deletions dlordinal/distributions/general_triangular_distribution.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ def abcj(J, j, alpha2j_1, alpha2j):
return params


def get_general_triangular_probabilities(J: int, alphas: np.ndarray, verbose: int = 0):
def get_general_triangular_softlabels(J: int, alphas: np.ndarray, verbose: int = 0):
"""Get probabilities from triangular distributions for ``J`` classes or splits.
The :math:`[0,1]` interval is split into ``J`` intervals and the probability for
each interval is computed as the difference between the value of the triangular
Expand Down Expand Up @@ -213,8 +213,8 @@ def get_general_triangular_probabilities(J: int, alphas: np.ndarray, verbose: in

if not isinstance(alphas, np.ndarray) or alphas.shape != (2 * J,):
raise ValueError(
f"alphas must be a numpy array or list of shape (2 * n,),"
f" but got {{alphas.shape}}"
"alphas must be a numpy array or list of shape (2 * n,),"
" but got {alphas.shape}"
)

intervals = get_intervals(J)
Expand Down
6 changes: 3 additions & 3 deletions dlordinal/distributions/tests/test_beta_distribution.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import numpy as np
import pytest

from ..beta_distribution import beta_dist, beta_func, get_beta_probabilities
from ..beta_distribution import beta_dist, beta_func, get_beta_softlabels


def test_beta_inc():
Expand Down Expand Up @@ -57,7 +57,7 @@ def test_beta_probabilities():
p = 2.0
q = 3.0
a = 1.0
result = get_beta_probabilities(n, p, q, a)
result = get_beta_softlabels(n, p, q, a)
expected_result = [
0.1808000009216,
0.34399999942400017,
Expand All @@ -74,7 +74,7 @@ def test_beta_probabilities():
p = 1.5
q = 2.5
a = 2.0
result = get_beta_probabilities(n, p, q, a)
result = get_beta_softlabels(n, p, q, a)
expected_result = [
0.05010107325697135,
0.283232260076362,
Expand Down
4 changes: 2 additions & 2 deletions dlordinal/distributions/tests/test_binomial_distribution.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import numpy as np
import pytest

from ..binomial_distribution import get_binomial_probabilities
from ..binomial_distribution import get_binomial_softlabels


def test_get_binomial_probabilities():
# Case 1: n = 5
n = 5
result = get_binomial_probabilities(n)
result = get_binomial_softlabels(n)
expected_result = np.array(
[
[6.561e-01, 2.916e-01, 4.860e-02, 3.600e-03, 1.000e-04],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import numpy as np
import pytest

from ..exponential_distribution import get_exponential_probabilities
from ..exponential_distribution import get_exponential_softlabels


def test_get_exponential_probabilities():
n = 5
p = 1.0
tau = 1.0
result = get_exponential_probabilities(n, p, tau)
result = get_exponential_softlabels(n, p, tau)
expected_result = np.array(
[
[0.63640865, 0.23412166, 0.08612854, 0.03168492, 0.01165623],
Expand All @@ -35,7 +35,7 @@ def test_exponential_probabilities():
n = 4
p = 2.0
tau = 1.0
result = get_exponential_probabilities(n, p, tau)
result = get_exponential_softlabels(n, p, tau)
expected_result = np.array(
[
[7.21334965e-01, 2.65364304e-01, 1.32117107e-02, 8.90198068e-05],
Expand Down
8 changes: 4 additions & 4 deletions dlordinal/distributions/tests/test_triangular_distribution.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import numpy as np
import pytest

from ..triangular_distribution import get_triangular_probabilities
from ..triangular_distribution import get_triangular_softlabels


def test_get_triangular_probabilities():
Expand All @@ -10,7 +10,7 @@ def test_get_triangular_probabilities():
alpha2 = 0.01
verbose = 0

result = get_triangular_probabilities(n, alpha2, verbose)
result = get_triangular_softlabels(n, alpha2, verbose)

expected_result = [
[0.98845494, 0.01154505, 0.0, 0.0, 0.0],
Expand Down Expand Up @@ -39,7 +39,7 @@ def test_get_triangular_probabilities():
alpha2 = 0.01
verbose = 0

result = get_triangular_probabilities(n, alpha2, verbose)
result = get_triangular_softlabels(n, alpha2, verbose)

expected_result = [
[0.98845494, 0.01154505, 0.0, 0.0, 0.0, 0.0, 0.0],
Expand Down Expand Up @@ -72,7 +72,7 @@ def test_get_triangular_probabilities_verbose():
alpha2 = 0.01
verbose = 4

result = get_triangular_probabilities(n, alpha2, verbose)
result = get_triangular_softlabels(n, alpha2, verbose)

expected_result = [
[
Expand Down
6 changes: 3 additions & 3 deletions dlordinal/distributions/triangular_distribution.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from .utils import get_intervals, triangular_cdf


def get_triangular_probabilities(J: int, alpha2: float = 0.01, verbose: int = 0):
def get_triangular_softlabels(J: int, alpha2: float = 0.01, verbose: int = 0):
"""
Get probabilities from triangular distributions for ``J`` classes or splits using
the approach described in :footcite:t:`vargas2023softlabelling`.
Expand Down Expand Up @@ -46,7 +46,7 @@ def get_triangular_probabilities(J: int, alpha2: float = 0.01, verbose: int = 0)
1 & \\text{if } j = J
\\end{cases}

The value of :math:`\\alpha_1`, that represents the error for the first class,
The value of :math:`\\alpha_1`, that represents the error for the first class,
is computed as follows:

.. math::
Expand All @@ -57,7 +57,7 @@ def get_triangular_probabilities(J: int, alpha2: float = 0.01, verbose: int = 0)
is computed as follows:

.. math::
\\alpha_3 = \\left(\\frac{1 -
\\alpha_3 = \\left(\\frac{1 -
\\sqrt{1 - 4\\left(\\frac{J - 1}{J}\\right)^2(1 - 2\\alpha_2)(\\sqrt{2\\alpha_2}
(-1 + \\sqrt{2\\alpha_2}))}}{2}\\right)^2

Expand Down
4 changes: 2 additions & 2 deletions dlordinal/losses/beta_loss.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import torch
from torch import Tensor

from ..distributions import get_beta_probabilities
from ..distributions import get_beta_softlabels
from .custom_targets_loss import CustomTargetsCrossEntropyLoss

# Params [a,b] for beta distribution
Expand Down Expand Up @@ -177,7 +177,7 @@ def __init__(
# Precompute class probabilities for each label
cls_probs = torch.tensor(
[
get_beta_probabilities(
get_beta_softlabels(
num_classes,
self.params[num_classes][i][0],
self.params[num_classes][i][1],
Expand Down
4 changes: 2 additions & 2 deletions dlordinal/losses/binomial_loss.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import torch
from torch import Tensor

from ..distributions import get_binomial_probabilities
from ..distributions import get_binomial_softlabels
from .custom_targets_loss import CustomTargetsCrossEntropyLoss


Expand Down Expand Up @@ -59,7 +59,7 @@ def __init__(
label_smoothing: float = 0.0,
):
# Precompute class probabilities for each label
cls_probs = torch.tensor(get_binomial_probabilities(num_classes)).float()
cls_probs = torch.tensor(get_binomial_softlabels(num_classes)).float()

super().__init__(
cls_probs=cls_probs,
Expand Down
4 changes: 2 additions & 2 deletions dlordinal/losses/exponential_loss.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import torch
from torch import Tensor

from ..distributions import get_exponential_probabilities
from ..distributions import get_exponential_softlabels
from .custom_targets_loss import CustomTargetsCrossEntropyLoss


Expand Down Expand Up @@ -63,7 +63,7 @@ def __init__(
label_smoothing: float = 0.0,
):
# Precompute class probabilities for each label
cls_probs = torch.tensor(get_exponential_probabilities(num_classes, p)).float()
cls_probs = torch.tensor(get_exponential_softlabels(num_classes, p)).float()

super().__init__(
cls_probs=cls_probs,
Expand Down
4 changes: 2 additions & 2 deletions dlordinal/losses/general_triangular_loss.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import torch
from torch import Tensor

from ..distributions import get_general_triangular_probabilities
from ..distributions import get_general_triangular_softlabels
from .custom_targets_loss import CustomTargetsCrossEntropyLoss


Expand Down Expand Up @@ -63,7 +63,7 @@ def __init__(
label_smoothing: float = 0.0,
):
# Precompute class probabilities for each label
r = get_general_triangular_probabilities(num_classes, alphas, verbose=0)
r = get_general_triangular_softlabels(num_classes, alphas, verbose=0)
cls_probs = torch.tensor(r)

super().__init__(
Expand Down
4 changes: 2 additions & 2 deletions dlordinal/losses/triangular_loss.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import torch
from torch import Tensor

from ..distributions import get_triangular_probabilities
from ..distributions import get_triangular_softlabels
from .custom_targets_loss import CustomTargetsCrossEntropyLoss


Expand Down Expand Up @@ -62,7 +62,7 @@ def __init__(
label_smoothing: float = 0.0,
):
# Precompute class probabilities for each label
cls_probs = torch.tensor(get_triangular_probabilities(num_classes, alpha2))
cls_probs = torch.tensor(get_triangular_softlabels(num_classes, alpha2))
super().__init__(
cls_probs=cls_probs,
eta=eta,
Expand Down
Loading