gutolin
gutolin

Reputation: 131

AttributeError: module 'keras.utils' has no attribute 'Sequence'

Traceback (most recent call last):
    File "C:\Users\gutolinPC\Desktop\tensorflow.py", line 3, in <module>
    from keras.datasets import mnist
    File "C:\Program Files\Python37\lib\site-packages\keras\__init__.py", line 3,in <module>
    from . import utils
    File "C:\Program Files\Python37\lib\site-packages\keras\utils\__init__.py", 
    line 6, in <module>
    from . import conv_utils
    File "C:\Program Files\Python37\lib\site-packages\keras\utils\conv_utils.py", 
    line 9, in <module>
    from .. import backend as K
    File "C:\Program Files\Python37\lib\site-packages\keras\backend\__init__.py", 
    line 89, in <module>
    from .tensorflow_backend import *
    File "C:\Program Files\Python37\lib\site- 
    packages\keras\backend\tensorflow_backend.py", line 5, in <module>
    import tensorflow as tf
    File "C:\Users\gutolinPC\Desktop\tensorflow.py", line 3, in <module>
    from keras.datasets import mnist
    File "C:\Program Files\Python37\lib\site- 
    packages\keras\datasets\__init__.py", line 4, in <module>
    from . import imdb
    File "C:\Program Files\Python37\lib\site-packages\keras\datasets\imdb.py", 
    line 8, in <module>
    from ..preprocessing.sequence import _remove_long_seq
    File "C:\Program Files\Python37\lib\site- 
    packages\keras\preprocessing\__init__.py", line 12, in <module>
    from . import image
    File "C:\Program Files\Python37\lib\site- 
    packages\keras\preprocessing\image.py", line 11, in <module>
    from keras_preprocessing import image
    File "C:\Program Files\Python37\lib\site- 
    packages\keras_preprocessing\image\__init__.py", line 6, in <module>
    from .dataframe_iterator import DataFrameIterator
    File "C:\Program Files\Python37\lib\site- 
    packages\keras_preprocessing\image\dataframe_iterator.py", line 10, in <module>
    from .iterator import BatchFromFilesMixin, Iterator
     File "C:\Program Files\Python37\lib\site-packages\keras_preprocessing\image\iterator.py", line 13, in <module>
    IteratorType = get_keras_submodule('utils').Sequence
    AttributeError: module 'keras.utils' has no attribute 'Sequence'

Win 10

python 3.7.0

Keras                2.2.4
Keras-Applications   1.0.7
Keras-Preprocessing  1.0.9
tensorboard          1.13.1
tensorflow           1.13.1
tensorflow-estimator 1.13.0

Full code

import numpy

from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense
from keras.utils import np_utils


numpy.random.seed(42)


(X_train, y_train), (X_test, y_test) = mnist.load_data()

X_train = X_train.reshape(60000, 784)
X_test = X_test.reshape(10000, 784)

X_train = X_train.astype('float32')
X_test = X_test.astype('float32')
X_train /= 255
X_test /= 255


Y_train = np_utils.to_categorical(y_train, 10)
Y_test = np_utils.to_categorical(y_test, 10)


model = Sequential()


model.add(Dense(800, input_dim=784, activation="relu",         
kernel_initializer="normal"))
model.add(Dense(10, activation="softmax", kernel_initializer="normal"))


model.compile(loss="categorical_crossentropy", optimizer="SGD", metrics=["accuracy"])

print(model.summary())


model.fit(X_train, Y_train, batch_size=200, epochs=25, validation_split=0.2, verbose=2)


scores = model.evaluate(X_test, Y_test, verbose=0)
print("Точность работы на тестовых данных: %.2f%%" % (scores[1]*100))

Upvotes: 12

Views: 34973

Answers (8)

Madeline Coven
Madeline Coven

Reputation: 1

The latest version of Keras for Tensorflow needed a change from:

tf.keras.Sequential()

to

keras.Sequential()

Upvotes: 0

Sarita Rajput
Sarita Rajput

Reputation: 1

I'm running Tensorflow version 2.6.0. on colab. By trial and error, I found that tf.keras.utils.to_categorical(y_train, num_classes) works.

Upvotes: 0

maryam gh
maryam gh

Reputation: 193

i had the same problem. and my keras versions is 2.7.0 and my tensorflow version is 2.7.0 and yet the line

keras.utils.Sequence

didnt work for me. you can use

keras.utils.all_utils.Sequence

instead

Upvotes: 13

Ritu verma
Ritu verma

Reputation: 11

For Keras Version- 2.5.0 and TF Version- 2.5.0

from tensorflow.keras.utils import to_categorical

and work with

keras.utils.to_categorical()

Upvotes: 1

Mounir Bouddoufi
Mounir Bouddoufi

Reputation: 1

I'm running Tensorflow version 2.5.0. By trial and error, I found that keras.utils.np_utils works. I guess they moved it into np_utils in some update, so with that .to_categorical works just fine.

change "np_utils.to_categorical" by "keras.utils.np_utils.to_categorical"

Upvotes: 0

SegaSta
SegaSta

Reputation: 51

I getting same error in Keras 2.4.3. when writing

from keras import utils

or

from keras.utils import to_categorical

Solving:

from keras.utils import np_utils

Apparenytly this changes from version to version.

Upvotes: 1

Henrique R
Henrique R

Reputation: 94

Newer versions of keras==2.4.0 and tensorflow==2.3.0 would work as follows.

Replacing:

from keras.utils import np_utils

for

from keras import utils as np_utils

Upvotes: 2

Sushanth
Sushanth

Reputation: 2342

Ran the above code by using keras==2.2.4 and tensorflow==1.14.0.

No errors.

Upgrading TensorFlow should solve the issue. Cheers :)

Upvotes: 0

Related Questions