“顺序”对象没有属性“ _get_distribution_strategy”

时间:2020-03-02 14:10:04

标签: python tensorflow keras callback cnn

从昨天开始我有一个问题,我不明白为什么。我在这里读过很多类似的主题,但是我没有找到解决方案。

我的进口商品如下:

import numpy as np
import librosa.display
import utils
import librosa
import os
import keras

from keras.callbacks import TensorBoard
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPooling2D, BatchNormalization
from keras.utils import to_categorical
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report

我的模型是:

    model = keras.Sequential()

    model.add(Conv2D(32, kernel_size=(2, 2), activation='relu', input_shape=input_shape))
    model.add(BatchNormalization())

    model.add(Conv2D(48, kernel_size=(2, 2), activation='relu'))
    model.add(BatchNormalization())

    model.add(Conv2D(120, kernel_size=(2, 2), activation='relu'))
    model.add(BatchNormalization())

    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(Dropout(0.25))

    model.add(Flatten())

    model.add(Dense(128, activation='relu'))
    model.add(BatchNormalization())
    model.add(Dropout(0.25))
    model.add(Dense(64, activation='relu'))
    model.add(BatchNormalization())
    model.add(Dropout(0.4))
    model.add(Dense(num_classes, activation='softmax'))
    model.compile(loss=keras.losses.categorical_crossentropy,
                  optimizer=keras.optimizers.Adadelta(),
                  metrics=['accuracy'])

    return model 

最后:

keras_callback = keras.callbacks.TensorBoard(log_dir='./Graph',
                                             histogram_freq=1,
                                             write_graph=True,
                                             write_images=True)

cnn_model.fit(X_train,
              y_train,
              batch_size=64,
              epochs=1,
              verbose=1,
              validation_split=0.1,
              callbacks=[keras_callback])

我的错误:

AttributeError                            Traceback (most recent call last)
<ipython-input-31-e1e874d24f0c> in <module>
     11               verbose=1,
     12               validation_split=0.1,
---> 13               callbacks=[keras_callback])

c:\users\antoine\appdata\local\programs\python\python37\lib\site-packages\keras\engine\training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, validation_freq, max_queue_size, workers, use_multiprocessing, **kwargs)
   1237                                         steps_per_epoch=steps_per_epoch,
   1238                                         validation_steps=validation_steps,
-> 1239                                         validation_freq=validation_freq)
   1240 
   1241     def evaluate(self,

c:\users\antoine\appdata\local\programs\python\python37\lib\site-packages\keras\engine\training_arrays.py in fit_loop(model, fit_function, fit_inputs, out_labels, batch_size, epochs, verbose, callbacks, val_function, val_inputs, shuffle, initial_epoch, steps_per_epoch, validation_steps, validation_freq)
    117         callback_metrics += ['val_' + n for n in model.metrics_names]
    118 
--> 119     callbacks.set_model(callback_model)
    120     callbacks.set_params({
    121         'batch_size': batch_size,

c:\users\antoine\appdata\local\programs\python\python37\lib\site-packages\keras\callbacks\callbacks.py in set_model(self, model)
     66         self.model = model
     67         for callback in self.callbacks:
---> 68             callback.set_model(model)
     69 
     70     def _call_batch_hook(self, mode, hook, batch, logs=None):

c:\users\antoine\appdata\local\programs\python\python37\lib\site-packages\keras\callbacks\tensorboard_v2.py in set_model(self, model)
    114         """Sets Keras model and writes graph if specified."""
    115         model.run_eagerly = False
--> 116         super(TensorBoard, self).set_model(model)

c:\users\antoine\appdata\local\programs\python\python37\lib\site-packages\tensorflow_core\python\keras\callbacks.py in set_model(self, model)
   1530     # possibly distributed settings.
   1531     self._log_write_dir = distributed_file_utils.write_dirpath(
-> 1532         self.log_dir, self.model._get_distribution_strategy())  # pylint: disable=protected-access
   1533 
   1534     with context.eager_mode():

AttributeError: 'Sequential' object has no attribute '_get_distribution_strategy'

我正在使用Tensorboard 2.1.0,tensorflow 2.1.0,Keras 2.3.1

谢谢,您可以问我是否需要更多详细信息!

1 个答案:

答案 0 :(得分:0)

在使用keras的任何地方都可以加上'tf'前缀并导入张量流吗?

  1. 添加到代码

    将tensorflow导入为tf

  2. 无论在哪里找到keras,都以'tf'作为前缀。例如:

来自

from keras.callbacks import TensorBoard
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPooling2D, BatchNormalization
from keras.utils import to_categorical

收件人

from tf.keras.callbacks import TensorBoard
from tf.keras.models import Sequential
from tf.keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPooling2D, BatchNormalization
from tf.keras.utils import to_categorical