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

AttributeError: 'Functional' object has no attribute '_layers' #15542

Closed
innat opened this issue Oct 24, 2021 · 2 comments
Closed

AttributeError: 'Functional' object has no attribute '_layers' #15542

innat opened this issue Oct 24, 2021 · 2 comments

Comments

@innat
Copy link

innat commented Oct 24, 2021

Please go to TF Forum for help and support:

https://discuss.tensorflow.org/tag/keras

If you open a GitHub issue, here is our policy:

It must be a bug, a feature request, or a significant problem with the documentation (for small docs fixes please send a PR instead).
The form below must be filled out.

Here's why we have that policy:.

Keras developers respond to issues. We want to focus on work that benefits the whole community, e.g., fixing bugs and adding features. Support only helps individuals. GitHub also notifies thousands of people when issues are filed. We want them to see you communicating an interesting problem, rather than being redirected to Stack Overflow.

System information.

  • Have I written custom code (as opposed to using a stock example script provided in Keras):
  • OS Platform and Distribution (e.g., Linux Ubuntu 16.04):
  • TensorFlow installed from (source or binary):
  • TensorFlow version (use command below):
  • Python version:
  • Bazel version (if compiling from source):
  • GPU model and memory:
  • Exact command to reproduce:

You can collect some of this information using our environment capture script:

https://github.com/tensorflow/tensorflow/tree/master/tools/tf_env_collect.sh

You can obtain the TensorFlow version with:
python -c "import tensorflow as tf; print(tf.version.GIT_VERSION, tf.version.VERSION)"

Describe the problem

Describe the problem clearly here. Be sure to convey here why it's a bug in Keras or why the requested feature is needed.

Describe the current behavior.

I want to remove the rescaling and normalization layer built inside the EfficientNets model. For that, I could do as follows in TF 2.4.1

from tensorflow.keras import applications

inputx = Input((512, 512, 3))
base = applications.EfficientNetB2(
            include_top  = False,
            weights      = None,
            input_tensor = inputx
        )

for i, l in enumerate(base.layers):
    print(l.name, l.output_shape)
    if i == 5: 
        break
    
base._layers.pop(1) # remove rescaling
base._layers.pop(1) # remove normalization

print('hhhh \n\n')
for i, l in enumerate(base.layers):
    print(l.name, l.output_shape)
    if i == 5: 
        break
    

But in TF 2.6 the above code gives the following attribution error. So, what would be the way now to kick out these built-in scaling layers?

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
/tmp/ipykernel_34/1445888425.py in <module>
     15 
     16 
---> 17 base._layers.pop(1) # remove rescaling
     18 base._layers.pop(1) # remove normalization
     19 

AttributeError: 'Functional' object has no attribute '_layers'

Describe the expected behavior.

Contributing.

  • Do you want to contribute a PR? (yes/no):
  • If yes, please read this page for instructions
  • Briefly describe your candidate solution(if contributing):

Standalone code to reproduce the issue.

Provide a reproducible test case that is the bare minimum necessary to generate
the problem. If possible, please share a link to Colab/Jupyter/any notebook.

Source code / logs.

Include any logs or source code that would be helpful to diagnose the problem. If including tracebacks, please include the full traceback. Large logs and files should be attached. Try to provide a reproducible test case that is the bare minimum necessary to generate the problem.

@sanatmpa1
Copy link
Contributor

@sachinprasadhs,

I am able to reproduce the issue reported in colab, I tried using model.layers.pop() instead of model._layers.pop() and it still returns the same layers as mentioned in point 1 on this comment. I've also tried following as per this SO thread to create a new functional model and add all the layers except rescaling and normalization ,but encountered the error ValueError: A merge layer should be called on a list of inputs. You can find the gist here. Can you please take a look on this issue? Thanks!

@sachinprasadhs sachinprasadhs added the keras-team-review-pending Pending review by a Keras team member. label Nov 4, 2021
@qlzh727 qlzh727 self-assigned this Nov 4, 2021
@qlzh727 qlzh727 removed the keras-team-review-pending Pending review by a Keras team member. label Nov 4, 2021
@qlzh727
Copy link
Member

qlzh727 commented Nov 4, 2021

I think model._layer is a private attribute and we never had any API contract for it. Even you can access it via
model.layers, I think it is generally a bad idea to update functional model network after the model is created.

You can change the sequential model by add() or pop() methods, but not for Functional model.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants