Skip to content

Commit

Permalink
Merge pull request #28 from imdeepmind/test-scripts-for-loss-functions
Browse files Browse the repository at this point in the history
Test scripts for loss functions
  • Loading branch information
imdeepmind authored Jun 20, 2020
2 parents 5c32c04 + e56d0f3 commit b6b0d30
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 42 deletions.
2 changes: 1 addition & 1 deletion neuralpy/loss_functions/cross_entropy.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def __init__(self, weight=None, ignore_index=-100, reduction='mean'):
if reduction not in ["none", "mean", "sum"]:
raise ValueError("Invalid reduction")

if not isinstance(ignore_index, int):
if isinstance(ignore_index, bool) or not isinstance(ignore_index, int):
raise ValueError("Invalid ignore index")

self.__weight = weight
Expand Down
1 change: 0 additions & 1 deletion neuralpy/loss_functions/mse.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

# pylint: disable=too-few-public-methods


class MSELoss:
"""
Applies a Mean Squared Error loss function to the model.
Expand Down
25 changes: 9 additions & 16 deletions tests/neuralpy/loss_functions/test_bce_loss.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,24 @@
import numpy as np
import torch

# Possible values that are invalid
weights=["asd", 12, -.3]
reductions=["asdas", "", 12, 6.3]
pos_weights=["asd", 12, -.3]

@pytest.mark.parametrize(
"weight, reduction, pos_weight",
[(weight, reduction, pos_weight) for weight in weights
for reduction in reductions
for pos_weight in pos_weights]
[("invalid", 12, -.3),
(12, 12, -.3),
([1.0, 1.0, 1.0], "sump", -.3),
([1.0, 1.0, 1.0], 3, -.3),
([1.0, 1.0, 1.0], "sum", -.3),
([1.0, 1.0, 1.0], "sum", "invalid")]
)
def test_bce_should_throw_value_error(weight, reduction, pos_weight):
with pytest.raises(ValueError) as ex:
x = BCELoss(weight=weight, reduction=reduction, pos_weight=pos_weight)

# Possible values that are valid
weights=[[1.0, 1.0, 1.0], [2.0, 1.0, 2.0], np.ones([3])]
reductions=["mean"]
pos_weights=[[1.0, 1.0, 1.0], [2.0, 1.0, 2.0], np.ones([3])]

@pytest.mark.parametrize(
"weight, reduction, pos_weight",
[(weight, reduction, pos_weight) for weight in weights
for reduction in reductions
for pos_weight in pos_weights]
[([1.0, 1.0, 1.0], "mean", np.ones([3])),
(np.ones([3]), "mean", [2.0, 1.0, 2.0]),
]
)
def test_bce_get_layer_method(weight, reduction, pos_weight):
x = BCELoss(weight=weight, reduction=reduction, pos_weight=pos_weight)
Expand Down
31 changes: 15 additions & 16 deletions tests/neuralpy/loss_functions/test_cross_entropy_loss.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,30 @@
import numpy as np
import torch

# Possible values that are invalid
weights=["asd", 12, -.3]
reductions=["asdas", "", 12, 6.3]
ignore_indexes=["asd", False, "", 2.36]

@pytest.mark.parametrize(
"weight, reduction, ignore_index",
[(weight, reduction, ignore_index) for weight in weights
for reduction in reductions
for ignore_index in ignore_indexes]
[
("invalid", "invalid", "invalid"),
(12, "invalid", "invalid"),
(np.ones([3]), "invalid", "invalid"),
(np.ones([3]), 12, "invalid"),
(np.ones([3]), "mean", "mean"),
(np.ones([3]), "sum", "sum"),
(np.ones([3]), "none", "asd"),
(np.ones([3]), "none", False)
]
)
def test_cce_should_throw_value_error(weight, reduction, ignore_index):
with pytest.raises(ValueError) as ex:
x = CrossEntropyLoss(weight=weight, reduction=reduction, ignore_index=ignore_index)

# Possible values that are valid
weights=[[1.0, 1.0, 1.0], [2.0, 1.0, 2.0], np.ones([3])]
reductions=["mean"]
ignore_indexes=[1, -100, 5]

@pytest.mark.parametrize(
"weight, reduction, ignore_index",
[(weight, reduction, ignore_index) for weight in weights
for reduction in reductions
for ignore_index in ignore_indexes]
[
([2.0, 1.0, 2.0], "mean", -100),
(np.ones([3]), "sum", 1),
(np.ones([3]), "none", 1),
]
)
def test_cce_get_layer_method(weight, reduction, ignore_index):
x = CrossEntropyLoss(weight=weight, reduction=reduction, ignore_index=ignore_index)
Expand Down
10 changes: 2 additions & 8 deletions tests/neuralpy/loss_functions/test_mse.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,17 @@
import numpy as np
import torch

# Possible values that are invalid
reductions=["asdas", "", 12, 6.3]

@pytest.mark.parametrize(
"reduction",
[(reduction) for reduction in reductions]
[("invalid", "", 12, 6.3)]
)
def test_cce_should_throw_value_error(reduction):
with pytest.raises(ValueError) as ex:
x = MSELoss(reduction=reduction)

# Possible values that are valid
reductions=["mean"]

@pytest.mark.parametrize(
"reduction",
[(reduction) for reduction in reductions]
[("none"), ("mean"), ("sum")]
)
def test_mse_get_layer_method(reduction):
x = MSELoss(reduction=reduction)
Expand Down

0 comments on commit b6b0d30

Please sign in to comment.