keras-team / keras-tuner

A Hyperparameter Tuning Library for Keras
https://keras.io/keras_tuner/
Apache License 2.0
2.86k stars 396 forks source link

Is it possible to send through hp additional parameters to the model #350

Open overg123 opened 4 years ago

overg123 commented 4 years ago

I have a model that I use for training. It basic structure look like this:

My_model(image_size=(img_height, img_width, img_channels ), n_classes=n_classes, mode=mode_net, ....

I want to use the Keras Tuner, however, the model can only receive the hp parameter. Is it possible to send those parameters inside hp? or in addition to it?

something like that:

My_model( hp, image_size=(img_height, img_width, img_channels ), n_classes=n_classes, mode=mode_net, ....

Or the only way is to use the HyperModel Class?

ben-arnao commented 4 years ago

What do you mean by "sending" parameters to the model. I'm not sure if this is what you're asking but i believe tuner.search() takes arguments that you can access later in run_trial() where you define your own customer model scoring logic. If you don't implement run_trial and use the default logic then any kwargs you pass in search() automatically go to fit()

overg123 commented 4 years ago

I mean that it seems that Keras Tuner can only work in the following structure:

def My_model(hp): .. ,,, ,,,

tuner = kt.tuners.BayesianOptimization(My_model,objective='val_loss', max_trials=max_trials,directory=

can not do something like that: def My_model(hp, my _parameters1, my_paramters2): ..... ....

tuner = kt.tuners.BayesianOptimization(My_model, my _parameters1, my_paramters2, objective='val_loss',

So in case that you can't do it, can I insert those parameters somehow in the hp structure? something like this:

def My_model(hp): .. value1 = hp.my_parameters1 value2 = hp.my_paramters2 ,,, ,,,

tuner = kt.tuners.BayesianOptimization(My_model,objective='val_loss', max_trials=max_trials,directory=

ben-arnao commented 4 years ago

@ben-arnao I'm not sure if there is a way to pass parameters outside of KT into your build_model function. What is your use case? seems a bit odd why you would do this.

overg123 commented 4 years ago

I would prefer to use my old 1000 and above lines of code function, then transferring all the code and the files around it for a class format.

umtek12 commented 4 years ago

@ben-arnao @overg123 did you ever solve this problem? I am wanting to do this exact thing and im having immense difficulty.

overg123 commented 4 years ago

@ben-arnao @overg123 did you ever solve this problem? I am wanting to do this exact thing and im having immense difficulty.

Yes, I did solve this problem, and everything is working fine. I created a class and it worked perfectly, I will be glad to help you solve this problem. Just give me more details and I will try to do it.

umtek12 commented 4 years ago

@overg123 I made an issue post an hour or so ago. Its simplified so as too not get too complicated to understand. https://github.com/keras-team/keras-tuner/issues/402

Thank you

relativeflux commented 4 years ago

@overg123 If I understand you correctly your main motivation is to avoid having to rewrite your model in the format required by the Keras Tuner build function... Indeed this is something I am struggling with. Surely most users would have existing model code they would like to tune, but I have yet to find an example of anything other than a completely fresh model being defined. I had hoped it would be just a case of passing hyperparameters in at my model's top level, so they can simply be passed down to the internal layers... but I fear this won't work.