Skip to content

Commit

Permalink
style: apply automated linter fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
megalinter-bot committed Jul 12, 2024
1 parent 8fc9357 commit c77d58d
Show file tree
Hide file tree
Showing 16 changed files with 51 additions and 51 deletions.
4 changes: 2 additions & 2 deletions src/safeds/ml/nn/_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def get_parameter_count(self) -> int:
last_input_neurons = self.input_size if isinstance(self.input_size, int) else 0
last_input_channels = self.input_size.channel if isinstance(self.input_size, ModelImageSize) else 0
for layer in self._layers:
layer._set_input_size(last_input_neurons if last_type=="int" else last_input_channels)
layer._set_input_size(last_input_neurons if last_type == "int" else last_input_channels)
summand += layer.get_parameter_count(TensorShape([last_input_neurons, last_input_channels]))
last_input_neurons = layer.output_size if isinstance(layer.output_size, int) else 0
last_input_channels = layer.output_size.channel if isinstance(layer.output_size, ModelImageSize) else 0
Expand Down Expand Up @@ -411,7 +411,7 @@ def get_parameter_count(self) -> int:
last_input_neurons = self.input_size if isinstance(self.input_size, int) else 0
last_input_channels = self.input_size.channel if isinstance(self.input_size, ModelImageSize) else 0
for layer in self._layers:
layer._set_input_size(last_input_neurons if last_type=="int" else last_input_channels)
layer._set_input_size(last_input_neurons if last_type == "int" else last_input_channels)
summand += layer.get_parameter_count(TensorShape([last_input_neurons, last_input_channels]))
last_input_neurons = layer.output_size if isinstance(layer.output_size, int) else 0
last_input_channels = layer.output_size.channel if isinstance(layer.output_size, ModelImageSize) else 0
Expand Down
4 changes: 2 additions & 2 deletions src/safeds/ml/nn/layers/_convolutional2d_layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ def __sizeof__(self) -> int:
)

def get_parameter_count(self, input_size: TensorShape) -> int:
return int((self._kernel_size*self._kernel_size*input_size._dims[1]+1)*self._output_channel)
return int((self._kernel_size * self._kernel_size * input_size._dims[1] + 1) * self._output_channel)


class ConvolutionalTranspose2DLayer(Convolutional2DLayer):
Expand Down Expand Up @@ -266,4 +266,4 @@ def __sizeof__(self) -> int:
return sys.getsizeof(self._output_padding) + super().__sizeof__()

def get_parameter_count(self, input_size: TensorShape) -> int:
return int((self._kernel_size*self._kernel_size*input_size._dims[1]+1)*self._output_channel)
return int((self._kernel_size * self._kernel_size * input_size._dims[1] + 1) * self._output_channel)
2 changes: 1 addition & 1 deletion src/safeds/ml/nn/layers/_flatten_layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,6 @@ def __eq__(self, other: object) -> bool:

def __sizeof__(self) -> int:
return sys.getsizeof(self._input_size) + sys.getsizeof(self._output_size)

def get_parameter_count(self, input_size: TensorShape) -> int: # noqa: ARG002
return 0
2 changes: 1 addition & 1 deletion src/safeds/ml/nn/layers/_forward_layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,4 @@ def __sizeof__(self) -> int:
return sys.getsizeof(self._input_size) + sys.getsizeof(self._output_size)

def get_parameter_count(self, input_size: TensorShape) -> int:
return (input_size._dims[0]+1)*self._output_size
return (input_size._dims[0] + 1) * self._output_size
3 changes: 1 addition & 2 deletions src/safeds/ml/nn/layers/_gru_layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,5 +97,4 @@ def __sizeof__(self) -> int:
return sys.getsizeof(self._input_size) + sys.getsizeof(self._output_size)

def get_parameter_count(self, input_size: TensorShape) -> int:
return (input_size._dims[0]+self._output_size+2)*self._output_size*3

return (input_size._dims[0] + self._output_size + 2) * self._output_size * 3
2 changes: 1 addition & 1 deletion src/safeds/ml/nn/layers/_layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,4 @@ def __sizeof__(self) -> int:

@abstractmethod
def get_parameter_count(self, input_size: TensorShape) -> int:
pass # pragma: no cover
pass # pragma: no cover
3 changes: 1 addition & 2 deletions src/safeds/ml/nn/layers/_lstm_layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,5 +97,4 @@ def __sizeof__(self) -> int:
return sys.getsizeof(self._input_size) + sys.getsizeof(self._output_size)

def get_parameter_count(self, input_size: TensorShape) -> int:
return (input_size._dims[0]+self._output_size+2)*self._output_size*4

return (input_size._dims[0] + self._output_size + 2) * self._output_size * 4
2 changes: 1 addition & 1 deletion src/safeds/ml/nn/layers/_pooling2d_layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ def __sizeof__(self) -> int:
+ sys.getsizeof(self._stride)
+ sys.getsizeof(self._padding)
)

def get_parameter_count(self, input_size: TensorShape) -> int: # noqa: ARG002
return 0

Expand Down
22 changes: 11 additions & 11 deletions src/safeds/ml/nn/typing/_tensor_shape.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ class TensorShape:
Parameters
----------
dims:
A list of integers where each integer represents
dims:
A list of integers where each integer represents
the size of the tensor in a particular dimension.
"""

def __init__(self, dims: list[int]) -> None:
self._dims = dims
self._dims = dims

def get_size(self, dimension: int | None = None) -> int:
"""
Expand All @@ -36,17 +36,17 @@ def get_size(self, dimension: int | None = None) -> int:
OutOfBoundsError:
If the actual value is outside its expected range.
"""
_check_bounds("dimension",dimension, lower_bound=_ClosedBound(0))
_check_bounds("dimension", dimension, lower_bound=_ClosedBound(0))
if dimension is not None and dimension >= self.dimensionality:

Check warning on line 40 in src/safeds/ml/nn/typing/_tensor_shape.py

View check run for this annotation

Codecov / codecov/patch

src/safeds/ml/nn/typing/_tensor_shape.py#L39-L40

Added lines #L39 - L40 were not covered by tests
#TODO maybe add error message indicating that the dimension is out of range
return 0
if(dimension is None):
# TODO maybe add error message indicating that the dimension is out of range
return 0
if dimension is None:
return self._dims[0]
return self._dims[dimension]

Check warning on line 45 in src/safeds/ml/nn/typing/_tensor_shape.py

View check run for this annotation

Codecov / codecov/patch

src/safeds/ml/nn/typing/_tensor_shape.py#L42-L45

Added lines #L42 - L45 were not covered by tests

def __hash__(self) -> int:
return _structural_hash(self._dims)

Check warning on line 48 in src/safeds/ml/nn/typing/_tensor_shape.py

View check run for this annotation

Codecov / codecov/patch

src/safeds/ml/nn/typing/_tensor_shape.py#L48

Added line #L48 was not covered by tests

@property
def dimensionality(self) -> int:
"""
Expand All @@ -56,4 +56,4 @@ def dimensionality(self) -> int:
-------
int: The number of dimensions of the tensor.
"""
return len(self._dims)
return len(self._dims)

Check warning on line 59 in src/safeds/ml/nn/typing/_tensor_shape.py

View check run for this annotation

Codecov / codecov/patch

src/safeds/ml/nn/typing/_tensor_shape.py#L59

Added line #L59 was not covered by tests
22 changes: 11 additions & 11 deletions tests/safeds/ml/nn/layers/test_convolutional2d_layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,22 +157,22 @@ def test_should_raise_if_input_size_is_set_with_int(
layer = conv_type(output_channel, kernel_size, stride=stride, padding=padding)
with pytest.raises(TypeError, match=r"The input_size of a convolution layer has to be of type ImageSize."):
layer._set_input_size(1)

def test_conv_get_parameter_count_returns_right_amount(self) -> None:
kernel_size=5
input_channels=3
output_channels=3
expected_output = int((kernel_size*kernel_size*input_channels+1)*output_channels)
kernel_size = 5
input_channels = 3
output_channels = 3
expected_output = int((kernel_size * kernel_size * input_channels + 1) * output_channels)
layer = Convolutional2DLayer(input_channels, kernel_size)
assert layer.get_parameter_count(TensorShape([1,input_channels])) == expected_output
assert layer.get_parameter_count(TensorShape([1, input_channels])) == expected_output

def test_conv_transposed_get_parameter_count_returns_right_amount(self) -> None:
kernel_size=5
input_channels=3
output_channels=3
expected_output = int((kernel_size*kernel_size*input_channels+1)*output_channels)
kernel_size = 5
input_channels = 3
output_channels = 3
expected_output = int((kernel_size * kernel_size * input_channels + 1) * output_channels)
layer = ConvolutionalTranspose2DLayer(input_channels, kernel_size)
assert layer.get_parameter_count(TensorShape([1,input_channels])) == expected_output
assert layer.get_parameter_count(TensorShape([1, input_channels])) == expected_output

class TestEq:
@pytest.mark.parametrize(
Expand Down
1 change: 0 additions & 1 deletion tests/safeds/ml/nn/layers/test_flatten_layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ def test_get_parameter_count_right_output(self) -> None:
layer = FlattenLayer()
assert layer.get_parameter_count(TensorShape([1])) == 0


class TestEq:
def test_should_be_equal(self) -> None:
assert FlattenLayer() == FlattenLayer()
Expand Down
7 changes: 4 additions & 3 deletions tests/safeds/ml/nn/layers/test_forward_layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,10 @@ def test_should_assert_that_different_forward_layers_have_different_hash(
def test_should_assert_that_layer_size_is_greater_than_normal_object(layer: ForwardLayer) -> None:
assert sys.getsizeof(layer) > sys.getsizeof(object())


def test_conv_transposed_get_parameter_count_returns_right_amount() -> None:
input_neurons=3
output_neurons=3
expected_output = int((input_neurons+1)*output_neurons)
input_neurons = 3
output_neurons = 3
expected_output = int((input_neurons + 1) * output_neurons)
layer = ForwardLayer(output_neurons)
assert layer.get_parameter_count(TensorShape([input_neurons])) == expected_output
7 changes: 4 additions & 3 deletions tests/safeds/ml/nn/layers/test_gru_layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,10 @@ def test_internal_layer_should_raise_error() -> None:
with pytest.raises(ValueError, match="The input_size is not yet set."):
layer._get_internal_layer(activation_function="relu")


def test_conv_transposed_get_parameter_count_returns_right_amount() -> None:
input_neurons=4
output_neurons=16
expected_output = int((input_neurons+output_neurons+2)*output_neurons*3)
input_neurons = 4
output_neurons = 16
expected_output = int((input_neurons + output_neurons + 2) * output_neurons * 3)
layer = GRULayer(output_neurons)
assert layer.get_parameter_count(TensorShape([input_neurons])) == expected_output
7 changes: 4 additions & 3 deletions tests/safeds/ml/nn/layers/test_lstm_layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,10 @@ def test_should_assert_that_different_forward_layers_have_different_hash(
def test_should_assert_that_layer_size_is_greater_than_normal_object(layer: LSTMLayer) -> None:
assert sys.getsizeof(layer) > sys.getsizeof(object())


def test_conv_transposed_get_parameter_count_returns_right_amount() -> None:
input_neurons=4
output_neurons=16
expected_output = int((input_neurons+output_neurons+2)*output_neurons*4)
input_neurons = 4
output_neurons = 16
expected_output = int((input_neurons + output_neurons + 2) * output_neurons * 4)
layer = LSTMLayer(output_neurons)
assert layer.get_parameter_count(TensorShape([input_neurons])) == expected_output
2 changes: 1 addition & 1 deletion tests/safeds/ml/nn/layers/test_pooling2d_layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def test_should_raise_if_input_size_is_set_with_int(self, strategy: Literal["max
layer._set_input_size(1)

@pytest.mark.parametrize(
"strategy",
"strategy",
[
"max",
"avg",
Expand Down
12 changes: 6 additions & 6 deletions tests/safeds/ml/nn/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -526,13 +526,13 @@ def test_parameters_model_not_fitted(self, device: Device) -> None:

def test_should_sum_parameters(self, device: Device) -> None:
configure_test_with_device(device)
expected_output = 16+0+9
expected_output = 16 + 0 + 9
model_fitted = NeuralNetworkClassifier(
InputConversionTable(),
[ForwardLayer(neuron_count=8), DropoutLayer(0.5), ForwardLayer(neuron_count=1)],
).fit(
Table.from_dict({"a": [1, 1, 1], "b": [2, 2, 2]}).to_tabular_dataset("a"),
epoch_size=3,
Table.from_dict({"a": [1, 1, 1], "b": [2, 2, 2]}).to_tabular_dataset("a"),
epoch_size=3,
)
assert expected_output == model_fitted.get_parameter_count()

Expand Down Expand Up @@ -913,12 +913,12 @@ def test_parameters_model_not_fitted(self, device: Device) -> None:

def test_should_sum_parameters(self, device: Device) -> None:
configure_test_with_device(device)
expected_output = 16+0+9
expected_output = 16 + 0 + 9
model_fitted = NeuralNetworkRegressor(
InputConversionTable(),
[ForwardLayer(neuron_count=8), DropoutLayer(0.5), ForwardLayer(neuron_count=1)],
).fit(
Table.from_dict({"a": [1, 1, 1], "b": [2, 2, 2]}).to_tabular_dataset("a"),
epoch_size=3,
Table.from_dict({"a": [1, 1, 1], "b": [2, 2, 2]}).to_tabular_dataset("a"),
epoch_size=3,
)
assert expected_output == model_fitted.get_parameter_count()

0 comments on commit c77d58d

Please sign in to comment.