-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Updated Categorical Hyperparameter, where the weights are now inside the distribution Choice() and not inside the class itself - Updated unittests
- Loading branch information
Dennis Jabs
committed
Nov 3, 2023
1 parent
c21f82c
commit ea2dac9
Showing
7 changed files
with
88 additions
and
199 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,59 @@ | ||
from typing import Union | ||
import numpy as np | ||
|
||
|
||
from PyHyperparameterSpace.dist.abstract_dist import Distribution | ||
|
||
|
||
class Choice(Distribution): | ||
""" | ||
Class for representing a Categorical choice dist. | ||
TODO: Refactor | ||
""" | ||
|
||
def __init__(self): | ||
pass | ||
def __init__(self, weights: Union[list[float], list[int], np.ndarray]): | ||
self.weights = None | ||
self.change_distribution(weights) | ||
|
||
def change_distribution(self, weights: Union[list[float], list[int], 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!" | ||
|
||
# Normalize the weights | ||
self.weights = self._normalize(weights) | ||
|
||
@classmethod | ||
def _normalize(cls, p: Union[list[float], np.ndarray]) -> Union[list[float], np.ndarray]: | ||
""" | ||
Normalizes the given probability distribution, so that sum(p)=1. | ||
Args: | ||
p (Union[list[float], np.ndarray]): | ||
Non-normalized probability distribution | ||
Returns: | ||
Union[list[float], np.ndarray]: | ||
Normalized probability distribution | ||
""" | ||
assert all(0.0 <= prob for prob in p), \ | ||
"The given non-normalized dist p cannot contain negative values!" | ||
|
||
if isinstance(p, list): | ||
result_type = list | ||
else: | ||
result_type = np.array | ||
|
||
def change_distribution(**kwargs): | ||
raise Exception("Illegal call of change_distribution(). Choice distribution cannot be changed!") | ||
sum_p = np.sum(p) | ||
if sum_p == 1: | ||
# Case: p is already normalized | ||
return result_type(p) | ||
# Case: p should be normalized | ||
return result_type([prob / sum_p for prob in p]) | ||
|
||
def __str__(self): | ||
return "Choice()" | ||
return f"Choice(weights={self.weights})" | ||
|
||
def __repr__(self): | ||
return self.__str__() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.