๐คParameter Naming
Hypster provides sensible defaults for naming your variables to keep your code DRY (Don't Repeat Yourself), while also supporting explicit naming when needed.
Naming Methods
Explicit Naming
Use the name
parameter when you want full control over variable names:
@config
def explicit_naming(hp: HP):
var = hp.select(["o1", "o2"], name="my_explicit_variable")
learning_rate = hp.number(0.001, name="lr")
Automatic Naming
Hypster automatically infers names from three contexts:
Variable Assignment
# Name becomes "model_type" model_type = hp.select(["cnn", "rnn"]) # Name becomes "learning_rate" learning_rate = hp.number(0.001)
Dictionary Keys
config = { "learning_rate": hp.number(0.001), # "config.learning_rate" "model_params": { "layers": hp.int(3) # "config.model_params.layers" } }
Function/Class Keywords
# Class initialization model = ModelConfig( model_type=hp.select(["cnn", "rnn"]), # "model.model_type" learning_rate=hp.number(0.001) # "model.learning_rate" ) # Function calls result = process_data( batch_size=hp.int(32) # "result.batch_size" )
Name Injection Process
Hypster uses Python's Abstract Syntax Tree (AST) to automatically inject parameter names:
# Original code
@config
def my_config(hp: HP):
model = hp.select(["cnn", "rnn"])
config = {"lr": hp.number(0.001)}
# After name injection (internal representation)
def my_config(hp: HP):
model = hp.select(["cnn", "rnn"], name="model")
config = {"lr": hp.number(0.001, name="config.lr")}
Important Notes
Assignment Priority
# Names are based on assignment targets, not function names result = some_func(param=hp.select([1, 2])) # Creates "result.param, not some_func.param"
Nested Naming
model = Model( type=hp.select(["cnn", "rnn"]), # "model.type" params={ "lr": hp.number(0.1), # "model.params.lr" "layers": hp.int(3) # "model.params.layers" } )
Warning: Avoid ambiguous assignments
# Bad: Unclear naming x = y = hp.select(["a", "b"]) # Which name should be used? # Good: Clear assignment model_type = hp.select(["a", "b"])
Disabling Automatic Naming
Security Note: While name injection is designed to be safe, users with strict security requirements can disable it using
inject_names=False
.
@config(inject_names=False)
def my_config(hp: HP):
# Must provide explicit names
model = hp.select(["cnn", "rnn"], name="model_type")
config = {
"lr": hp.number(0.001, name="learning_rate")
}
Best Practices
Use Descriptive Variables
# Good: Clear variable names learning_rate = hp.number(0.001) model_type = hp.select(["cnn", "rnn"]) # Bad: Unclear names x = hp.number(0.001) y = hp.select(["cnn", "rnn"])
Consistent Naming
# Good: Consistent structure model_config = { "type": hp.select(["cnn", "rnn"]), "params": { "learning_rate": hp.number(0.001) } }
Explicit Names for Clarity
# Use explicit names when auto-naming might be ambiguous result = complex_function( param=hp.select([1, 2], name="specific_param_name") )
Last updated
Was this helpful?