2

so i am using a CNN and i am trying to use two callbacks, one for early stopping and one for saving the weights

My code looks like this:

#building model
def build_model():
    model = models.Sequential()
    model.add(layers.InputLayer(input_shape=(100,28)))
    model.add(layers.Dense(28,activation = 'relu'))
    model.add(BatchNormalization(momentum = 0.99))
    model.add(Dropout(0.1))
    model.add(layers.Conv1D(filters=16,kernel_size=3,strides=1,padding='same',activation='relu'))
    model.add(BatchNormalization(momentum = 0.99))
    model.add(Dropout(0.1))
    model.add(layers.Conv1D(filters=32,kernel_size=3,strides=1,padding='same',activation='relu'))
    model.add(BatchNormalization(momentum = 0.99))
    model.add(Dropout(0.1))
    model.add(layers.Conv1D(filters=64,kernel_size=3,strides=1,padding='same',activation='relu'))
    model.add(BatchNormalization(momentum = 0.99))
    model.add(Dropout(0.1))
    model.add(layers.Dense(1, activation = 'linear'))

    model.compile(
        optimizer='adam',
        loss=['mean_squared_error'],
        metrics=[tf.keras.metrics.RootMeanSquaredError()]
    )
    return model

checkpoint_path = r"C:/Users/xatzo/Desktop/fz1.ckpt"
checkpoint_dir = os.path.dirname(checkpoint_path)
cp_callback = tf.keras.callbacks.ModelCheckpoint(filepath=checkpoint_path,
                                                 save_weights_only=True,
                                                 verbose=1)


model = build_model()

print(model.summary)

#train model and output
history = model.fit(
    data1,
    target_fz1,
    epochs=200,
    validation_data=(
        val_d1,
        val_tz1),
    callbacks=[keras.callbacks.EarlyStopping(
        monitor = 'val_loss',
        min_delta=0.00001,
        patience = 5),
        ProgbarLogger(count_mode='samples',stateful_metrics=None)
    ,cp_callback]
)

predict_z1=model.predict(val_d1)

but when i run it i get the following error:

<bound method Network.summary of <keras.engine.sequential.Sequential object at 0x0000013A8D7BF088>>
Train on 733 samples, validate on 366 samples
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-39-53fa487538b4> in <module>
     48         patience = 5),
     49         ProgbarLogger(count_mode='samples',stateful_metrics=None)
---> 50     ,cp_callback]
     51 )
     52 

~\Anaconda3\envs\deeplearning\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,

~\Anaconda3\envs\deeplearning\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)
    127         'metrics': callback_metrics,
    128     })
--> 129     callbacks._call_begin_hook('train')
    130     callbacks.model.stop_training = False
    131     for cbk in callbacks:

~\Anaconda3\envs\deeplearning\lib\site-packages\keras\callbacks\callbacks.py in _call_begin_hook(self, mode)
    101         """Helper function for on_{train|test|predict}_begin methods."""
    102         if mode == _TRAIN:
--> 103             self.on_train_begin()
    104         elif mode == _TEST:
    105             self.on_test_begin()

~\Anaconda3\envs\deeplearning\lib\site-packages\keras\callbacks\callbacks.py in on_train_begin(self, logs)
    217         """
    218         for callback in self.callbacks:
--> 219             callback.on_train_begin(logs)
    220 
    221     def on_train_end(self, logs=None):

~\Anaconda3\envs\deeplearning\lib\site-packages\tensorflow_core\python\keras\callbacks.py in on_train_begin(self, logs)
    925   def on_train_begin(self, logs=None):
    926     # pylint: disable=protected-access
--> 927     if self.model._in_multi_worker_mode():
    928       # MultiWorkerTrainingState is used to manage the training state needed
    929       # for preemption-recovery of a worker in multi-worker training.

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

Here is also code with all my imports, in case i missed smth there:

import pandas as pd
import scipy as sp
from scipy import interpolate
import numpy as np
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dropout, Dense, Conv2D, MaxPool2D, Flatten
from tensorflow.keras.callbacks import ProgbarLogger
import keras.utils.np_utils as ku
import matplotlib.pyplot as plt
from keras import regularizers
import keras
import keras.models as models
import keras.layers as layers
from keras.layers import Dropout, LeakyReLU
from keras import regularizers
from keras.layers.normalization import BatchNormalization
#from keras.callbacks import ModelCheckpoint
#from tensorflow.keras.callbacks import ModelCheckpoint
import os
import glob
from numpy import genfromtxt
%matplotlib inline

the two lines that have comments have been tried but got the same error

Does anyone know anything about this?

4
  • You are mixing tf.keras and keras in your imports, that is not supported. Commented Apr 23, 2020 at 0:51
  • @MatiasValdenegro so what should I change? Commented Apr 23, 2020 at 13:21
  • I would suggest staying with tensorflow.keras. Reasons? Perhaps check this post. Commented Apr 23, 2020 at 13:24
  • You should choose one library and make all imports from it. Commented Apr 23, 2020 at 13:31

1 Answer 1

4

As suggested by Matias, Reason for this Error is that you are mixing usage of keras and tf.keras, especially in CheckPoint Callback.

To be more specific, you are building the Model using Layers imported from Keras (keras.layers.InputLayer) but you are importing ModelCheckpoint from tensorflow.keras (tf.keras.callbacks.ModelCheckpoint).

Importing both the Layers as well as ModelCheckPoint from tensorflow.keras will resolve your issue.

The modified code which should work, is shown below:

import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dropout, Dense, Conv1D, Flatten,InputLayer,BatchNormalization
from tensorflow.keras.callbacks import ProgbarLogger


def build_model():
    model = models.Sequential()
    model.add(InputLayer(input_shape=(100,28)))
    model.add(Dense(28,activation = 'relu'))
    model.add(BatchNormalization(momentum = 0.99))
    model.add(Dropout(0.1))
    model.add(Conv1D(filters=16,kernel_size=3,strides=1,padding='same',activation='relu'))
    model.add(BatchNormalization(momentum = 0.99))
    model.add(Dropout(0.1))
    model.add(Conv1D(filters=32,kernel_size=3,strides=1,padding='same',activation='relu'))
    model.add(BatchNormalization(momentum = 0.99))
    model.add(Dropout(0.1))
    model.add(Conv1D(filters=64,kernel_size=3,strides=1,padding='same',activation='relu'))
    model.add(BatchNormalization(momentum = 0.99))
    model.add(Dropout(0.1))
    model.add(Dense(1, activation = 'linear'))

    model.compile(
        optimizer='adam',
        loss=['mean_squared_error'],
        metrics=[tf.keras.metrics.RootMeanSquaredError()]
    )
    return model

checkpoint_path = r"C:/Users/xatzo/Desktop/fz1.ckpt"
checkpoint_dir = os.path.dirname(checkpoint_path)
cp_callback = tf.keras.callbacks.ModelCheckpoint(filepath=checkpoint_path,
                                                 save_weights_only=True,
                                                 verbose=1)


model = build_model()

print(model.summary)

#train model and output
history = model.fit(
    data1,
    target_fz1,
    epochs=200,
    validation_data=(
        val_d1,
        val_tz1),
    callbacks=[tf.keras.callbacks.EarlyStopping(
        monitor = 'val_loss',
        min_delta=0.00001,
        patience = 5),
        ProgbarLogger(count_mode='samples',stateful_metrics=None)
    ,cp_callback]
)

predict_z1=model.predict(val_d1)
Sign up to request clarification or add additional context in comments.

1 Comment

@Dimitris Xatzakis, Can you please accept and upvote the answer if it answers your question. Thank You

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.