Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: make _Layer, _InputConversion, _OutputConversion public #692

Merged
merged 1 commit into from
May 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/safeds/ml/nn/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -23,7 +29,10 @@

__all__ = [
"ForwardLayer",
"InputConversion",
"InputConversionTable",
"Layer",
"OutputConversion",
"OutputConversionTable",
"NeuralNetworkClassifier",
"NeuralNetworkRegressor",
Expand Down
4 changes: 2 additions & 2 deletions src/safeds/ml/nn/_forward_layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion src/safeds/ml/nn/_input_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/safeds/ml/nn/_input_conversion_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion src/safeds/ml/nn/_layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from torch import nn


class _Layer(ABC):
class Layer(ABC):
@abstractmethod
def __init__(self) -> None:
pass # pragma: no cover
Expand Down
30 changes: 15 additions & 15 deletions src/safeds/ml/nn/_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/safeds/ml/nn/_output_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/safeds/ml/nn/_output_conversion_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down