Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
298 views
in Technique[技术] by (71.8m points)

loops - Composable parameter iteration in Python

I'm looking for a trick similar to that used by KerasTuner's hyperparameters, which as a reminder might do something like:

def make_hyper_model(hp: kerastuner.HyperParameters):
   model = keras.Sequential([
       Dense(hp.Int('units', min_value=8, max_value=64, step=2)
   ])
   return model

This can later be used for tuning hyperparameters by doing some smart searching through the parameter space.

What I want is some way of doing this in a composable manner without having to pass around a HyperParameters object.

Ergo:

import hyper_it  # import some module (hypothetical)

@hyper_it.perhaps_some_decorator
def helper():
    what_to_do = hyper_it.choice(['foo', 'bar'])
    return what_to_do

@hyper_it.perhaps_some_decorator
def main():
    print(helper(), hyper_it.choice([1, 3]), end=' ')

main.scan_all()  # Some method using the resulting composition of parameterized functions.
## prints: "foo 1 foo 3 bar 1 bar 3"

I suppose this could pass an object around under the hood (like the HyperParameters in the kerastuner version), but it would be best if that can be hidden from the user.

Any ideas on how to implement something like this?

question from:https://stackoverflow.com/questions/65887941/composable-parameter-iteration-in-python

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...