Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (19.9k points)

I have a simple code snippet to train a model but, when I use pickle to save the model for future use, it gives me an error message:

cannot pickle thread.LOCK objects

I used the pickle in more than one format yet it gives me the same error.

import pickle

model = keras.Sequential([

    keras.layers.Dense(SHAPE, input_shape=(SHAPE,)),

    keras.layers.Dense(300, activation='sigmoid'),

    keras.layers.Dense(10, activation='softmax')

])

#******************    COMPILING THE MODE        *****************

LEARNING_RATE = 0.0005

model.compile(optimizer=keras.optimizers.Adam(lr=LEARNING_RATE),

              loss='sparse_categorical_crossentropy',

              metrics=['accuracy']              

             )

# ***********      TRAINING THE MODEL   **********

EPOCHS = 20

BATCH_SIZE=50

history_original_data = model.fit(X_original_train_images, y_original_train_labels, epochs=EPOCHS, batch_size=BATCH_SIZE) 

hist_original=history_original_data.history

### PICKLE TO SAVE THE MODEL TO BE USED WITHOU PRO-TRAINING IT

pickname ="SequentialNeuroNetwork.pkl"

PickleSeq = open(pickname, 'wb')

pickle.dump(model, PickleSeq)

PickleSeq.close()

I was expecting the above code snippet to run smoothly but it is taking the toll out on me.

1 Answer

0 votes
by (25.1k points)

Instead of using pickle you should use model.save() as it is recommended in keras official documentation, pickle is not available in older versions of keras so using model.save() is the better option.

Related questions

Browse Categories

...