Keras using Google Collaboration cannot model.save

Asked 2 years ago, Updated 2 years ago, 116 views

I'm learning image recognition through a book called Image Recognition Programming Recipe.
When I try to save the model in keras, an error occurs and I cannot save it.
I thought the following site might be helpful, but I couldn't understand it well even after reading it.

Try to model.save in Keras and getoverrideget_configWhat to do with the error

Due to the number of characters, we have extracted some of the models.
Please let me know if you need any information.

Model

model=Sequential()

# decide how to learn

# (Learning style, loss function, evaluation method of correctness)
model.compile(optimizer=keras.optimizers.Adadelta(),
            loss=keras.losses.categorical_crossentropy,
            metrics=["accuracy"])

print("Repeated Learning Count:",EPOCHS)
fit_record=model.fit(train_data,train_teacher_labels,batch_size=BATCH_SIZE,epochs=EPOCHS, verbose=1,validation_data=(test_data,test_teacher_labels))

# building neural networks

# Convolution layer (number of neurons in input, width and height of convolution area, activation function, format of input data)
model.add(Conv2D(32, kernel_size=(3,3), activation="relu", input_shape=input_shape))
model.add(Conv2D(64,(3,3), activation="relu"))
# pooling layer
model.add(MaxPooling2D(pool_size=(2,2))))
# Dropout layer (prevent overlearning)
model.add (Dropout (0.25))
#smooth the input
model.add(Flatten())
# Full bond layer (128 nodes, activation function)
model.add(Dense(128, activation="relu"))
model.add(Dropout(0.5))
# Output layer (numbers num_classes)
model.add(Dense(NUM_CLASSES, activation="softmax"))

When I got an error

model.save("keras-mnist-model.h5")

Error Contents

NotImplementedError Traceback (most recent call last)
<ipython-input-90-a2ac93d46f7c>in<module>()
---->1model.save("keras-mnist-model.h5")

8 frames
/usr/local/lib/python 3.7/dist-packages/tensorflow/python/keras/engine/base_layer.py inget_config(self)
    719 raise NotImplementedError('Layer%shas arguments in `__init__`and'
    720'therefore must override`get_config`.'%
-->721self.__class__.__name__)
    722 return config
    723 

NotImplementedError: Layer ModuleWrapper has arguments in `__init__` and therefore must override`get_config`.

keras 2.5.0
tensorflow 2.5.0
numpy1.19.5
macOSX
Google colab

python machine-learning tensorflow keras google-colaboratory

2022-09-30 16:24

1 Answers

Consider MNIST as an example:

    Compile the
  • model after creating the architecture
  • Training the model (call the fit method) should be done after creating an architecture and compiling.
  • Verify that the shape of the data is correct.
import numpy as np
from tensorflow import keras
from tensorflow.keras.layers import*
from tensorflow.keras.models import Sequential

(x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data()

EPOCHS=2
BATCH_SIZE = 1000
NUM_CLASSES=10

x_train=x_train.astype("float32")/255
x_test=x_test.astype("float32")/255
x_train=np.expand_dims(x_train, -1)
x_test=np.expand_dims(x_test, -1)
y_train=keras.utils.to_categorical(y_train,NUM_CLASSES)
y_test=keras.utils.to_categorical(y_test,NUM_CLASSES)

model=Sequential()

model.add(keras.Input(shape=(28,28,1))))
model.add(Conv2D(32, kernel_size=(3,3), activation="relu"))
model.add(Conv2D(64,(3,3), activation="relu"))
model.add(MaxPooling2D(pool_size=(2,2))))
model.add (Dropout (0.25))
model.add(Flatten())
model.add(Dense(128, activation="relu"))
model.add(Dropout(0.5))
model.add(Dense(NUM_CLASSES, activation="softmax"))

model.compile(optimizer=keras.optimizers.Adadelta(),
            loss=keras.losses.categorical_crossentropy,
            metrics=["accuracy"])

fit_record=model.fit(x_train,y_train,batch_size=BATCH_SIZE,epochs=EPOCHS, verbose=1,validation_data=(x_test,y_test))

model.save("keras-mnist-model.h5")


2022-09-30 16:24

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.