13

I am using keras and trying to plot the logs using tensorboard. Bellow you can find out the error I am getting and also the list of packages versions I am using. I can not understand it is giving me the error of 'Sequential' object has no attribute '_get_distribution_strategy'.

Package: 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:

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

ERROR:

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'```
Pavel Fedotov
  • 748
  • 1
  • 7
  • 29
Bruno Taborda
  • 155
  • 1
  • 1
  • 8

2 Answers2

14

You are mixing imports between keras and tf.keras, they are not the same library and doing this is not supported.

You should make all imports from one of the libraries, either keras or tf.keras.

Dr. Snoopy
  • 55,122
  • 7
  • 121
  • 140
  • I am using the following imports: 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. All are coming from keras and not mixing any libraries I guess. – Bruno Taborda Jan 24 '20 at 10:40
  • @BrunoTaborda Your traceback says otherwise, as at the end is going from keras to inside tf.keras, maybe you can add a script that we can run and reproduces the problem. – Dr. Snoopy Jan 24 '20 at 10:47
  • Unfortunately I can not... is there any way to just force him to use keras and not tf.keras? – Bruno Taborda Jan 24 '20 at 11:19
  • @BrunoTaborda Sorry, impossible to say without actual code. – Dr. Snoopy Jan 24 '20 at 12:10
  • 1
    As a final solution I changed my imports from keras to tf.keras it worked. – Bruno Taborda Jan 24 '20 at 14:29
  • In my case, commenting: ```from tensorflow.keras.models import Sequential``` and replaced it by: ```import tensorflow``` Then to create the model: ```model = tensorflow.keras.Sequential()``` worked like a charm! – Gabriel Jun 24 '21 at 14:26
7

It seems that your python environment is mixing imports from keras and tensorflow.keras. Try to use Sequential module like this:

model = tensorflow.keras.Sequential()

Or change your imports to something like

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

Rocío García Luque
  • 3,597
  • 31
  • 31
  • 1
    I commented: ```from tensorflow.keras.models import Sequential``` and replaced it by: ```import tensorflow``` Then to create my model I did just like you said: ```model = tensorflow.keras.Sequential()``` worked like a charm! – Gabriel Jun 24 '21 at 14:23