Keras和TensorBoard - AttributeError: 'Sequential'对象没有属性'_get_distribution_strategy'

13

我正在使用Keras,并尝试使用TensorBoard绘制日志。下面是我遇到的错误以及我使用的软件包版本列表。我无法理解为什么会出现“Sequential”对象没有属性“_get_distribution_strategy”的错误。

软件包: Keras 2.3.1 Keras-Applications 1.0.8 Keras-Preprocessing 1.1.0 tensorboard 2.1.0 tensorflow 2.1.0 tensorflow-estimator 2.1.0

模型:

model = Sequential()
    model.add(Embedding(MAX_NB_WORDS, EMBEDDING_DIM, input_shape=(X.shape[1],)))
    model.add(GlobalAveragePooling1D())
    #model.add(Dense(10, activation='sigmoid'))
    model.add(Dense(len(CATEGORIES), activation='softmax'))
    model.summary()
    #opt = 'adam'       # Here we can choose a certain optimizer for our model
    opt = 'rmsprop'
    model.compile(loss='categorical_crossentropy', optimizer=opt, metrics=['accuracy'])                  # Here we choose the loss function, input our optimizer choice, and set our metrics.

    # Create a TensorBoard instance with the path to the logs directory
    tensorboard = TensorBoard(log_dir='logs/{}'.format(time()),
                    histogram_freq = 1,
                    embeddings_freq = 1,
                    embeddings_data = X)

    history = model.fit(X, Y, epochs=epochs, batch_size=batch_size, validation_split=0.1, callbacks=[tensorboard])

错误:

C:\Users\Bruno\AppData\Local\Programs\Python\Python37\lib\site-packages\keras\callbacks\tensorboard_v2.py:102: UserWarning: The TensorBoard callback does not support embeddings display when using TensorFlow 2.0. Embeddings-related arguments are ignored.
  warnings.warn('The TensorBoard callback does not support '
C:\Users\Bruno\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\framework\indexed_slices.py:433: UserWarning: Converting sparse IndexedSlices to a dense Tensor of unknown shape. This may consume a large amount of memory.
  "Converting sparse IndexedSlices to a dense Tensor of unknown shape. "
Train on 1123 samples, validate on 125 samples
Traceback (most recent call last):
  File ".\NN_Training.py", line 128, in <module>
    history = model.fit(X, Y, epochs=epochs, batch_size=batch_size, validation_split=0.1, callbacks=[tensorboard])    # Feed in the train
set for X and y and run the model!!!
  File "C:\Users\Bruno\AppData\Local\Programs\Python\Python37\lib\site-packages\keras\engine\training.py", line 1239, in fit
    validation_freq=validation_freq)
  File "C:\Users\Bruno\AppData\Local\Programs\Python\Python37\lib\site-packages\keras\engine\training_arrays.py", line 119, in fit_loop
    callbacks.set_model(callback_model)
  File "C:\Users\Bruno\AppData\Local\Programs\Python\Python37\lib\site-packages\keras\callbacks\callbacks.py", line 68, in set_model
    callback.set_model(model)
  File "C:\Users\Bruno\AppData\Local\Programs\Python\Python37\lib\site-packages\keras\callbacks\tensorboard_v2.py", line 116, in set_model
    super(TensorBoard, self).set_model(model)
  File "C:\Users\Bruno\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\keras\callbacks.py", line 1532, in
set_model
    self.log_dir, self.model._get_distribution_strategy())  # pylint: disable=protected-access
AttributeError: 'Sequential' object has no attribute '_get_distribution_strategy'```
2个回答

14
你正在混用kerastf.keras之间的import,它们不是同一个库,这样做是不受支持的。
你应该从这两个库中选择一个进行所有的imports,要么是keras或者是tf.keras

我正在使用以下导入: from keras.preprocessing.text import Tokenizer, from keras.preprocessing.sequence import pad_sequences, from keras.models import Sequential, from keras.layers import Dense, Embedding, GlobalAveragePooling1D, from keras.models import Model, load_model, from keras.callbacks import TensorBoard。所有导入都来自keras,我想没有混合任何库。 - Bruno Taborda
@BrunoTaborda 您的回溯信息表明不同,因为最后是从keras进入tf.keras内部,也许您可以添加一个脚本,我们可以运行并重现这个问题。 - Dr. Snoopy
很遗憾我不能...有没有什么办法强制他只使用keras而不是tf.keras? - Bruno Taborda
@BrunoTaborda 抱歉,没有实际的代码无法确定。 - Dr. Snoopy
1
作为最终解决方案,我将我的导入从keras更改为tf.keras,这样就可以了。 - Bruno Taborda
在我的情况下,将注释:from tensorflow.keras.models import Sequential 替换为:import tensorflow 然后创建模型:model = tensorflow.keras.Sequential() 就像魔法一样奏效了! - Gabriel

7

看起来你的Python环境正在混合使用kerastensorflow.keras的导入。尝试像这样使用Sequential模块:

model = tensorflow.keras.Sequential()

或者将你的引入更改为以下的形式:

import tensorflow
layers = tensorflow.keras.layers
BatchNormalization = tensorflow.keras.layers.BatchNormalization
Conv2D = tensorflow.keras.layers.Conv2D
Flatten = tensorflow.keras.layers.Flatten
TensorBoard = tensorflow.keras.callbacks.TensorBoard
ModelCheckpoint = tensorflow.keras.callbacks.ModelCheckpoint

...etc


1
我进行了以下更改: 将from tensorflow.keras.models import Sequential注释掉,并替换为import tensorflow然后按照您的建议创建模型: model = tensorflow.keras.Sequential()非常顺利! - Gabriel

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接