Skip to content

Commit

Permalink
feat: hyperparameter optimization for fnn models (#897)
Browse files Browse the repository at this point in the history
Closes #861 

### Summary of Changes

Added fit_by_exhaustive_search method to NNClassifier, NNRegressor
Added FittingWithChoice and FittingWithoutChoice Error
Added Choice options for neuron_count params of all layers
Added contains_choices method to all layers
Added _get_layers_for_all_choices method to all layers
Added ClassifierMetric and RegressorMetric as Enums

fit_by_exhaustive_search currently only works for tables as input,
images and timeseries will be added in a later pr

---------

Co-authored-by: megalinter-bot <129584137+megalinter-bot@users.noreply.github.com>
  • Loading branch information
sibre28 and megalinter-bot authored Jul 15, 2024
1 parent 5447551 commit c1f66e5
Show file tree
Hide file tree
Showing 17 changed files with 1,237 additions and 533 deletions.
8 changes: 7 additions & 1 deletion src/safeds/exceptions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
from ._ml import (
DatasetMissesDataError,
DatasetMissesFeaturesError,
EmptyChoiceError,
FeatureDataMismatchError,
FittingWithChoiceError,
FittingWithoutChoiceError,
InputSizeError,
InvalidFitDataError,
InvalidModelStructureError,
Expand Down Expand Up @@ -72,13 +75,16 @@ class OutOfBoundsError(SafeDsError):
# ML exceptions
"DatasetMissesDataError",
"DatasetMissesFeaturesError",
"TargetDataMismatchError",
"EmptyChoiceError",
"FeatureDataMismatchError",
"FittingWithChoiceError",
"FittingWithoutChoiceError",
"InvalidFitDataError",
"InputSizeError",
"InvalidModelStructureError",
"LearningError",
"ModelNotFittedError",
"PlainTableError",
"PredictionError",
"TargetDataMismatchError",
]
27 changes: 27 additions & 0 deletions src/safeds/exceptions/_ml.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,33 @@ def __init__(self) -> None:
super().__init__("Dataset contains no rows")


class EmptyChoiceError(ValueError):
"""Raised when a choice object is created, but no arguments are provided."""

def __init__(self) -> None:
super().__init__("Please provide at least one Value in a Choice Parameter")


class FittingWithChoiceError(Exception):
"""Raised when a model is fitted with a choice object as a parameter."""

def __init__(self) -> None:
super().__init__(
"Error occurred while fitting: Trying to fit with a Choice Parameter. Please use "
"fit_by_exhaustive_search() instead.",
)


class FittingWithoutChoiceError(Exception):
"""Raised when a model is fitted by exhaustive search without a choice object as a parameter."""

def __init__(self) -> None:
super().__init__(
"Error occurred while fitting: Trying to fit by exhaustive search without a Choice "
"Parameter. Please use fit() instead.",
)


class InvalidFitDataError(Exception):
"""Raised when a Neural Network is fitted on invalid data."""

Expand Down
14 changes: 13 additions & 1 deletion src/safeds/ml/hyperparameters/_choice.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from collections.abc import Collection
from typing import TYPE_CHECKING, TypeVar

from safeds.exceptions import EmptyChoiceError

if TYPE_CHECKING:
from collections.abc import Iterator
from typing import Any
Expand All @@ -15,14 +17,17 @@ class Choice(Collection[T]):

def __init__(self, *args: T) -> None:
"""
Create a new choice.
Create a new choice. Duplicate values will be removed.
Parameters
----------
*args:
The values to choose from.
"""
self.elements = list(args)
if len(args) < 1:
raise EmptyChoiceError
self.elements = list(dict.fromkeys(args))

def __contains__(self, value: Any) -> bool:
"""
Expand Down Expand Up @@ -61,3 +66,10 @@ def __len__(self) -> int:
The number of values in this choice.
"""
return len(self.elements)

def __eq__(self, other: object) -> bool:
if not isinstance(other, Choice):
return NotImplemented
if self is other:
return True
return (self is other) or (self.elements == other.elements)
Loading

0 comments on commit c1f66e5

Please sign in to comment.