1

I have a use case that seems difficult to represent with keras-tuner and I could use some guidance. I'm training a model with an Embeddings layer. I want to train over different sequence lengths which requires a preprocessing step. I've subclassed KerasTuner like so (omitted lots of code for brevity):

class MyTuner(kt.Tuner):
  def run_trial(self, trial):
    hp = trial.hyperparameters
    # ...
    X = keras.preprocessing.sequence.pad_sequences(tokens, 
          maxlen= hp.Int('sequence_length', min_value=100, max_value=200, step=50))
    model = self.hypermodel.build(hp)
    # ...

And I've created a build model function:

def build_model(hp):
    model = keras.models.Sequential()
    sequence_length = ???
    model.add( keras.layers.Embedding( word_index,
                                       embedding_dim,
                                       input_length=sequence_length,
                                       trainable=False ) )

My issue is with getting the sequence_length parameter in the build_model function. Things I have tried:

  • hp.get('sequence_length'). This throws NameError: name 'word_index' is not defined. Looks like hp is separate when passed into build_model and run_trial.
  • adding parameters to build_model. This doesn't work either (throws TypeError: build_model() missing 1 required positional argument)

I would appreciate some guidance on how to structure my code to use keras-tuner.

0 Answers0