question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Keras 1.2 Functional API 'GraphModel' object has no attribute 'get_layers'

See original GitHub issue

I’m using the Keras 1.2 w/ TF (dev-tf branch) functional API & updated to use kc.convert_functional_model.

Here’s the error:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-10-262bf3b052dd> in <module>()
      4 
      5 deeplift_model = revealcancel_model
----> 6 deeplift_prediction_func = compile_func([deeplift_model.get_layers()[0].get_activation_vars()],
      7                                        deeplift_model.get_layers()[-1].get_activation_vars())
      8 original_model_predictions = keras_model.predict(X_test.values, batch_size=200)

AttributeError: 'GraphModel' object has no attribute 'get_layers'

Here’s the code snippet I’m running…

...
revealcancel_model = kc.convert_functional_model(model=keras_model, nonlinear_mxts_mode=NonlinearMxtsMode.RevealCancel)

from  deeplift.util  import compile_func
import numpy as np
from keras import backend as K

deeplift_model = revealcancel_model
deeplift_prediction_func = compile_func([deeplift_model.get_layers()[0].get_activation_vars()],
                                       deeplift_model.get_layers()[-1].get_activation_vars())
original_model_predictions = keras_model.predict(X_test.values, batch_size=200)
converted_model_predictions = deeplift.util.run_function_in_batches(
                                input_data_list=[X_test.values],
                                func=deeplift_prediction_func,
                                batch_size=200,
                                progress_update=None)
print("difference in predictions:",np.max(np.array(converted_model_predictions)-np.array(original_model_predictions)))
assert np.max(np.array(converted_model_predictions)-np.array(original_model_predictions)) < 10**-5
predictions = converted_model_predictions

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5

github_iconTop GitHub Comments

2reactions
donigiancommented, Jun 23, 2018

@AvantiShri It works now.

In summary in case anyone would like to use the Keras Functional API…

  1. Run deeplift_model.get_name_to_blob().keys()
  2. Update your compile_func to use layer name, example…
deeplift_prediction_func = compile_func([deeplift_model.get_name_to_blob()["input_1"].get_activation_vars()], deeplift_model.get_name_to_blob()["dense_4"].get_activation_vars())
  1. Update get_target_contribs_func to use layer names as well, note you’ll need the pre-activation layer here…
revealcancel_func = revealcancel_model.get_target_contribs_func(find_scores_layer_name="input_1", pre_activation_target_layer_name="preact_dense_4")
1reaction
JanZrimeccommented, Oct 3, 2018

UPDATE: deeplift_model.get_name_to_layer().keys()

Read more comments on GitHub >

github_iconTop Results From Across the Web

Keras AttributeError: 'Functional' object has no attribute 'shape'
The reason why you get that error is that you need to provide the input_shape of the base_model , instead of the base_model...
Read more >
Model (functional API) - Keras 1.2.2 Documentation
Scalar test loss (if the model has a single output and no metrics) or list of scalars (if the model has multiple outputs...
Read more >
The Model class - Keras
Model groups layers into an object with training and inference features. ... 1 - With the "Functional API", where you start from Input...
Read more >
errors in merging two layers output - Google Groups
to Keras-users. Hi, dear all, I have problem about merging two layers output, ... AttributeError: 'TensorVariable' object has no attribute ...
Read more >
3 ways to create a Keras model with TensorFlow 2.0 ...
Keras and TensorFlow 2.0 provide you with three methods to implement your own neural network architectures:, Sequential API, Functional API, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found