From ad63ee1a9d647b2c0a82cba64c074f60b3009547 Mon Sep 17 00:00:00 2001 From: Lars Reimann Date: Thu, 2 May 2024 16:37:57 +0200 Subject: [PATCH] fix: make `_Layer`, `_InputConversion`, `_OutputConversion` public --- src/safeds/ml/nn/__init__.py | 9 ++++++ src/safeds/ml/nn/_forward_layer.py | 4 +-- src/safeds/ml/nn/_input_conversion.py | 2 +- src/safeds/ml/nn/_input_conversion_table.py | 4 +-- src/safeds/ml/nn/_layer.py | 2 +- src/safeds/ml/nn/_model.py | 30 ++++++++++---------- src/safeds/ml/nn/_output_conversion.py | 2 +- src/safeds/ml/nn/_output_conversion_table.py | 4 +-- 8 files changed, 33 insertions(+), 24 deletions(-) diff --git a/src/safeds/ml/nn/__init__.py b/src/safeds/ml/nn/__init__.py index 6158e640d..84da47c45 100644 --- a/src/safeds/ml/nn/__init__.py +++ b/src/safeds/ml/nn/__init__.py @@ -6,15 +6,21 @@ if TYPE_CHECKING: from ._forward_layer import ForwardLayer + from ._input_conversion import InputConversion from ._input_conversion_table import InputConversionTable + from ._layer import Layer from ._model import NeuralNetworkClassifier, NeuralNetworkRegressor + from ._output_conversion import OutputConversion from ._output_conversion_table import OutputConversionTable apipkg.initpkg( __name__, { "ForwardLayer": "._forward_layer:ForwardLayer", + "InputConversion": "._input_conversion:InputConversion", "InputConversionTable": "._input_conversion_table:InputConversionTable", + "Layer": "._layer:Layer", + "OutputConversion": "._output_conversion:OutputConversion", "OutputConversionTable": "._output_conversion_table:OutputConversionTable", "NeuralNetworkClassifier": "._model:NeuralNetworkClassifier", "NeuralNetworkRegressor": "._model:NeuralNetworkRegressor", @@ -23,7 +29,10 @@ __all__ = [ "ForwardLayer", + "InputConversion", "InputConversionTable", + "Layer", + "OutputConversion", "OutputConversionTable", "NeuralNetworkClassifier", "NeuralNetworkRegressor", diff --git a/src/safeds/ml/nn/_forward_layer.py b/src/safeds/ml/nn/_forward_layer.py index bd0e1ffd1..ba3694ed2 100644 --- a/src/safeds/ml/nn/_forward_layer.py +++ b/src/safeds/ml/nn/_forward_layer.py @@ -7,7 +7,7 @@ from safeds._utils import _structural_hash from safeds.exceptions import ClosedBound, OutOfBoundsError -from safeds.ml.nn._layer import _Layer +from safeds.ml.nn._layer import Layer def _create_internal_model(input_size: int, output_size: int, activation_function: str) -> nn.Module: @@ -33,7 +33,7 @@ def forward(self, x: Tensor) -> Tensor: return _InternalLayer(input_size, output_size, activation_function) -class ForwardLayer(_Layer): +class ForwardLayer(Layer): def __init__(self, output_size: int, input_size: int | None = None): """ Create a FNN Layer. diff --git a/src/safeds/ml/nn/_input_conversion.py b/src/safeds/ml/nn/_input_conversion.py index 8c8d947c5..ca4d7291e 100644 --- a/src/safeds/ml/nn/_input_conversion.py +++ b/src/safeds/ml/nn/_input_conversion.py @@ -13,7 +13,7 @@ PT = TypeVar("PT", Table, TimeSeries) -class _InputConversion(Generic[FT, PT], ABC): +class InputConversion(Generic[FT, PT], ABC): """The input conversion for a neural network, defines the input parameters for the neural network.""" @property diff --git a/src/safeds/ml/nn/_input_conversion_table.py b/src/safeds/ml/nn/_input_conversion_table.py index ccf729102..884d35706 100644 --- a/src/safeds/ml/nn/_input_conversion_table.py +++ b/src/safeds/ml/nn/_input_conversion_table.py @@ -7,10 +7,10 @@ from safeds.data.labeled.containers import TabularDataset from safeds.data.tabular.containers import Table -from safeds.ml.nn._input_conversion import _InputConversion +from safeds.ml.nn._input_conversion import InputConversion -class InputConversionTable(_InputConversion[TabularDataset, Table]): +class InputConversionTable(InputConversion[TabularDataset, Table]): """The input conversion for a neural network, defines the input parameters for the neural network.""" def __init__(self, feature_names: list[str], target_name: str) -> None: diff --git a/src/safeds/ml/nn/_layer.py b/src/safeds/ml/nn/_layer.py index 36f653a50..59224b195 100644 --- a/src/safeds/ml/nn/_layer.py +++ b/src/safeds/ml/nn/_layer.py @@ -7,7 +7,7 @@ from torch import nn -class _Layer(ABC): +class Layer(ABC): @abstractmethod def __init__(self) -> None: pass # pragma: no cover diff --git a/src/safeds/ml/nn/_model.py b/src/safeds/ml/nn/_model.py index 2ed3a412c..f6d4b97b4 100644 --- a/src/safeds/ml/nn/_model.py +++ b/src/safeds/ml/nn/_model.py @@ -18,9 +18,9 @@ from torch import Tensor, nn - from safeds.ml.nn._input_conversion import _InputConversion - from safeds.ml.nn._layer import _Layer - from safeds.ml.nn._output_conversion import _OutputConversion + from safeds.ml.nn._input_conversion import InputConversion + from safeds.ml.nn._layer import Layer + from safeds.ml.nn._output_conversion import OutputConversion IFT = TypeVar("IFT", TabularDataset, TimeSeries) # InputFitType IPT = TypeVar("IPT", Table, TimeSeries) # InputPredictType @@ -30,13 +30,13 @@ class NeuralNetworkRegressor(Generic[IFT, IPT, OT]): def __init__( self, - input_conversion: _InputConversion[IFT, IPT], - layers: list[_Layer], - output_conversion: _OutputConversion[IPT, OT], + input_conversion: InputConversion[IFT, IPT], + layers: list[Layer], + output_conversion: OutputConversion[IPT, OT], ): - self._input_conversion: _InputConversion[IFT, IPT] = input_conversion + self._input_conversion: InputConversion[IFT, IPT] = input_conversion self._model = _create_internal_model(layers, is_for_classification=False) - self._output_conversion: _OutputConversion[IPT, OT] = output_conversion + self._output_conversion: OutputConversion[IPT, OT] = output_conversion self._input_size = self._model.input_size self._batch_size = 1 self._is_fitted = False @@ -177,13 +177,13 @@ def is_fitted(self) -> bool: class NeuralNetworkClassifier(Generic[IFT, IPT, OT]): def __init__( self, - input_conversion: _InputConversion[IFT, IPT], - layers: list[_Layer], - output_conversion: _OutputConversion[IPT, OT], + input_conversion: InputConversion[IFT, IPT], + layers: list[Layer], + output_conversion: OutputConversion[IPT, OT], ): - self._input_conversion: _InputConversion[IFT, IPT] = input_conversion + self._input_conversion: InputConversion[IFT, IPT] = input_conversion self._model = _create_internal_model(layers, is_for_classification=True) - self._output_conversion: _OutputConversion[IPT, OT] = output_conversion + self._output_conversion: OutputConversion[IPT, OT] = output_conversion self._input_size = self._model.input_size self._batch_size = 1 self._is_fitted = False @@ -332,11 +332,11 @@ def is_fitted(self) -> bool: return self._is_fitted -def _create_internal_model(layers: list[_Layer], is_for_classification: bool) -> nn.Module: +def _create_internal_model(layers: list[Layer], is_for_classification: bool) -> nn.Module: from torch import nn class _InternalModel(nn.Module): - def __init__(self, layers: list[_Layer], is_for_classification: bool) -> None: + def __init__(self, layers: list[Layer], is_for_classification: bool) -> None: super().__init__() self._layer_list = layers diff --git a/src/safeds/ml/nn/_output_conversion.py b/src/safeds/ml/nn/_output_conversion.py index 17bb789dc..2305fcbc2 100644 --- a/src/safeds/ml/nn/_output_conversion.py +++ b/src/safeds/ml/nn/_output_conversion.py @@ -13,7 +13,7 @@ OT = TypeVar("OT", TabularDataset, TimeSeries) -class _OutputConversion(Generic[IT, OT], ABC): +class OutputConversion(Generic[IT, OT], ABC): """The output conversion for a neural network, defines the output parameters for the neural network.""" @abstractmethod diff --git a/src/safeds/ml/nn/_output_conversion_table.py b/src/safeds/ml/nn/_output_conversion_table.py index c26c33d85..5c956d9a2 100644 --- a/src/safeds/ml/nn/_output_conversion_table.py +++ b/src/safeds/ml/nn/_output_conversion_table.py @@ -7,10 +7,10 @@ from safeds.data.labeled.containers import TabularDataset from safeds.data.tabular.containers import Column, Table -from safeds.ml.nn._output_conversion import _OutputConversion +from safeds.ml.nn._output_conversion import OutputConversion -class OutputConversionTable(_OutputConversion[Table, TabularDataset]): +class OutputConversionTable(OutputConversion[Table, TabularDataset]): """The output conversion for a neural network, defines the output parameters for the neural network.""" def __init__(self, prediction_name: str = "prediction") -> None: