Skip to content

Commit

Permalink
Merge pull request #36 from ayrna/development
Browse files Browse the repository at this point in the history
Pip correction
  • Loading branch information
franberchez authored Jan 18, 2024
2 parents faf77f5 + 90d2204 commit 8064fbd
Show file tree
Hide file tree
Showing 14 changed files with 773 additions and 4,100 deletions.
1 change: 1 addition & 0 deletions .github/workflows/tutorials.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ concurrency:
group: ${{ github.workflow }}-${{ github.head_ref || github.ref }}
cancel-in-progress: true


jobs:
run-notebook-tutorials:
runs-on: ubuntu-22.04
Expand Down
2 changes: 1 addition & 1 deletion dlordinal/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
__all__ = []
__version__ = "0.1"
__version__ = "1.0.0"
2 changes: 1 addition & 1 deletion dlordinal/losses/general_triangular_loss.py
Original file line number Diff line number Diff line change
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=3)
r = get_general_triangular_probabilities(num_classes, alphas, verbose=0)
cls_probs = torch.tensor(r)

super().__init__(
Expand Down
102 changes: 101 additions & 1 deletion dlordinal/losses/tests/test_beta_loss.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import pytest
import torch

from ..beta_loss import BetaCrossEntropyLoss
Expand All @@ -8,7 +9,7 @@ def test_beta_loss_creation():
assert isinstance(loss, BetaCrossEntropyLoss)


def test_beta_loss_output():
def test_beta_loss_basic():
loss = BetaCrossEntropyLoss(num_classes=6)

input_data = torch.tensor(
Expand All @@ -28,3 +29,102 @@ def test_beta_loss_output():

# Verifies that the loss is greater than zero
assert output.item() > 0


def test_beta_loss_exactvalue():
loss = BetaCrossEntropyLoss(num_classes=6)

input_data = torch.tensor(
[
[0.8, 0.1, 0.1, 0.0, 0.0, 0.0],
[0.1, 0.8, 0.1, 0.0, 0.0, 0.0],
[0.0, 0.1, 0.8, 0.1, 0.0, 0.0],
]
)
target = torch.tensor([0, 1, 2])

# Compute the loss
output = loss(input_data, target)

# Verifies that the output is a tensor
assert isinstance(output, torch.Tensor)

# Verifies that the loss is greater than zero
assert output.item() == pytest.approx(1.3925, rel=1e-3)


def test_beta_loss_relative():
loss = BetaCrossEntropyLoss(num_classes=6)

input_data = torch.tensor(
[
[100.0, 0.0, 0.0, 0.0, 0.0, 0.0],
]
)

input_data2 = torch.tensor(
[
[0.0, 0.0, 100.0, 0.0, 0.0, 0.0],
]
)

input_data3 = torch.tensor(
[
[0.0, 0.0, 0.0, 0.0, 100.0, 0.0],
]
)

target = torch.tensor([0])

# Compute the loss
output = loss(input_data, target)
output2 = loss(input_data2, target)
output3 = loss(input_data3, target)

# Verifies that the output is a tensor
assert isinstance(output, torch.Tensor)

assert output3.item() > output2.item() > output.item()


def test_beta_loss_eta():
input_data = torch.tensor(
[
[0.0, 0.0, 100.0, 0.0, 0.0, 0.0],
]
)

target = torch.tensor([0])

last_loss = None
for eta in [0.1, 0.3, 0.5, 0.7, 1.0]:
loss = BetaCrossEntropyLoss(num_classes=6, eta=eta)

# Compute the loss
output = loss(input_data, target)

if last_loss is not None:
assert output.item() < last_loss.item()

last_loss = output


def test_beta_loss_weights():
weights = torch.tensor([5.0, 2.0, 1.0, 0.5, 0.1, 0.1])
loss = BetaCrossEntropyLoss(num_classes=6, weight=weights)

input_data = torch.tensor(
[
[0.0, 0.0, 100.0, 0.0, 0.0, 0.0],
]
)

target = torch.tensor([0])
target2 = torch.tensor([1])
target3 = torch.tensor([3])

loss1 = loss(input_data, target)
loss2 = loss(input_data, target2)
loss3 = loss(input_data, target3)

assert loss1.item() > loss2.item() > loss3.item()
102 changes: 101 additions & 1 deletion dlordinal/losses/tests/test_binomial_loss.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import pytest
import torch

from ..binomial_loss import BinomialCrossEntropyLoss
Expand All @@ -8,7 +9,7 @@ def test_binomial_loss_creation():
assert isinstance(loss, BinomialCrossEntropyLoss)


def test_binomial_loss_output():
def test_binomial_loss_basic():
loss = BinomialCrossEntropyLoss(num_classes=6)

input_data = torch.tensor(
Expand All @@ -29,3 +30,102 @@ def test_binomial_loss_output():

# Verifies that the loss is greater than zero
assert output.item() > 0


def test_binomial_loss_exactvalue():
loss = BinomialCrossEntropyLoss(num_classes=6)

input_data = torch.tensor(
[
[0.8, 0.1, 0.1, 0.0, 0.0, 0.0],
[0.1, 0.8, 0.1, 0.0, 0.0, 0.0],
[0.0, 0.1, 0.8, 0.1, 0.0, 0.0],
]
)
target = torch.tensor([0, 1, 2])

# Compute the loss
output = loss(input_data, target)

# Verifies that the output is a tensor
assert isinstance(output, torch.Tensor)

# Verifies that the loss is greater than zero
assert output.item() == pytest.approx(1.60699, rel=1e-3)


def test_binomial_loss_relative():
loss = BinomialCrossEntropyLoss(num_classes=6)

input_data = torch.tensor(
[
[100.0, 0.0, 0.0, 0.0, 0.0, 0.0],
]
)

input_data2 = torch.tensor(
[
[0.0, 0.0, 100.0, 0.0, 0.0, 0.0],
]
)

input_data3 = torch.tensor(
[
[0.0, 0.0, 0.0, 0.0, 100.0, 0.0],
]
)

target = torch.tensor([0])

# Compute the loss
output = loss(input_data, target)
output2 = loss(input_data2, target)
output3 = loss(input_data3, target)

# Verifies that the output is a tensor
assert isinstance(output, torch.Tensor)

assert output3.item() > output2.item() > output.item()


def test_binomial_loss_eta():
input_data = torch.tensor(
[
[0.0, 0.0, 100.0, 0.0, 0.0, 0.0],
]
)

target = torch.tensor([0])

last_loss = None
for eta in [0.1, 0.3, 0.5, 0.7, 1.0]:
loss = BinomialCrossEntropyLoss(num_classes=6, eta=eta)

# Compute the loss
output = loss(input_data, target)

if last_loss is not None:
assert output.item() < last_loss.item()

last_loss = output


def test_binomial_loss_weights():
weights = torch.tensor([5.0, 2.0, 1.0, 0.5, 0.1, 0.1])
loss = BinomialCrossEntropyLoss(num_classes=6, weight=weights)

input_data = torch.tensor(
[
[0.0, 0.0, 100.0, 0.0, 0.0, 0.0],
]
)

target = torch.tensor([0])
target2 = torch.tensor([1])
target3 = torch.tensor([3])

loss1 = loss(input_data, target)
loss2 = loss(input_data, target2)
loss3 = loss(input_data, target3)

assert loss1.item() > loss2.item() > loss3.item()
Loading

0 comments on commit 8064fbd

Please sign in to comment.