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

Created on 30 May 2018  路  12Comments  路  Source: keras-team/keras

Environment: Anaconda 5.1, Python 3.6, Ubuntu 16.04

Ran code under the following versions separately:
a) Tensorflow is 1.8. Using tensorflow.python.keras.

b) Standalone package. keras.__version__: 2.1.6

The code runs on Keras (keras.__version__: 2.1.6), but when I ran it under tensorflow.python.keras package, I got this error:

This is the model generator:
`# Model definition

def create_model0():
model = Sequential()
model.add(Dense(16, input_shape=(X_train_seq.shape[1],)))
model.add(Dense(1)) # default activation is linear
model.compile(optimizer="Adam", loss="mean_squared_error", metrics=["mse", "mae"])
return model
`

Here is the code causing the error when using Tensorflow version 1.8. and tensorflow.python.keras:
`#create model

model0 = create_model0()
print(model0.summary())

temp disabled for now.

SVG(model_to_dot(model0).create(prog='dot', format='svg'))`

Here is the error:
`---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
in ()
4
5 #temp disabled for now.
----> 6 SVG(model_to_dot(model0).create(prog='dot', format='svg'))
7 # ~/anaconda3/envs/tflowpy36a51/lib/python3.6/site-packages/tensorflow/python/keras/_impl/keras/utils/vis_utils.py in model_to_dot(model, show_shapes, show_layer_names, rankdir)
8 # 80 if not model.built:

~/anaconda3/envs/tflowpy36a51/lib/python3.6/site-packages/tensorflow/python/keras/_impl/keras/utils/vis_utils.py in model_to_dot(model, show_shapes, show_layer_names, rankdir)
80 if not model.built:
81 model.build()
---> 82 model = model.model
83 layers = model.layers
84

AttributeError: 'Sequential' object has no attribute 'model'`

Note: I have manually inserted export for model_to_dot() when using tensorflow.python.keras package.

I thought the Keras package inside Tensorflow is the same as the standalone Keras.
But it looks like it is not! Correct?

What is causing this error when using tensorflow.python only?
Repeat: No such error when using standalone keras.

Most helpful comment

pip install tensorflow or pip install keras should get you the most stable version of each release, e.g. tensorflow==1.8.0 or keras==2.1.6.

That being said, the plotting fix isn't released in an official release yet.

When TensorFlow ported the Keras API, they got rid of some of the deprecated things, such as models.models in Sequential API. However, the plotting was not hit with a test.

All 12 comments

Hi Brian:

There is still a problem.
Code fragment from # ~/anaconda3/envs/tflowpy36a51/lib/python3.6/site-packages/tensorflow/python/keras/_impl/keras/utils/vis_utils.py in model_to_dot(model, show_shapes, show_layer_names, rankdir)
in model_to_dot(model, show_shapes, show_layer_names, rankdir)
... ...
if isinstance(model, Sequential):
if not model.built:
model.build()
# model = model.model #5/31/2018: Obsolete. Will be remove in next TF.pythonKeras release.
layers = model.layers <=== what do you replace for model.layers with now?
... ...

Thank you.
========================= =============================
Side topic:
How do you update to latest release (nightly builds?) for either standalone keras or tensorflow.python.keras package from within anaconda's environment. Meaning after doing command:

$ source activate

then update like this:
conda update conda
conda update anaconda
or
conda update Keras
conda update tensorflow-gpu

but I suspect the above commands only update the stable releases
Is there a more targeted command just for most recent Keras or tensorflow.python.keras package?

To answer the side topic:

TensorFlow is known to not play well with conda install the newest versions of virtualenv play nicely with pip, you should be able to do pip install tf-nightly see (https://pypi.org/project/tf-nightly/)

Gimme a second, and I will try to debug the plotting and open source fix it if needed.

@briannemsick is my personal github. @brge17 is my work.

Hi Brian:
>>> "Gimme a second, and I will try to debug the plotting and open source fix it if needed."
Oops. No need to do any debug.
===>>>> My bad!!!!
Commenting out ONLY model.model works.
This should be left alone:
layers = model.layers

Sorry.
All is good now.

But I have questions (see above) on how to update to latest release.

pip install tensorflow or pip install keras should get you the most stable version of each release, e.g. tensorflow==1.8.0 or keras==2.1.6.

That being said, the plotting fix isn't released in an official release yet.

When TensorFlow ported the Keras API, they got rid of some of the deprecated things, such as models.models in Sequential API. However, the plotting was not hit with a test.

tf.keras follows the same release cycle as tensorflow...

You can use conda install -c hesi_m keras to install Keras-2.2.0 in accompany with Tensorflow-1.8.0 for CPU.

Thank you VanitarNordic.
What about installing latest version on Tensorflow-1.8.0 for GPU?

not yet for the GPU, because Tensorflow-GPU was not generating consistent results by all means

I'll do as soon as possible if I had time

Hi Brian:
You wrote:

"To answer the side topic:
TensorFlow is known to not play well with conda install the newest versions of virtualenv play nicely with pip, you should be able to do pip install tf-nightly see (https://pypi.org/project/tf-nightly/)"

I assume when you already have TensorFlow 1.8.0 in your environment, installing
nightly build into same environment would cause conflicts if we want to use an older version of TF later since the Python import command does not allow us to specify a desired TensorFlow version to use, correct?

I guess the alternative solution is to install TensorFlow nightly built in a new or separate environment, correct?

Thank you.

Another side topic:
While using TF.Keras, when you use SVG, like so:

SVG(model_to_dot(model1).create(prog='dot', format='svg'))

to draw the flow chart for the model, the first (or top most) block (or box), which is the input, has a strange set of numbers inside the block.

But when using the _standalone_ Keras, this same block has "input" written in it as oppose to a series of numbers.

Does this bug sounds familiar?

You can just pip install TensorFlow 1.9.0-rc0 and you should be good.

I haven't actually looked at the plots tbh. It just failed a unit test in our stack that asserts they do exist, not what they look like (shrug)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

somewacko picture somewacko  路  3Comments

Imorton-zd picture Imorton-zd  路  3Comments

farizrahman4u picture farizrahman4u  路  3Comments

snakeztc picture snakeztc  路  3Comments

amityaffliction picture amityaffliction  路  3Comments