🧠Best Practices
"Shift Left" - Move Complexity to your Configs
Hypster encourages moving complexity into the configuration phase ("shifting left") rather than the execution phase:
from hypster import HP, instantiate
def model_config(hp: HP):
# Complex logic in configuration
model_type = hp.select(["lstm", "transformer"], name="model_type", default="lstm")
if model_type == "lstm":
hidden_size = hp.int(128, name="hidden_size", min=64, max=512)
num_layers = hp.int(2, name="num_layers", min=1, max=4)
bidirectional = hp.bool(True, name="bidirectional")
model = LSTMModel(
hidden_size=hidden_size,
num_layers=num_layers,
bidirectional=bidirectional
)
else: # transformer
num_heads = hp.int(8, name="num_heads", min=4, max=16)
num_layers = hp.int(6, name="num_layers", min=2, max=12)
dropout = hp.float(0.1, name="dropout", min=0, max=0.5)
model = TransformerModel(
num_heads=num_heads,
num_layers=num_layers,
dropout=dropout
)
# Common training parameters
optimizer = hp.select(["adam", "sgd"], name="optimizer", default="adam")
learning_rate = hp.float(0.001, name="learning_rate", min=1e-5, max=0.1)
return {
"model": model,
"optimizer": optimizer,
"learning_rate": learning_rate
}
# Simple execution code (outside config)
config = instantiate(model_config, values={"model_type": "transformer"})
model = config["model"]
model.fit(X_train, y_train) # All complexity handled in configPerformance Guidelines
Keep configuration execution under 1ms
Never make API calls or database requests during configuration
Avoid any operations that incur costs
Defer resource initialization to execution phase
Pythonic Configuration
Use Native Python Features
Utilize Hypster's built-in Type Safety
Use Built-in Type Checking
Value Validation
Required Naming Convention
All hp.* calls that you want to be overrideable must include an explicit name="..." argument. An hp.* call without a name will raise an error upon execution.
Configuration Function Structure
Last updated
Was this helpful?