1

I'm trying to make keras modelcheckpoint code. But Whenever I started the code, that get_config error occured. Below is my keras code.

model = keras.Sequential()
model.add(keras.layers.Flatten(input_shape=(8,)))
model.add(keras.layers.Dense(512))
model.add(LeakyReLU(alpha=0.2))
model.add(keras.layers.Dropout(0.1))
model.add(keras.layers.Dense(1))
model.compile(optimizer = keras.optimizers.Adam(learning_rate =0.0001),
            loss = keras.losses.MeanSquaredError(reduction="auto"),
            metrics = 'mae')


mc2 = keras.callbacks.ModelCheckpoint('best_model_scaled'+str(fold_no)+'.h5', 
monitor='val_loss', mode='min', save_best_only=True,verbose=1)
es=keras.callbacks.EarlyStopping(monitor='val_loss', mode='min', patience=1500)
history = model.fit(x_train2, y_train2, batch_size=16,epochs=2000, validation_data = (x_val2, y_val2), callbacks = [es,mc2])

I saw other questions and found that "def get_config(self):" code solves the problem. But I have no "Class():" code for "get_config(self)". Please help me solve the problem. Thanks!

김기완
  • 11
  • 2
  • Are you using colab? – Dr. Snoopy Jun 07 '21 at 18:33
  • 1
    Also please add full imports to your question. – Dr. Snoopy Jun 07 '21 at 18:40
  • Yes. I use Colab. – 김기완 Jun 08 '21 at 00:21
  • I made my model by using Sequential().(this described above) And I want to train this model by model.fit & callback But whenever i save my model by ModelCheckpoint, "Layer ModuleWrapper has arguments in `__init__` and therefore must override `get_config`". occured In my thought, something isn't renewed so that error occurs. But I can't find what it is. – 김기완 Jun 08 '21 at 00:38
  • https://stackoverflow.com/questions/58678836/notimplementederror-layers-with-arguments-in-init-must-override-get-conf/58799021 In this page, he solved the problem by def get_config(self): config = super().get_config().copy() return config in def. But I didn't define my model by def. If I have to define "def get_config", where is the location? – 김기완 Jun 08 '21 at 00:38
  • You still need to add all the imports to your question. – Dr. Snoopy Jun 08 '21 at 00:40
  • sorry but could you tell me what is "import" of the question? – 김기완 Jun 08 '21 at 01:22
  • The python statements you use to import libraries like keras in your code. – Dr. Snoopy Jun 08 '21 at 01:29
  • import tensorflow / from tensorflow import keras / from keras.layers import LeakyReLU / from keras.callbacks import EarlyStopping / from keras.callbacks import ModelCheckpoint / I used these and keras version is 2.5.0 – 김기완 Jun 08 '21 at 04:24
  • Dr.Snoopy. I found what is wrong. Maybe "from tensorflow import keras" is wrong. When I changed that code into just "import keras", it works. Thank you for your help! – 김기완 Jun 08 '21 at 06:25
  • You were mixing imports between tf.keras and keras which is not supported – Dr. Snoopy Jun 08 '21 at 06:57

1 Answers1

0

This problem is caused by mixing imports between the keras and tf.keras libraries, which is not supported.

You should never mix imports between these libraries, as it will not work and produces all kinds of strange error messages. These errors change with versions of keras and tensorflow.

Dr. Snoopy
  • 55,122
  • 7
  • 121
  • 140