Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggest a better method to provide the training data and its labels to model.fit() #705

Closed
itsSrijan opened this issue Jul 7, 2022 · 1 comment

Comments

@itsSrijan
Copy link

I have two variables called timesteps and nosamples which I am hyper tuning apart from others. And my input shape to the model depends on these two values. Hence, for every new pair of values of these, I have to change the shape of my training data and its label. To overcome this issue, I get the training data and its label inside build_model() and return them along with the model.

def build_model(hp):
  timesteps = hp.Choice('timesteps', [2, 4, 8, 16], ordered = False)
  nosamples = hp.Choice('nosample', [ 32, 64, 128], ordered = False)
  .......
  train, train_label, _, _ = train_test_splitting(DFS, UFS)

  model = keras.Sequential()
  model.add(InputLayer(input_shape = (timesteps, nosamples)))
........
  return model, train, train_label

I am providing train and train_label to model.fit() this way

class MyTuner(RandomSearch):
  def run_trial(self, trial, *args, **kwargs):
    hp = trial.hyperparameters
    model, train, train_label = self.hypermodel.build(hp)
    kwargs['batch_size'] = hp.Choice('batch_size', [16, 32, 64, 128])
    return self.hypermodel.fit(hp, model, train, train_label, *args, **kwargs)

I know the best way to tune batch_size is this, but I couldn't understand how to use it for train and train_label.

The method I employed is working fine but I think there is a more efficient way to do the same that I don't know. Any help would be welcome.

Thanks for the patience to read till here.

@haifeng-jin
Copy link
Collaborator

You can access the training data in HyperModel.fit().
It should be something like this:

class MyHyperModel(HyperModel):
  def build(self, hp):
    timesteps = hp.Choice('timesteps', [2, 4, 8, 16], ordered = False)
    nosamples = hp.Choice('nosample', [ 32, 64, 128], ordered = False)
    ...
    return model
  def fit(self, hp, model, DFS, UFS):
    timesteps = hp.get('timesteps')
    nosamples = hp.get('nosample')
    strain, train_label, _, _ = train_test_splitting(DFS, UFS)
    ...

...
tuner.search(DFS, UFS)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants