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

KeyError: 'batch_input_shape' while using deeplift on keras #118

Open
AmirHoseinSafari opened this issue Dec 4, 2020 · 0 comments
Open

Comments

@AmirHoseinSafari
Copy link

Hi,

I am using Keras (V 2.2.4) and my model is LRCN (a combination of CNN + LSTM).
You can see my code here:
https://github.com/AmirHoseinSafari/LRCN-drug-resistance/blob/master/models/Bayesian_optimizer.py
I am trying to run it this way:

   X_train2 = np.array(X_train).astype(np.float)
   X_test2 = np.array(X_test).astype(np.float)
   X_val2 = np.array(X_val).astype(np.float)

   y_train2 = np.array(y_train).astype(np.float)
   y_test2 = np.array(y_test).astype(np.float)
   y_val2 = np.array(y_val).astype(np.float)
  model.compile(
       loss=masked_loss_function,
       optimizer='Adam',
       metrics=[masked_accuracy]
   )
   history = model.fit(
       X_train2,
       y_train2,
       epochs=epochs,
       batch_size=128,
       verbose=2,
       validation_data=(X_val2, y_val2)
   )

  model.save('model_save.h5')
import deeplift
from deeplift.conversion import kerasapi_conversion as kc

deeplift_model = \
    kc.convert_model_from_saved_files(
        'model_save.h5',
        nonlinear_mxts_mode=deeplift.layers.NonlinearMxtsMode.DeepLIFT_GenomicsDefault)

find_scores_layer_idx = 0

deeplift_contribs_func = deeplift_model.get_target_contribs_func(
    find_scores_layer_idx=find_scores_layer_idx,
    target_layer_idx=-1)

scores = np.array(deeplift_contribs_func(task_idx=0,
                                         input_data_list=[X],
                                         batch_size=10,
                                         progress_update=1000))`

My model is something like:

model = Sequential()
model.add(Dropout(dropout2_rate))
model.add(Conv1D(filters=filterCNN1, kernel_size=kernelCNN1, activation='relu', padding='same'))
model.add(MaxPooling1D(pool_size=poolCNN1, padding='same'))
model.add(LSTM(LSTM1, return_sequences=False, recurrent_dropout=0.3))
model.add(Dropout(dropout2_rate))
model.add(Dense(dense_1_neurons))
model.add(Dropout(dropout2_rate))
model.add(Dense(12, activation='sigmoid'))

I am getting this error (at convert_model_from_saved_files function) and I couldn't find any way to resolve it. Could you please help me with this?

Here is the error:

Traceback (most recent call last):
File "/Users/amir/PycharmProjects/Lab/LSTM-DR/LSTM-drug-resistance/pipe_line_gene.py", line 31, in
train()
File "/Users/amir/PycharmProjects/Lab/LSTM-DR/LSTM-drug-resistance/pipe_line_gene.py", line 14, in train
model_gene_based.run_bayesian(df_train, labels)
File "/Users/amir/PycharmProjects/Lab/LSTM-DR/LSTM-drug-resistance/models/model_gene_based.py", line 578, in run_bayesian
Bayesian_optimizer.BO(X_train, X_test, X_val, y_train, y_test, y_val, limited, portion)
File "/Users/amir/PycharmProjects/Lab/LSTM-DR/LSTM-drug-resistance/models/Bayesian_optimizer.py", line 409, in BO
fit_with_partial(dropout2_rate=0.2, dense_1_neurons_x128=1, dense_2_neurons_x128=1, dense_3_neurons_x128=1,
File "/Users/amir/PycharmProjects/Lab/LSTM-DR/LSTM-drug-resistance/models/Bayesian_optimizer.py", line 191, in fit_with
return run_one_fold(model)
File "/Users/amir/PycharmProjects/Lab/LSTM-DR/LSTM-drug-resistance/models/Bayesian_optimizer.py", line 304, in run_one_fold
kc.convert_model_from_saved_files(
File "/Users/amir/venv/p3/lib/python3.8/site-packages/deeplift/conversion/kerasapi_conversion.py", line 414, in convert_model_from_saved_files
return model_conversion_function(model_config=model_config, **kwargs)
File "/Users/amir/venv/p3/lib/python3.8/site-packages/deeplift/conversion/kerasapi_conversion.py", line 459, in convert_sequential_model
batch_input_shape = model_config[0]['config'][KerasKeys.batch_input_shape]
KeyError: 'batch_input_shape'

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

1 participant