Skip to content

Commit

Permalink
fix: include probability when computing equality or hash
Browse files Browse the repository at this point in the history
  • Loading branch information
lars-reimann committed Jun 29, 2024
1 parent f0ef2ff commit 151401e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
6 changes: 4 additions & 2 deletions src/safeds/ml/nn/layers/_dropout_layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,14 @@ def _set_input_size(self, input_size: int | ModelImageSize) -> None:
self._input_size = input_size

def __hash__(self) -> int:
return _structural_hash(self._input_size)
return _structural_hash(self._input_size, self._probability)

def __eq__(self, other: object) -> bool:
if not isinstance(other, DropoutLayer):
return NotImplemented
return (self is other) or (self._input_size == other._input_size)
if self is other:
return True
return self._input_size == other._input_size and self._probability == other._probability

def __sizeof__(self) -> int:
if self._input_size is None:
Expand Down
22 changes: 11 additions & 11 deletions tests/safeds/ml/nn/layers/test_dropout_layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,17 @@
from torch import nn


class TestProbability:
def test_should_be_accessible(self) -> None:
probability = 0.5
layer = DropoutLayer(probability)
assert layer.probability == probability

@pytest.mark.parametrize("probability", [-1, 2], ids=["too low", "too high"])
def test_should_raise_if_out_of_bounds(self, probability: int) -> None:
with pytest.raises(OutOfBoundsError):
DropoutLayer(probability)

class TestDropoutLayer:
def test_should_create_dropout_layer(self) -> None:
size = 10
Expand All @@ -17,12 +28,6 @@ def test_should_create_dropout_layer(self) -> None:
assert layer.output_size == size
assert isinstance(next(next(layer._get_internal_layer().modules()).children()), nn.Dropout)

def test_should_check_bounds(self) -> None:
with pytest.raises(OutOfBoundsError, match=r"probability must be in \(0, 1\) but was 2."):
DropoutLayer(2)
with pytest.raises(OutOfBoundsError, match=r"probability must be in \(0, 1\) but was -1."):
DropoutLayer(-1)

def test_input_size_should_be_set(self) -> None:
layer = DropoutLayer(0.5)
with pytest.raises(ValueError, match=r"The input_size is not yet set."):
Expand All @@ -37,11 +42,6 @@ def test_input_size_should_be_set(self) -> None:
with pytest.raises(ValueError, match=r"The input_size is not yet set."):
layer.__sizeof__()

def test_probability_is_set(self) -> None:
probability_to_set = 0.5
layer = DropoutLayer(probability_to_set)
assert layer.probability == probability_to_set


class TestEq:
def test_should_be_equal(self) -> None:
Expand Down

0 comments on commit 151401e

Please sign in to comment.