Keras: AttributeError: 'model' object has no attribute 'layers'

Created on 17 Apr 2019  路  4Comments  路  Source: keras-team/keras

Please make sure that this is a Bug or a Feature Request and provide all applicable information asked by the template.
If your issue is an implementation question, please ask your question on StackOverflow or on the Keras Slack channel instead of opening a GitHub issue.

System information

  • Have I written custom code (as opposed to using example directory):
  • OS Platform and Distribution (e.g., Linux Ubuntu 16.04):
  • TensorFlow backend (yes / no):
  • TensorFlow version:
  • Keras version:
  • Python version:
  • CUDA/cuDNN version:
  • GPU model and memory:

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

Describe the current behavior

Describe the expected behavior

Code to reproduce the issue
Provide a reproducible test case that is the bare minimum necessary to generate the problem.

Other info / 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.

tensorflow awaiting response support

Most helpful comment

You can try accessing the layer using this, model.layers[i].output where [i] is the index of the layer. Take a look at following toy example;

import tensorflow as tf
mnist = tf.keras.datasets.mnist

(x_train, y_train),(x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0

model = tf.keras.models.Sequential([
  tf.keras.layers.Flatten(input_shape=(28, 28)),
  tf.keras.layers.Dense(512, activation=tf.nn.relu),
  tf.keras.layers.Dropout(0.2),
  tf.keras.layers.Dense(10, activation=tf.nn.softmax)
])

model.layers[0].output

Output:

<tf.Tensor 'flatten_15/Reshape:0' shape=(?, 784) dtype=float32>

All 4 comments

I am trying to get individual layer information using model.layers, but keep getting this error message. Is there any other way through which I can get access to individual layers of keras functional model. The model has also some custom layers in it. model.summary() works fine.

You can try accessing the layer using this, model.layers[i].output where [i] is the index of the layer. Take a look at following toy example;

import tensorflow as tf
mnist = tf.keras.datasets.mnist

(x_train, y_train),(x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0

model = tf.keras.models.Sequential([
  tf.keras.layers.Flatten(input_shape=(28, 28)),
  tf.keras.layers.Dense(512, activation=tf.nn.relu),
  tf.keras.layers.Dropout(0.2),
  tf.keras.layers.Dense(10, activation=tf.nn.softmax)
])

model.layers[0].output

Output:

<tf.Tensor 'flatten_15/Reshape:0' shape=(?, 784) dtype=float32>

Thanks @ymodak model.layers[0].output solved my problem

Are you satisfied with the resolution of your issue?
Yes
No

Was this page helpful?
0 / 5 - 0 ratings