diff --git a/src/safeds/ml/nn/layers/_dropout_layer.py b/src/safeds/ml/nn/layers/_dropout_layer.py index cbb539434..1814e4383 100644 --- a/src/safeds/ml/nn/layers/_dropout_layer.py +++ b/src/safeds/ml/nn/layers/_dropout_layer.py @@ -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: diff --git a/tests/safeds/ml/nn/layers/test_dropout_layer.py b/tests/safeds/ml/nn/layers/test_dropout_layer.py index f5aa562c0..5dad5f3a8 100644 --- a/tests/safeds/ml/nn/layers/test_dropout_layer.py +++ b/tests/safeds/ml/nn/layers/test_dropout_layer.py @@ -8,6 +8,18 @@ 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 @@ -17,12 +29,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."): @@ -37,11 +43,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: