属性错误:模块“keras.utils”没有属性“Sequence”

kuhbmx9i  于 2023-10-19  发布在  其他
关注(0)|答案(8)|浏览(223)
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

完整代码

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))
hgncfbus

hgncfbus1#

我也有同样的问题。我的keras版本是2.7.0我的tensorflow版本是2.7.0

keras.utils.Sequence

对我不起作用。您可以使用

keras.utils.all_utils.Sequence

相反

nukf8bse

nukf8bse2#

keras==2.4.0和tensorflow==2.3.0的较新版本如下所示。
更换:

from keras.utils import np_utils

from keras import utils as np_utils
yfjy0ee7

yfjy0ee73#

我在Keras 2.4.3中遇到了同样的错误。写作时

from keras import utils

from keras.utils import to_categorical

解决:

from keras.utils import np_utils

显然,这是从版本到版本的变化。

cdmah0mi

cdmah0mi4#

适用于Keras版本2.5.0和TF版本2.5.0

from tensorflow.keras.utils import to_categorical

和使用

keras.utils.to_categorical()
egdjgwm8

egdjgwm85#

使用keras==2.2.4和tensorflow==1.14.0运行上述代码。
没有错误。
升级TensorFlow应该可以解决这个问题。Cheers:)

gpfsuwkq

gpfsuwkq6#

我正在运行Tensorflow 2.5.0版。通过反复试验,我发现keras.utils.np_utils可以工作。我猜他们在一些更新中把它移到了np_utils中,所以.to_categorical就可以正常工作了。
将“np_utils.to_categorical”更改为“keras.utils.np_utils.to_categorical”

uplii1fm

uplii1fm7#

我正在运行Tensorflow 2.6.0版。通过反复试验,我发现tf.keras.utils.to_categorical(y_train, num_classes)可以工作。

xfyts7mz

xfyts7mz8#

最新版本的Keras for Tensorflow需要更改:

tf.keras.Sequential()

keras.Sequential()

相关问题