0

I am tunning this cnn-lstm network, and want to make it so that if an error occurs during the process the combination is skipped. somebody else had this problem,https://stackoverflow.com/questions/63927011/how-to-skip-problematic-hyperparameter-combinations-when-tuning-models-using-ker, however, when i tried doing this, nothing happens because the error doesn't occur during the build process it occurs during the search procces.

    build_model(hp):
    
    model = Sequential()

    for i in range(hp.Int("num_layers", 1, 5)):
        
            model.add(TimeDistributed(Conv1D(
                filters=hp.Int(f"filters{i}", min_value=32, max_value=4096,      step=32),
                kernel_size=hp.Int(f"kernel_size{i}", min_value=5, max_value=10, step=1),                           
                        activation=hp.Choice("activation", ["relu", "tanh"]),
                        input_shape=(None, n_steps,n_features))))
    
    
        

    model.add(TimeDistributed(MaxPooling1D())) 
    model.add(TimeDistributed(Flatten())) 
    
    for i in range(hp.Int("num_layers2", 1, 5)):
        model.add(LSTM(units=hp.Int(f"units{i}", min_value=32, max_value=4096, step=32), 
               activation=hp.Choice("activation2", ["relu", "tanh"]),return_sequences=(True)))
    model.add(Dense(1))
    model.compile(
        optimizer="adam", loss="mae",metrics=['mae'])
   
    
    

 
    
    return model
    



    tuner = keras_tuner.BayesianOptimization(
      hypermodel=build_model,
      objective='val_mae',
      max_trials=100,
    
      alpha=0.0001,
      beta=2.6,
      overwrite=(True)
      )


   print(tuner.search_space_summary())

   tuner.search(trainX, trainY, epochs=1, validation_data=(testX, testY))

Here is the error i am reaciving.

`ValueError: in user code:

File "/Users/benjaminschwartz/opt/anaconda3/lib/python3.9/site-packages/keras/engine/training.py", line 1160, in train_function  *
    return step_function(self, iterator)
File "/Users/benjaminschwartz/opt/anaconda3/lib/python3.9/site-packages/keras/engine/training.py", line 1146, in step_function  **
    outputs = model.distribute_strategy.run(run_step, args=(data,))
File "/Users/benjaminschwartz/opt/anaconda3/lib/python3.9/site-packages/keras/engine/training.py", line 1135, in run_step  **
    outputs = model.train_step(data)
File "/Users/benjaminschwartz/opt/anaconda3/lib/python3.9/site-packages/keras/engine/training.py", line 993, in train_step
    y_pred = self(x, training=True)
File "/Users/benjaminschwartz/opt/anaconda3/lib/python3.9/site-packages/keras/utils/traceback_utils.py", line 70, in error_handler
    raise e.with_traceback(filtered_tb) from None
File "/Users/benjaminschwartz/opt/anaconda3/lib/python3.9/site-packages/keras/layers/convolutional/base_conv.py", line 347, in compute_output_shape
    raise ValueError(

ValueError: Exception encountered when calling layer "sequential" "                 f"(type Sequential).

One of the dimensions in the output is <= 0 due to downsampling in conv1d. Consider increasing the input size. Received input shape [None, 3, 6] which would produce output shape with a zero or negative value in a dimension.

Call arguments received by layer "sequential" "                 f"(type Sequential):
  • inputs=tf.Tensor(shape=(None, 1, 3, 6), dtype=float32)
  • training=True
  • mask=None

I know why the kernel is way to high and it will never run, however, was just trying to figure out how to get this to work. Based off of trial and error the error occurs during the tuner.search(stuff goes here). However putting the try and except around this dosn't continue to run after it fails once. How do i get it to continue running after a fail and just continue going. Thank you.

PS the code looks formatted really bad. I tried spacing down and indenting however, it still looks like this. I am verry sorry.

Edit:

The solloutiont that i found is

try:
        print('test1')
        prediction = model(np.array(trainX),training=False)
        print('test2')
        
except ValueError:
        
        model = invalid_model()

this is doing the same thing the other one was doing just now during the prediction inside of the build class. I use mode(stuff) instead of model.predict because it saves time according to keras documentation. hope this is helpful.

BEn
  • 5
  • 4

0 Answers0