Skip to content

Commit

Permalink
D. Jabs:
Browse files Browse the repository at this point in the history
- Changed comments
- Improved readability
  • Loading branch information
Dennis Jabs committed Nov 15, 2023
1 parent ea2dac9 commit ba2ca17
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 27 deletions.
10 changes: 7 additions & 3 deletions PyHyperparameterSpace/dist/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,22 @@
class Choice(Distribution):
"""
Class for representing a Categorical choice dist.
Args:
weights (Union[list[float], np.ndarray]):
Probability distribution to select the item
"""

def __init__(self, weights: Union[list[float], list[int], np.ndarray]):
def __init__(self, weights: Union[list[float], np.ndarray]):
self.weights = None
self.change_distribution(weights)

def change_distribution(self, weights: Union[list[float], list[int], np.ndarray]):
def change_distribution(self, weights: Union[list[float], np.ndarray]):
weights = np.array(weights)

assert weights.ndim == 1, f"Illegal weights {weights}. Argument should be a matrix of size (n,)!"
assert np.all(0.0 <= w for w in weights), \
f"Illegal weights {weights}. Each p inside weights should >= 0.0!"
f"Illegal weights {weights}. Each element inside weights should be higher or equal to 0.0!"

# Normalize the weights
self.weights = self._normalize(weights)
Expand Down
30 changes: 27 additions & 3 deletions PyHyperparameterSpace/dist/continuous.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@
class MatrixNormal(Distribution):
"""
Class for representing a Matrix Normal (Gaussian) Distribution ~M N_n,p(M, U, V)
Args:
M (Union[list[list[float]], np.ndarray]):
The mean matrix
U (Union[list[list[float]], np.ndarray]):
Covariance matrix for the rows
V (Union[list[list[float]], np.ndarray]):
Covariance matrix for the columns
"""

def __init__(
Expand All @@ -30,11 +40,11 @@ def change_distribution(
U = np.array(U)
V = np.array(V)

assert M.ndim == 2, f"Illegal M {M}. Argument should be a matrix of size (n, p)!"
assert M.ndim == 2, f"Illegal M {M}. The argument should be a matrix of size (n, p)!"
assert U.ndim == 2 and U.shape == (M.shape[0], M.shape[0]), \
f"Illegal U {U}. Argument should be a matrix of size (n, n)!"
f"Illegal U {U}. The argument should be a matrix of size (n, n)!"
assert V.ndim == 2 and V.shape == (M.shape[1], M.shape[1]), \
f"Illegal V {V}. Argument should be a matrix of size (p, p)!"
f"Illegal V {V}. The argument should be a matrix of size (p, p)!"

self.M = M
self.U = U
Expand All @@ -50,6 +60,13 @@ def __repr__(self):
class MultivariateNormal(Distribution):
"""
Class for representing a Multivariate Normal (Gaussian) Distribution ~N(mean, Covariance)
Args:
mean (Union[list[float], np.ndarray]):
The mean vector
cov (Union[list[list[float]], np.ndarray]):
The covariance matrix
"""

def __init__(self, mean: Union[list[float], np.ndarray], cov: Union[list[list[float]], np.ndarray]):
Expand Down Expand Up @@ -80,6 +97,13 @@ def __repr__(self):
class Normal(Distribution):
"""
Class for representing a Normal (Gaussian) Distribution ~N(mean, std).
Args:
mean (float):
The mean value
std (float):
The standard deviation
"""

def __init__(self, mean: float, std: float):
Expand Down
62 changes: 41 additions & 21 deletions PyHyperparameterSpace/hp/abstract_hp.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,14 @@ class Hyperparameter(ABC):
Abstract class to represent a hyperparameter.
Attributes:
name (str): name of the hyperparameter
default (Any): default value of the hyperparameter
shape (Union[int, tuple[int], None]): shape of the hyperparameter
name (str):
Name of the hyperparameter
default (Any):
Default value of the hyperparameter
shape (Union[int, tuple[int, ...], None]):
Shape of the hyperparameter
"""

def __init__(
Expand All @@ -37,21 +42,24 @@ def __init__(
def get_name(self) -> str:
"""
Returns:
str: name of the hyperparameter
str:
Name of the hyperparameter
"""
return self._name

def get_default(self) -> Any:
"""
Returns:
Any: default value of the hyperparameter
Any:
Default value of the hyperparameter
"""
return self._default

def get_shape(self) -> Union[int, tuple[int, ...], None]:
"""
Returns:
Union[int, tuple[int, ...], None]: shape of the hyperparameter
Union[int, tuple[int, ...], None]:
Shape of the hyperparameter
"""
return self._shape

Expand All @@ -68,10 +76,12 @@ def _check_default(self, default: Any) -> Any:
- if weights are not given: first option of choices
Args:
default (Any): default value to check
default (Any):
Default value to check
Returns:
Any: default value according to the description
Any:
Default value according to the description
"""
pass

Expand All @@ -84,10 +94,12 @@ def _is_legal_default(self, default: Any) -> bool:
- bounds and choices are not given (constant hyperparameter)
Args:
default (Any): default value to check
default (Any):
Default value to check
Returns:
bool: True if default value is legal, otherwise False
bool:
True if default value is legal, otherwise False
"""
pass

Expand All @@ -102,10 +114,12 @@ def _check_shape(self, shape: Union[int, tuple[int, ...], None]) -> Union[int, t
and has the same dimension as the given default value.
Args:
shape (Union[int, tuple[int, ...], None]): shape to check
shape (Union[int, tuple[int, ...], None]):
Shape to check
Returns:
Union[int, tuple[int, ...], None]: legal shape
Union[int, tuple[int, ...], None]:
Legal shape
"""
pass

Expand All @@ -120,10 +134,12 @@ def _is_legal_shape(self, shape: Union[int, tuple[int, ...], None]) -> bool:
and has the same dimension as the given default value.
Args:
shape (Union[int, tuple[int, ...], None]): shape to check
shape (Union[int, tuple[int, ...], None]):
Shape to check
Returns:
bool: True if given shape is legal
bool:
True if given shape is legal
"""
pass

Expand All @@ -133,11 +149,15 @@ def sample(self, random: np.random.RandomState, size: Union[int, None] = None) -
Returns a sample of values from the given hyperparameter, according to the given distribution.
Args:
random (np.random.RandomState): random generator for the sampling procedure
size (Union[int, None]): number of samples
random (np.random.RandomState):
Random generator for the sampling procedure
size (Union[int, None]):
Number of samples
Returns:
Any: sample of values from the given hyperparameter
Any:
Sample of values from the given hyperparameter
"""
pass

Expand All @@ -148,7 +168,7 @@ def valid_configuration(self, value: Any) -> bool:
Args:
value (Any):
The configuration you want to check.
The configuration you want to check
Returns:
bool:
Expand Down Expand Up @@ -202,11 +222,11 @@ def _get_sample_size(
Union[int, tuple[int, ...]]:
Shape of the samples
"""
assert size is None or size > 0, "#ERROR_HYPERPARAMETER: size should be None or higher than 0!"
assert size is None or size > 0, f"Illegal size {size}. The argument should be None or higher than 0!"
if isinstance(shape, int):
assert shape > 0, "#ERROR_HYPERPARAMETER: shape should be higher than 0!"
assert shape > 0, f"Illegal shape {shape}. The argument should be higher than 0!"
elif isinstance(shape, tuple):
assert all(s > 0 for s in shape), "#ERROR_HYPERPARAMETER: shape should be higher than 0!"
assert all(s > 0 for s in shape), f"Illegal shape {shape}. The argument should be higher than 0!"

if shape == 1 or shape == (1,):
# Case: Shape of the hyperparameter is just a single value
Expand Down

0 comments on commit ba2ca17

Please sign in to comment.