Machine Learning
Let's walk through a simple example to understand how Hypster works. We'll create a basic ML classifier configuration.
Prerequisites:
uv add scikit-learnor
pip install scikit-learnConfigurable Machine Learning Classifier
from hypster import HP, instantiate
def classifier_config(hp: HP):
from sklearn.ensemble import HistGradientBoostingClassifier, RandomForestClassifier
# Define the model type choice
model_type = hp.select(["random_forest", "hist_boost"],
name="model_type", default="hist_boost")
# Create the classifier based on selection
if model_type == "hist_boost":
learning_rate = hp.float(0.01, name="learning_rate", min=0.001, max=0.1)
max_depth = hp.int(10, name="max_depth", min=3)
classifier = HistGradientBoostingClassifier(
learning_rate=learning_rate,
max_depth=max_depth,
)
else: # model_type == "random_forest"
n_estimators = hp.int(100, name="n_estimators", max=500)
max_depth = hp.int(5, name="max_depth")
bootstrap = hp.bool(True, name="bootstrap")
classifier = RandomForestClassifier(
n_estimators=n_estimators,
max_depth=max_depth,
bootstrap=bootstrap
)
return {"classifier": classifier}This example demonstrates several key features of Hypster:
Configuration Definition: Using a regular Python function to define a configuration space
Parameter Types: Using different HP call types (
select,float,int,bool)Conditional Logic: Different parameters based on model selection
Multiple Instantiations: Creating different configurations from the same space
Understanding the Code
We define a configuration function that takes an
hpparameter of typeHPThe configuration function uses
returnto explicitly return its outputsWe use various HP calls to define our parameter space:
hp.select()for categorical choiceshp.float()for floating-point valueshp.int()for integer values onlyhp.bool()for boolean values
All
hp.*calls include explicitname="..."arguments (required for overrideability)We use
instantiate(config_func, values=...)to execute the configuration with overrides
Training and Evaluating
This basic example shows how Hypster makes it easy to:
Define configuration spaces with type-safe parameters
Set reasonable defaults and parameter ranges
Create multiple configurations from the same space
Integrate with existing ML libraries seamlessly
Last updated
Was this helpful?