3

I am trying to add layers to a Sequential model to train it with the fashion_mnist data. However, I am receiving this error repeatedly.

#import statements

import tensorflow as tf

from tensorflow import keras

import matplotlib.pyplot as plt

from tensorflow.keras import models


#Creating a sequential model
model=models.Sequential([tf.keras.layers.Flatten(),tf.keras.layers.Dense(128,activation=tf.nn.relu),tf.keras.layers.Dense(10,activation=tf.nn.softmax)])

#Compiling the model with optimizer and loss function
model.compile(optimizer='tf.train.AdamOptimizer',loss='sparse_categorical_crossentropy')

The error:

TypeError                                 Traceback (most recent call last)
<ipython-input-4-ffa2750d675a> in <module>()
      1 #creating a sequential model
----> 2 model=models.Sequential([tf.keras.layers.Flatten(),tf.keras.layers.Dense(128,activation=tf.nn.relu),tf.keras.layers.Dense(10,activation=tf.nn.softmax)])

c:\users\admin\appdata\local\programs\python\python36\lib\site-packages\keras\models.py in __init__(self, layers, name)
    439         if layers:
    440             for layer in layers:
--> 441                 self.add(layer)
    442 
    443     def add(self, layer):

c:\users\admin\appdata\local\programs\python\python36\lib\site-packages\keras\models.py in add(self, layer)
    458             raise TypeError('The added layer must be '
    459                             'an instance of class Layer. '
--> 460                             'Found: ' + str(layer))
    461         if not self.outputs:
    462             # First layer in model: check that it is an input layer.

TypeError: The added layer must be an instance of class Layer. Found: <tensorflow.python.keras.layers.core.Flatten object at 0x00000254C648FE48>

Can anyone help me out with this please?

Nayan Barde
  • 41
  • 1
  • 6

2 Answers2

1

Please show us the import statements at the top. If you used

from keras import models

then this error is expected as you are adding tf.keras layers to keras model.

Unfortunately, keras and tf.keras are not compatible.

If you change the import to:

from tensorflow.keras import models

then your code should work.

mostafa.elhoushi
  • 3,758
  • 1
  • 17
  • 10
  • I have added the import statements in the question itself, can you please have a look! – Nayan Barde May 14 '19 at 11:12
  • Hmmm... this looks strange... you seem to be importing everything from `tensorflow.keras` but looking at the source file paths in the error log (e.g., python\python36\lib\site-packages\keras\models.py), it seems that the code is dealing with Keras. In your code, can you replace `models.Sequential(...` with `tf.keras.moels.Sequential(...` and see what happens? – mostafa.elhoushi May 14 '19 at 19:12
1

I found the similar error in my code:

import tensorflow as tf
from keras import Sequential, Embedding
model = Sequential(name='embedding')
model.add(Embedding(2, 2, input_length=7))
model.compile('rmsprop', 'mse')
model.predict(np.array([[0, 1, 0, 1, 1, 0, 0]]))

Then I found this article about tensorflow 2.0 and keras.

In this article, the author said:

Now that TensorFlow 2.0 is released both keras and tf.keras are in sync, implying that keras and tf.keras are still separate projects; however, developers should start using tf.keras moving forward as the keras package will only support bug fixes.

Then I changed my code and everything was okay

import tensorflow as tf
import tensorflow.keras as k
from tensorflow.keras.layers import Embedding
import numpy as np

model: k.Sequential = k.Sequential(name='embedding')
em: Embedding = Embedding(2, 2, input_length=7)
model.add(em)
model.compile('rmsprop', 'mse')
model.predict(np.array([[0, 1, 0, 1, 1, 0, 0]]))
print("model.weights: ", model.weights)
AlohaWorld
  • 51
  • 1
  • 6