0

I am working on a text classification problem and trying to use Kerastuner to identify the best configuration for my LSTM network. Below is the code for same:

keras Tuner

def build_model(hp):
  
  num_hidden_layers =1
  num_units = 8
  dropout_rate = 0.1
  learning_rate=0.01
  
  

  if hp:
    num_hidden_layers = hp.Int('num_hidden_layers', min_value=2, max_value=100, step=5)
    num_units = hp.Int('num_units', min_value=50, max_value=2000, step=50)
    dropout_rate = hp.Float('dropout_rate', min_value=0.1, max_value=0.5)
    learning_rate = hp.Float('learning_rate', min_value=0.0001, max_value=0.01)
    momentum_rate = hp.Float('momentum_rate', min_value=0.5, max_value=0.9)
    vocab_size = len(tokenizer.word_index)+1
    max_sequence_length = 500
    embedding_size = 300

  model = tf.keras.models.Sequential()
  model.add(tf.keras.layers.Embedding(input_dim=vocab_size , output_dim=embedding_size, input_length=max_sequence_length , weights=[embedding_matrix], trainable=False))
  
  
  for _ in range(0,num_hidden_layers):
    
    model.add(tf.keras.layers.LSTM(num_units))
    model.add(tf.keras.layers.Dropout(dropout_rate))

  model.add(tf.keras.layers.Dense(1, activation='sigmoid'))
  
  model.compile(
      loss = 'mse',
      optimizer =tf.keras.optimizers.SGD(learning_rate=learning_rate, momentum=momentum_rate),
      metrics = [tf.keras.metrics.BinaryCrossentropy(name='binary_crossentropy')]
  )
  return model

class CustomTuner(kerastuner.tuners.BayesianOptimization):
  def run_trial(self, trial, *args, **kwargs):
    kwargs['batch_size'] = trial.hyperparameters.Int('batch_size', 128,1024, step=32)
    super(CustomTuner, self).run_trial(trial,*args,**kwargs)

tuner = CustomTuner(
    build_model,
    objective=kerastuner.Objective('val_loss','min'),
    max_trials=2,
    executions_per_trial=1,
    directory='/dbfs/FileStore/GDPR_Dev/Data/',
    project_name = 'nn_logs_lstm_30062021',
    overwrite=True

The code fails with the below error:

ValueError: Input 0 of layer lstm_1 is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: (None, 50)

Can anyone please help me with this?

1 Answers1

0

Currently, your data is two-dimensional (N x M), however, input data should be three dimensional. To solve this you should reshape your input as to a N x M x 1 matrix as follows:

x = np.reshape(x, (x.shape[0], x.shape[1], 1))

If your input was multivariate, then the desired shape of the input would be N x M x K, where k is the number of dimensions

Andres
  • 11
  • 5