Opimization of CNN for Regression Keras Tuner

306 Views Asked by At

I am using Keras Tuner to optimize a CNN model for a regression problem. Basicly I have sequences of DNA that I turned into a matrix in order to use them as images to train a CNN model. What I want to predict is a percentage that depends on those sequences.

this is my model to try:

def build_model(hp):  
  model = keras.Sequential([
    keras.layers.Conv2D(
        filters=hp.Int('conv_1_filter', min_value=32, max_value=128, step=16),
        kernel_size=hp.Choice('conv_1_kernel', values = [3,3]),
        activation='relu',
        padding = 'same',
        input_shape=(30,4,1)
    ),
    keras.layers.Conv2D(
        filters=hp.Int('conv_2_filter', min_value=32, max_value=96, step=16),
        kernel_size=hp.Choice('conv_2_kernel', values = [5,3]),
        activation='relu',
        padding = 'same'
    ),
    keras.layers.Conv2D(
        filters=hp.Int('conv_3_filter', min_value=32, max_value=64, step=16),
        kernel_size=hp.Choice('conv_3_kernel', values = [7,3]),
        activation='relu',
        padding = 'same'
    ),
    keras.layers.MaxPooling2D(
        pool_size=hp.Choice('maxpool_1_size', values = [2,2]),
    ),
    keras.layers.Flatten(),
    keras.layers.Dense(
        units=hp.Int('dense_1_units', min_value=32, max_value=128, step=16),
        activation='relu'
    ),
    keras.layers.Dense(1, activation='relu')
  ])
  
  model.compile(optimizer=keras.optimizers.Adam(hp.Choice('learning_rate', values=[1e-2, 1e-3,1e-4])),
              loss='mean_absolute_error',
              metrics=[R2_Score])
  
  return model

As I am working with regression I used this fuction for R2Score as in Keras they lack of it:

def R2_Score(y_true, y_pred):
    from keras import backend as K
    SS_res =  K.sum(K.square( y_true-y_pred ))
    SS_tot = K.sum(K.square( y_true - K.mean(y_true) ) )
    return ( 1 - SS_res/(SS_tot + K.epsilon()) )

For tunning I am using this code:

tuner=RandomSearch(build_model,
                   objective= kerastuner.Objective('R2_Score', direction='max'),
                   max_trials=5,
                   directory='output',
                   project_name="CRISPR-Cas9")

tuner.search(X_train, y_train,
             epochs=5,
             validation_data=(X_test, y_test),
             metrics=[R2_Score])

I get the error in model.compile, and I suspect that I will get the same in the tuner or tuner.search:

TypeError: fit() got an unexpected keyword argument 'metrics'

I saw some solutions for this problem but none work form me, also almost every example of CNN I see is used in clasification but I am doing a regression.

0

There are 0 best solutions below